euler_totient_function.jl 479 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/julia
  2. # Trizen
  3. # 09 November 2016
  4. # https://github.com/trizen
  5. # A simple implementation of Euler's totient function.
  6. # See also:
  7. # https://www.youtube.com/watch?v=fq6SXByItUI
  8. # https://en.wikipedia.org/wiki/Euler%27s_totient_function
  9. using Primes
  10. function Φ(n::Int64)
  11. for p in keys(factor(n))
  12. n -= div(n, p)
  13. end
  14. n
  15. end
  16. # Φ(240) = Φ(2^4 * 3^1 * 5^1)
  17. # Φ(240) = (2^4 - 2^3) * (3^1 - 3^0) * (5^1 - 5^0)
  18. # Φ(240) = 64
  19. println(Φ(240))