From: Pete Yandell Date: 2006-12-27T15:14:50+09:00 Subject: Re: Peculiar Behavior for a Newby to Undertstand (Sorry, previous message got eaten for some reason. I'll try again.) On 27/12/2006, at 12:20 PM, Charles A Gray wrote: > Then when I enter > a=primes.new > a.show_primes(1000000,1000100) I get > 1000003 1000033 1000037 1000039 1000081 1000099 1000001 > Where is that trailing 1000001 coming from? It is not a prime > number and > in fact is the lower limit. > If I do exactly the same in my Ruby 1.8.5 irb, I get: irb(main):034:0> a.show_primes(1000000,1000100) 1000003 1000033 1000037 1000039 1000081 1000099 => 1000001 The 1000001 is the return value of the method, which irb prints after your method finishes printing the primes. So your method does the right thing, but irb output is confusing you. Just for fun, I've rewritten your code to be shorter and a bit more Rubyish: class Integer def prime? return false if self % 2 == 0 3.step(Math.sqrt(self).ceil, 2) {|i| return false if self % i == 0 } true end end Now you can simply ask n.prime? for any integer n. Listing primes then becomes as simple as: (1000000...1000100).select {|i| i.prime? } => [1000003, 1000033, 1000037, 1000039, 1000081, 1000099]} Cheers, Pete Yandell