Will this work and/or what should I do to improve it?
n = 2
peter = gets
until n > peter**0.5
if peter % n == 0
peter = peter / n
n = 2
else
n = n + 1
end
end
print peter
Printable View
Will this work and/or what should I do to improve it?
n = 2
peter = gets
until n > peter**0.5
if peter % n == 0
peter = peter / n
n = 2
else
n = n + 1
end
end
print peter
Code Ruby:
input = gets.to_i # this code is the sieve of Eratosthenes, I just googled for it sieve = [] for i in 2 .. input sieve[i] = i end for i in 2 .. Math.sqrt(input) next unless sieve[i] (i*i).step(input, i) do |j| sieve[j] = nil end end # here we look for the highest prime factor sieve.compact.reverse.each do |p| if input % p == 0 print p exit end end