Closed p6rt closed 15 years ago
Again, playing around with Euler, wrote myself a simple little class to generate primes.
Pugs gives the correct result, printing out the first 10 primes.
Rakudo prints ‘2' and then sits spinning.
#!perl6
class Prime { has @!primes is rw; has Int $!number is rw = 1;
method next() { my $not_prime = 1;
while($not_prime && $!number++) { $not_prime = @!primes.grep({$!number % $^a == 0}); } @!primes.push($!number);
my $copy = $!number; return $copy; } }
my $p = Prime.new; say $p.next for (1..10);
Sorry for not finding the specific problem before.
while\($not\_prime && $\!number\+\+\) \{ $not\_prime = @​\!primes\.grep\(\{$\!number % $^a == 0\}\); \}
The problem with Rakudo is that it is setting @!primes to a list containing a single item (the latest value of $!number) on each turn through the loop.
Saying $not_prime after the expression:
while\($not\_prime && $\!number\+\+\) \{ $not\_prime = @​\!primes\.grep\(\{$\!number % $^a == 0\}\); say $not\_prime; \}
shows $not_prime is being set to the current value of $!number. And of course, since $not_prime is always true, the loop never exits.
What I don't understand it why it works on the first time through, but never after that.
Oh, as an added bonus, adding the say causes a segfault.
class Prime { has @!primes is rw; has Int $!number is rw = 1;
method next() { my $not_prime = 1;
while($not_prime && $!number++) { $not_prime = @!primes.grep({$!number % $^a == 0}); }
Appears that last assignment changes $not_prime.WHAT from "Int" into "Array".
a
Andy Bach Systems Mangler Internet: andy_bach@wiwb.uscourts.gov Voice: (608) 261-5738; Cell: (608) 658-1890
Straw? No, too stupid a fad. I put soot on warts.
The RT System itself - Status changed from 'new' to 'open'
Again, playing around with Euler, wrote myself a simple little class to generate primes.
Pugs gives the correct result, printing out the first 10 primes.
Rakudo prints ‘2' and then sits spinning.
#!perl6
class Prime { has @!primes is rw; has Int $!number is rw = 1;
method next() { my $not_prime = 1;
while($not_prime && $!number++) { $not_prime = @!primes.grep({$!number % $^a == 0}); } @!primes.push($!number);
my $copy = $!number; return $copy; } }
my $p = Prime.new; say $p.next for (1..10);
On Fri Jan 02 18:02:41 2009, Andy_Bach@wiwb.uscourts.gov wrote:
class Prime { has @!primes is rw; has Int $!number is rw = 1;
method next() { my $not_prime = 1;
while\($not\_prime && $\!number\+\+\) \{ $not\_prime = @​\!primes\.grep\(\{$\!number % $^a == 0\}\); \}
Appears that last assignment changes $not_prime.WHAT from "Int" into "Array".
Correct. And since $not_prime therefore always has at least one element, we end up spinning forever.
So, marking this as not-a-bug and closing ticket. Thanks!
Pm
@pmichaud - Status changed from 'open' to 'resolved'
Migrated from rt.perl.org#61904 (status was 'resolved')
Searchable as RT61904$