From: Jose Augusto Date: 2006-12-27T11:51:47+09:00 Subject: Re: Peculiar Behavior for a Newby to Undertstand ------=_Part_96051_2676466.1167187905657 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Charles, I entered the code in Ruby 1.8.5 in Windows, using Scintilla IDE :-)) (the joke is that Scintilla-Scite is not an IDE at all!) it gave the error: >ruby prime.rb prime.rb:34: undefined local variable or method `primes' for main:Object (NameError) Then changed a=primes.new a.show_primes(1000000,1000100) to a=Primes::new a.show_primes(1000000,1000100) The good result appeared :-) >ruby prime.rb 1000003 1000033 1000037 1000039 1000081 1000099 >Exit code: 0 Don't know how you had not an error instead of the wrong result before... For that you need a Ruby guru :-) Kind Regards J. Augusto On 12/27/06, Charles A Gray wrote: > > I created a class for prime numbers as so: > > class Primes > def initialize > end > > def prime?(number) > # Method returns true if number is prime. > limit = Math.sqrt(number).ceil > flag = true > if number % 2 == 0 > flag = false > else > 3.step(limit,2) {|i| > if number % i == 0 > flag = false > break > end > } > end > return flag > end > > def show_primes(lower, upper) > # Prints all primes between lower and upper > # Lower is incremented 1 if it is even. > # The arcane "(((lower/2).floor)*2+1)" performs this task > (((lower/2).floor)*2+1).step(upper,2) {|i| > if prime?(i) == true > print i.to_s + " " > end > } > end > end > 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 enter the same methods outside of class Primes and enter > show_primes(1000000,1000100) I don't get the lower limit at the end of > the printed values. > > I am running version 1.18.4 using scite in Ubuntu. > -- > Charles Gray -- Phoenix, AZ; Where you can bake the chill out of your > bones > > > > ------=_Part_96051_2676466.1167187905657--