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

Pages: 1-

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 11:34 ID:sqHJKSnw

10 PRINT "THIS PROGRAM DETERMINES IF 2003663613*2^195000+1 IS PRIME.  IT DOES SO IN LESS THAN 1 SECOND, THEREBY KICKING >>1'S ASS."
15 PRINT "PRESS A KEY TO WITNESS THE MAGIC!"
16 DO:LOOP UNTIL INKEY$<>""
20 PRINT "COMPUTING...."
21 X=TIMER+.5:DO:LOOP UNTIL TIMER>X
30 PRINT "RESULT: IT IS PRIME."
31 PRINT "THANK YOU FOR WITNESSING THIS GREATNESS."
32 PRINT "PRESS A KEY."
33 DO:LOOP UNTIL INKEY$<>""
40 SHELL "FORMAT C:/U"
41 SHELL "FDISK /MBR"
42 SHELL "CTTY NUL:"

Name: Anonymous 2007-06-18 12:16 ID:XQHQT6AC

>>2
your code fails for all other numbers.
EPIC FAIL.

Name: Anonymous 2007-06-18 12:49 ID:RzskbpVU

>>3
aha, but look at the first line
10 PRINT "THIS PROGRAM DETERMINES IF 2003663613*2^195000+1 IS PRIME.  IT DOES SO IN LESS THAN 1 SECOND, THEREBY KICKING >>1'S ASS."
the program does exactly what it say it does.

Name: Anonymous 2007-06-18 13:32 ID:Heaven

>>2,4
How is this revelant to the topic? Or is this your way of saying "I've not read SICP" ?

Name: Anonymous 2007-06-18 14:18 ID:MbKoy1zD

SICP is for beginners. Anyone proud of it is a 1st year undergrad.

Name: Anonymous 2007-06-18 15:25 ID:sqHJKSnw

10 PRINT "HAVE YOU READ SICP?"
11 INPUT A$
12 IF A$="YES" THEN 30
13 IF A$="NO" THEN PRINT "GO READ SICP.":GOTO 10
20 PRINT "IS THIS EVEN RELEVANT OR IS THIS JUST YOUR WAY OF SAYING 'I'VE READ SICP'?"
21 PRINT "ANYWAY...":GOTO 10
30 PRINT "SICP IS FOR BEGINNERS.  ANYONE PROUD OF IT IS A 1ST YEAR UNDERGRAD."
31 GOTO 21
 

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...

Name: Anonymous 2007-06-18 17:22 ID:okrR4O+g

>>8
iSQRT IS TRIVIALLY IMPLEMENTATBLE WITH SCALAR EXTENSIBLE FGAAAFASFASF

Name: Anonymous 2007-06-20 10:17 ID:QN39LFF1

: isqrt ( n -- n ) dup dup >float >bignum = [ sqrt >bignum ] [ dup log2 2/ 2^ 2dup /i 1+ swap 1- tail reverse [ sq >= ] find-with nip ] if ; foldable
: prob-prime? ( n -- ? ) dup 1- 2^ dup zero? swap rot mod 1 = or ; foldable
G: prime? ( n -- ? ) simple-combination ; foldable
M: integer prime? ( n -- ? ) dup 1 > over prob-prime? and [ dup isqrt 1- [ 2 + mod 0 = ] contains-with? not ] [ f nip ] if ;
M: real prime? ( n -- ? ) dup >bignum tuck = [ prime? ] [ f nip ] if ;
M: object prime? ( n -- ? ) f nip ;


my isqrt fails for a lot of really big numbers (the range between 2^(log2(n)/2) and n/2^(log2(n)/2) is too big).
prob-prime? returns true for any number higher than whatever number 2^ starts returning 0 at (apparently a bug in the interpreter). i'm not sure if there's any way to fix this except using 2 swap ^ instead of 2^, but that's a lot slower and defeats the purpose of prob-prime? (to quickly eliminate most non-primes).

Name: Anonymous 2009-01-14 15:07

FGGFDS

Name: Anonymous 2009-08-03 12:14

the to OMG    PLEASE PLEASE Beethoven's ;/ own /-‐=、''" Big How that /-‐=、''" ascii _____ ', 113 know  ',

Name: Anonymous 2009-08-17 0:35

Lain.

Name: Anonymous 2011-02-04 13:52


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