Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

primality testing

Name: Anonymous 2007-06-18 11:04 ID:qFudCK33

so i was playing with factor and i wrote this function:
: prime? ( n -- ? ) [ dup sqrt >bignum 1 - [ 2 + , ] each ] { } make [ 2dup mod swap drop ] map [ 0 = ] find drop not swap 1 > and ;

it takes less than a second to determine that 2003663613*2^195000+1 (the largest known prime number according to http://primes.utm.edu/largest.html) is prime and less than a second to determine that 2003663613*2^195000 is not...

so i'm wondering... how would one go about doing this in other languages?

Name: Anonymous 2007-06-18 16:38 ID:Heaven

>>1
that code has a bug.
here's a fixed version:
: prime? ( n -- ? ) dup 1 > [ dup log2 2/ 1+ 2^ 2 - [ 2 + mod zero? ] contains-with? not ] [ drop f ] if ;

it'd be nice if factor had isqrt...

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List