You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

38 lines
611 B

( Forth version of a Prime Number Finder
haplo@mindstab.net )
: check ( m cnt -- b )
{ m cnt }
\ var i
-1 3 -1
begin
over m <= and while
nip
dup 2 + swap cnt swap mod tuck ( # cnt+2 # )
repeat
drop
;
: primes ( m sq sqm -- )
{ m sq sqm }
\ var i sq sqm
sqm sq 3
begin
dup m <= while
over over \ sq(max) cnt
check ( max cnt -- b )
if dup . CR then
2 +
rot ( sq cnt sqm ) over over
> if
rot ( cnt sqm sq )
1 + nip ( cnt sq )
dup dup * ( cnt sq sqm )
rot swap ( sq cnt sqm )
then
\ sqm sq 3
rot rot
repeat
drop drop drop
;