primes/primes.fs

39 lines
611 B
Forth
Raw Normal View History

2011-03-08 07:01:27 +00:00
( 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
;