Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse Totient Function

Tags:

c++

c

algorithm

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;
}
like image 541
Gaurav Deshmukh Avatar asked Nov 03 '22 05:11

Gaurav Deshmukh


1 Answers

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 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.

like image 139
Daniel Fischer Avatar answered Nov 15 '22 00:11

Daniel Fischer