Skip to content

Instantly share code, notes, and snippets.

@mame-n
Created February 15, 2017 15:32
Show Gist options
  • Save mame-n/b899c9a9bce31690145e7423992d5f20 to your computer and use it in GitHub Desktop.
Save mame-n/b899c9a9bce31690145e7423992d5f20 to your computer and use it in GitHub Desktop.
class P69
MAXN = 1000000
def initialize(n = MAXN)
@n = n
end
def main
phis = phis( @n )
phis.index( max_phi( phis ) ) + 1
end
def max_phi( phis )
phis.inject do |max,phi|
max < phi ? phi : max
end
end
def phis( k )
(1..k).map do |n|
n/phi(n).to_f
end
end
def phi(n)
n.downto(1).select do |i|
n.gcd(i) == 1
end.size
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment