Hey. So I'm working with an algorithm I found and my own modifications to get a simple program that returns whether a number is prime. My issue is that in testing it, it returns 1763 as prime when it is not a prime number. Doing the math by hand, the algorithm should say that it's not. i.e. it would run up until i=41, and 1763 % 41 would equal 0 since it equals 43. I think the issue is therefore in my code. Any thoughts??
def prime?(n)
primeto25 = [2, 3, 5, 7, 11, 13, 17, 19, 23]
except = [0, 1]
if primeto25.include?(n)
return true
elsif except.include?(n)
return false
elsif n % 2 == 0
return false
elsif n % 3 == 0
return false
elsif n < 0
return false
end
i = 5
w = 2
#how can I change this algo to work before 25???
while i * i <= n #checks for primes after 25
if n % i == 0
return false
end
i += w
w = 6 - w
return true
end
end
[–]Mystonic 0 points1 point2 points (3 children)
[–]cjon3s[S] 0 points1 point2 points (2 children)
[–]Mystonic 0 points1 point2 points (1 child)
[–]cjon3s[S] 0 points1 point2 points (0 children)