I was solving Euler's project question number 70 and my Euler's totient function was slow. Can anyone help?
Euler project Question 70 description:
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of positive numbers less than or equal to n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9)=6. The number 1 is considered to be relatively prime to every positive number, so φ(1)=1.
Interestingly, φ(87109)=79180, and it can be seen that 87109 is a permutation of 79180.
Find the value of n, 1 < n < N, for which φ(n) is a permutation of n and the ratio n/φ(n) produces a minimum.
Input format: Contains and integer N
Constraints: 1<=N<=10**7
Output format: Print the answer corresponding to the test case
Sample Input: 100
Sample Output: 21
This optimized code does not pass for 5 cases out of 10. The phi function is slow. I don't know what else to do to optimize it.
from math import gcd
from itertools import permutations
def totatives(n):
phi = int(n > 1 and n)
for p in range(2, int(n ** .5) + 1):
if not n % p:
phi -= phi // p
while not n % p:
n //= p
#if n is > 1 it means it is prime
if n > 1: phi -= phi // n
return phi
def permute(num,phi_num):
temp="".join(sorted(str(num)))
phi_num="".join(sorted(str(phi_num)))
return temp==phi_num
N=int(input())
d={}
for n in range(12,N):
if permute(n,totatives(n)):
#print(permute,phi(n))
d[n]=(n/totatives(n))
#print(d)
min_b=min(d.values())
for a,b in d.items():
if b==min_b:
print(a)
break