Jump to content

Summarize primes: Difference between revisions

m
Forth - faster without using a sieve
(→‎{{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:
=={{header|Forth}}==
{{works with|Gforth}}
<lang forth>: prime? ( n -- ?flag ) here + c@ 0= ;
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 -- }
35
here n erase
0 notprime!
1 notprime!
n 4 > if
n 4 do i notprime! 2 +loop
then
3
begin
dup2dup dup * n <>=
while
2dup mod 0= if 2drop false exit then
dup prime? if
n over dup * do
i notprime!
dup 2* +loop
then
2 +
2dup mod 0= if 2drop false exit then
n 4 >4 if+
repeat
drop2drop true ;
 
: main
0 0 { count sum }
." count prime sum" cr
100000 prime_sieve
1000 2 do
i prime? if
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.