Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:48:46+00:00 2026-06-18T06:48:46+00:00

Given n , I want to find I such that phi(i) = n .

  • 0

Given n, I want to find I such that phi(i) = n. n <= 100,000,000.
Maximum value of i = 202918035 for n = 99683840. I want to solve this problem

My approach is to pre-compute totient function of all numbers upto maximum i.
For that I am first finding all prime numbers up to maximum i using sieve of erathronese. Totient of prime numbers is recorded at the time of sieve.
Then using

enter image description here

Then I search for input number in phi array and print result to output.
But it is giving time limit exceeded.
What can be further optimized in pre-computation or there is some better way to do this?

My code is:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

int* Prime = (int*)malloc(sizeof(int) * (202918036 >> 5 + 1));
int* pos = (int*)malloc(sizeof(int) * (11231540));
int* phi = (int*)malloc(sizeof(int) * 202918036);

#define prime(i) ((Prime[i >> 5]) & (1 << (i & (31))))
#define set(j) (Prime[j >> 5] |= (1 << (j & (31))))
#define LIM 202918035
#define SLIM 14245

int sieve() {
    int i, j, m, n, t, x, k, l, h;
    set(1);
    phi[0] = 0;
    phi[1] = 0;
    pos[1] = 2;
    phi[2] = 1;
    pos[2] = 3;
    phi[3] = 2;
    for (k = 2, l = 3, i = 5; i <= SLIM; k++, i = 3 * k - (k & 1) - 1)
    if (prime(k) == 0) {
        pos[l++] = i;
        phi[i] = i - 1;
        for (j = i * i, h = ((j + 2) / 3); j <= LIM; h += (k & 1) ? (h & 1 ? ((k << 2) - 3) : ((k << 1) - 1)) : (h & 1 ? ((k << 1) - 1) : ((k << 2) - 1)), j = 3 * h - (h & 1) - 1)
        set(h);
    }

    i = 3 * k - (k & 1) - 1;
    for (; i <= LIM; k++, i = 3 * k - (k & 1) - 1)
    if (prime(k) == 0) {
        pos[l++] = i;
        phi[i] = i - 1;
    }
    return l;
}

int ETF() {
    int i;
    for (i = 4; i < LIM; i++) {
        if (phi[i] == 0) {
            for (int j = 1; j < i; j++) {
                if (i % pos[j] == 0) {
                    int x = pos[j];
                    int y = i / x;
                    if (y % x == 0) {
                        phi[i] = x * phi[y];
                    } else {
                        phi[i] = phi[x] * phi[y];
                    }
                    break;
                }
            }
        }
    }
}

int search(int value) {
    for (int z = 1; z < LIM; z++) {
        if (phi[z] == value) return z;
    }
    return -1;
}


int main() {

    int m = sieve();

    int t;
    ETF();

    scanf("\n%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        if (n % 2 == 1) {
            printf("-1\n");
        } else {
            int i;
            i = search(n);
            if (i == -1) printf("-1\n");
            else printf("%d\n", i);
        }

    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:48:47+00:00Added an answer on June 18, 2026 at 6:48 am

    This

         for(int j=1;j<i;j++)
         {            
            if(i%pos[j]==0)
            {
    

    means you’re finding the smallest prime factor of i by trial division. That makes your algorithm O(n^2/log^2 n), since there are about n/log n primes not exceeding n, and for a prime i, you test all primes not exceeding i.

    You can get a much faster algorithm (I doubt it will be fast enough, though) if you find the smallest [or any] prime factors using a sieve. That’s a simple modification of the Sieve of Eratosthenes, instead of just marking a number as composite, you store the prime as a factor of that number. After having filled a sieve with prime factors of each number, you can compute the totient like you do as either

    phi[i] = p*phi[i/p]
    

    if p² divides i or

    phi[i] = (p-1)*phi[i/p]
    

    if it doesn’t.

    Computing the totients using that method is an O(n*log n) [perhaps even O(n*log log n), I haven’t analysed it in detail yet] algorithm.

    Further, your search

    int search(int value) {
        for (int z = 1; z < LIM; z++) {
            if (phi[z] == value) return z;
        }
        return -1;
    }
    

    is very slow. You could make a lookup table to get O(1) lookup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to solve the following optimization problem: Non-Latex: Given x and mu, find
Given a string such as: a:2:{i:0;s:1:1;i:1;s:1:2;} I want to find every integer within quotes
I want to find a combinatorial formula that given a certain number of integers,
I will phrase the problem in the precise form that I want below: Given
I want to find out that out of the given <li> which li has
I want to solve this CodeChef challenge: Suppose We are given an array A
I want find all Saturdays and Sundays in A given month. How can I
I want to find out the following: given a date ( datetime object), what
I want to find files older than N days from a given timestamp in
Given a word W, I want to find all words containing the letters in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.