It doesn’t work as expected… any help would be very appreciated.
class Integer
def is_prime?(k)
if self % 2 == 0
return ‘NO’
end
n = self
d = n - 1
s = 0
while d % 2 == 0
d /= 2
s += 1
end
k.times do
a = rand(n - 1) + 1
t = true
for r in 0...s
if (a ** d) % n == 1 or (a ** ((2 ** r) * d)) % n == -1
t = false
end
end
return 'NO' if t == true
end
return 'YES'
end
end
STDIN.gets.to_i.times do
puts STDIN.gets.to_i.is_prime?(25)
end
According to the page you posted your condition is not correct.
I don’t read it carefully nor I tested it but it’s obvious that
you forget to calculate the -1 mod n
… == (-1 % n)
According to the page you posted your condition is not correct.
I don’t read it carefully nor I tested it but it’s obvious that
you forget to calculate the -1 mod n
Thanks it seems to be working now, sometimes you just miss the obvious.
According to the page you posted your condition is not correct.
I don’t read it carefully nor I tested it but it’s obvious that
you forget to calculate the -1 mod n
Thanks it seems to be working now, sometimes you just miss the obvious.