[Python] Computing Eulers Totient Function

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to find an efficient way to compute Euler's totient function.

    What is wrong with this code? It doesn't seem to be working.

    def isPrime(a):
    return not ( a < 2 or any(a % i == 0 for i in range(2, int(a ** 0.5) + 1)))

    def phi(n):
    y = 1
    for i in range(2,n+1):
    if isPrime(i) is True and n % i == 0 is True:
    y = y * (1 - 1/i)
    else:
    continue
    return int(y)

    Continue reading...

Compartilhe esta Página