Raku / old-issue-tracker

Tickets from RT
https://github.com/Raku/old-issue-tracker/issues
2 stars 1 forks source link

r34778 - code takes forever #565

Closed p6rt closed 15 years ago

p6rt commented 15 years ago

Migrated from rt.perl.org#61904 (status was 'resolved')

Searchable as RT61904$

p6rt commented 15 years ago

From mah@everybody.org

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);

p6rt commented 15 years ago

From mah@everybody.org

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.

p6rt commented 15 years ago

From Andy_Bach@wiwb.uscourts.gov

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.

p6rt commented 15 years ago

The RT System itself - Status changed from 'new' to 'open'

p6rt commented 15 years ago

From mah@everybody.org

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);

p6rt commented 15 years ago

From @pmichaud

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

p6rt commented 15 years ago

@pmichaud - Status changed from 'open' to 'resolved'