potion - Printing odd prime every 100K primes found -
i'm trying make program print every 100k-th odd prime number until 10m using potion, code:
last = 3 res = (last) # create array loop: last += 2 prime = true len = res length -1 = 0 while(i<len): v = res at(i) if(v*v > last): break. if(last%v == 0): prime = false, break. += 1 . if(prime): res append(last) if(res size % 100000 == 0): last print. if(last>9999999): break. . .
but gives segmentation fault (core dumped)
, wonder what's wrong?
for reference, working ruby version:
res = [last=3] loop last += 2 prime = true res.each |v| break if v*v > last if last % v == 0 prime = false break end end if prime res << last puts last if res.length % 100000 == 0 break if last > 9999999 end end
the output should be:
1299721 2750161 4256249 5800139 7368791 8960467
and no, not homework, out of curiosity.
you found out yourself, great! println
called say
in potion. , crashed in res size
.
e.g. use debbugging: rm config.inc make debug=1 bin/potion -d -dvt example/100thoddprime.pn
and press enter until crash.
(example/100thoddprime.pn:18): res append(last)
>
; (3, 5) [95] getlocal 1 1 ; (3, 5) [96] move 2 1 ; (3, 5) [97] loadk 1 5 ; size [98] bind 1 2 ; function size() [99] loadpn 3 0 ; nil [100] call 1 3segmentation fault
so size on res returned nil, , caused crash.
and instead of last print, "\n" print.
last say.
came perl6 syntax, sorry :)
Comments
Post a Comment