python - Computing Eulers Totient Function -
i trying find efficient way compute euler's totient function.
what wrong code? doesn't seem working.
def isprime(a): return not ( < 2 or any(a % == 0 in range(2, int(a ** 0.5) + 1))) def phi(n): y = 1 in range(2,n+1): if isprime(i) true , n % == 0 true: y = y * (1 - 1/i) else: continue return int(y)
here's faster, working way, based on description on wikipedia:
thus if n positive integer, φ(n) number of integers k in range 1 ≤ k ≤ n gcd(n, k) = 1.
i'm not saying fastest or cleanest, works.
import fractions def phi(n): amount = 0 k in range(1, n + 1): if fractions.gcd(n, k) == 1: amount += 1 return amount
Comments
Post a Comment