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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -