Skip to content

Instantly share code, notes, and snippets.

@hyuki0000
Created July 2, 2017 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyuki0000/f576912ba328ec2c266278d812f5f059 to your computer and use it in GitHub Desktop.
Save hyuki0000/f576912ba328ec2c266278d812f5f059 to your computer and use it in GitHub Desktop.
オイラーのφ関数を、RubyのPrimeモジュールを使って素朴に実装したものです
require 'prime'
def φ(n)
if n == 1
return 1
end
Prime.each(n-1) do |p|
if n % p == 0
b = n
j = 0
while b % p == 0
b /= p
j += 1
end
return p ** (j-1) * (p-1) * φ(b)
end
end
return n-1
end
(1..20).each do |n|
print "#{φ(n)}, "
end
puts
@hyuki0000
Copy link
Author

20170702215525

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment