Forum: Ruby prime numbers problem

Posted by Roelof Wobben (roelof)
on 2012-09-30 17:49
(Received via mailing list)
Hello,

I found out that ruby 1.9 has a prime class.
So I tried this : list_primes.each { |prime| print prime, " "; break 
unless prime < 90 }

And I get this answer : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 
61 67 71 73 79 83 89 97

That looks no good because 97 is more then 90.

How can this be happen ?

Roelof
Posted by Xavier Noria (fxn)
on 2012-09-30 17:53
(Received via mailing list)
On Sun, Sep 30, 2012 at 5:48 PM, Roelof Wobben <rwobben@hotmail.com> 
wrote:

  Hello,
> How can this be happen ?
>

Because the loop first print, and checks later.
Posted by Stefano Crocco (crocco)
on 2012-09-30 17:54
(Received via mailing list)
On Monday 01 October 2012 Roelof Wobben wrote
>
> How can this be happen ?
>
> Roelof

Well, you're telling ruby to break the loop after having printed out the
number, so of course it'll print 97, then break the loop. You'll need to
reverse the order of the expressions inside the block to achieve what 
you
want.

Stefano
Posted by Roelof Wobben (roelof)
on 2012-09-30 18:56
(Received via mailing list)
Thanks it worked now and I solved the problem. Roelof
 > Date: Mon, 1 Oct 2012 00:54:05 +0900
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.