On 25.07.2009 12:14, David A. Black wrote: > Hi -- > > On Sat, 25 Jul 2009, Lloyd Linklater wrote: > >> 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[i] = 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[i] = false } >> currentNum += 2 until primes[currentNum] == true >> end >> >> but that just hangs the program. What am I missing? > > When you do: > > statement until condition > > statement isn't executed at all unless condition is true. Since > primes[3] is false, the incrementation never takes place. > > You can guarantee that the body of an until statement gets executed at > least once by doing it like this: > > begin > num +=2 > end while primes[num] Shouldn't that read begin num += 2 end until primes[num] ? At least that would be a direct translation of the original currentNum += 2 while primes[currentNum] == false do currentNum += 2 end > There's another issue here, though. Since the default value for > non-existent array values is nil, the test for truth will fail even if > num has gone over the maximum. So you might want to flip the logic: There's yet another issue: the limit highestPrime is not honored during the incrementation of current_num which likely leads to the hanging which I assume is in fact an endless loop. The whole looping construction looks suspicious though: there is an outer loop and two inner loops of which I am not sure they align properly. Lloyd, I believe you should reevaluate your logic. This reminds me of Eratosthenes' Sieve but your code is different. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/