Created
July 2, 2017 12:53
-
-
Save hyuki0000/f576912ba328ec2c266278d812f5f059 to your computer and use it in GitHub Desktop.
オイラーのφ関数を、RubyのPrimeモジュールを使って素朴に実装したものです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
hyuki0000
commented
Jul 2, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment