Summarize primes: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Small change so it can also do the "Prime numbers p which sum of prime numbers less or equal to p is prime" task) |
m (Forth - faster without using a sieve) |
||
Line 448: | Line 448: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|Gforth}} |
{{works with|Gforth}} |
||
<lang forth>: prime? ( n -- |
<lang forth>: prime? ( n -- flag ) |
||
dup 2 < if drop false exit then |
|||
: notprime! ( n -- ) here + 1 swap c! ; |
|||
dup 2 mod 0= if 2 = exit then |
|||
dup 3 mod 0= if 3 = exit then |
|||
: prime_sieve { n -- } |
|||
⚫ | |||
here n erase |
|||
0 notprime! |
|||
1 notprime! |
|||
⚫ | |||
n 4 do i notprime! 2 +loop |
|||
then |
|||
⚫ | |||
begin |
begin |
||
2dup dup * >= |
|||
while |
while |
||
2dup mod 0= if 2drop false exit then |
|||
dup prime? if |
|||
n over dup * do |
|||
i notprime! |
|||
dup 2* +loop |
|||
then |
|||
2 + |
2 + |
||
2dup mod 0= if 2drop false exit then |
|||
⚫ | |||
repeat |
repeat |
||
2drop true ; |
|||
: main |
: main |
||
0 0 { count sum } |
0 0 { count sum } |
||
." count prime sum" cr |
." count prime sum" cr |
||
100000 prime_sieve |
|||
1000 2 do |
1000 2 do |
||
i prime? if |
i prime? if |