For n = p^a * q^b , the totem Ο(n) = (p-1)*p^(a-1) * (q-1)*q^(b-1) . Without loss of generality, p < q .
So gcd(n,Ο(n)) = p^(a-1) * q^(b-1) if p does not divide q-1 and gcd(n,Ο(n)) = p^a * q^(b-1) if p divides q-1 .
In the first case, we have n/gcd(n,Ο(n)) = p*q and Ο(n)/gcd(n,Ο(n)) = (p-1)*(q-1) = p*q + 1 - (p+q) , so you have x = p*q = n/gcd(n,Ο(n)) and y = p+q = n/gcd(n,Ο(n)) + 1 - Ο(n)/gcd(n,Ο(n)) . Then the search for p and q is simple: y^2 - 4*x = (qp)^2 , therefore q = (y + sqrt(y^2 - 4*x))/2 and p = yq . Then finding the exponents a and b trivial.
In the second case, n/gcd(n,Ο(n)) = q . Then you can easily find the exponent b divisible by q until the division leaves a remainder, and thus get p^a . Dividing Ο(n) by (q-1)*q^(b-1) gives you z = (p-1)*p^(a-1) . Then p^a - z = p^(a-1) and p = p^a/(p^az) . The search for exponent a is trivial again.
So it remains to decide in which case you have. You have case 2 if and only if n/gcd(n,Ο(n)) is simple.
To do this, you need a decent test for compliance. Or you can first assume that you have case 1, and if that does not work, conclude that you have case 2.
source share