248 Numbers for which Euler s totient function equals 13.sf 991 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/ruby
  2. # Algorithm from invphi.gp ver. 2.1 by Max Alekseyev.
  3. # See also:
  4. # https://home.gwu.edu/~maxal/gpscripts/
  5. # https://projecteuler.net/problem=248
  6. # Runtime: 12.280s
  7. func dynamicPreimage(N, L, unitary = false) {
  8. var r = Hash(1 => [1])
  9. L.each {|l|
  10. var t = Hash()
  11. l.each_2d {|a,b|
  12. N/a -> divisors.each {|d|
  13. t{a*d} := [] << ((unitary ? r{d}.grep{.is_coprime(b)} : r{d}) ~S* b)... if r.has(d)
  14. }
  15. }
  16. t.each {|k,v|
  17. r{k} := [] << v...
  18. }
  19. }
  20. r{N} \\ [] -> sort
  21. }
  22. func cook_phi(N) {
  23. var L = Hash()
  24. N.divisors.each {|d|
  25. var p = d+1 -> is_prime || next
  26. var v = N.valuation(p)
  27. L{p} := [] << {|k| [d*(p**(k-1)), p**k] }.map(1 .. v+1)...
  28. }
  29. L.values
  30. }
  31. # Inverse of Euler phi function
  32. func inverse_phi(N) {
  33. dynamicPreimage(N, cook_phi(N))
  34. }
  35. say ::inverse_phi(13!)[150_000 - 1] # built-in
  36. say inverse_phi(13!)[150_000 - 1]