L
Lloyd Linklater
I am writing a little thing to find all the prime numbers to a million.
I got it to work this way:
highestPrime = 1_000_000
primes = Array.new(highestPrime, true)
currentNum = 3
while currentNum < highestPrime do
(currentNum*currentNum).step(highestPrime, currentNum * 2) { |i|
primes = false }
currentNum += 2
while primes[currentNum] == false do
currentNum += 2
end
end
I am unhappy with using currentNum += 2 twice. I very much want to
write something like this:
while currentNum < highestPrime do
(currentNum*currentNum).step(highestPrime, currentNum * 2) { |i|
primes = false }
currentNum += 2 until primes[currentNum] == true
end
but that just hangs the program. What am I missing?
I got it to work this way:
highestPrime = 1_000_000
primes = Array.new(highestPrime, true)
currentNum = 3
while currentNum < highestPrime do
(currentNum*currentNum).step(highestPrime, currentNum * 2) { |i|
primes = false }
currentNum += 2
while primes[currentNum] == false do
currentNum += 2
end
end
I am unhappy with using currentNum += 2 twice. I very much want to
write something like this:
while currentNum < highestPrime do
(currentNum*currentNum).step(highestPrime, currentNum * 2) { |i|
primes = false }
currentNum += 2 until primes[currentNum] == true
end
but that just hangs the program. What am I missing?