ffweb
New Member
Posts: 20
|
Post by ffweb on Sept 11, 2010 16:21:04 GMT 1
A Sieve of Eratosthenes for prime numbers, that I wrote before discovering that there was allready one in the examples.
Warning: it saves results in a file that is some Mb big.
'Sieve of Eratosthenes for prime numbers
OPTION BASE 1
CONST maxnum = 10000000
DECLARE number[maxnum] TYPE short
timer = NOW
'Array initialization PRINT "Initialization" FOR i = 1 TO maxnum number[i] = 1 NEXT
'Sieve PRINT "Sieve" FOR i = 2 TO maxnum/2 IF number[i] THEN numtodel = 2 * i WHILE numtodel <= maxnum number[numtodel] = 0 numtodel = numtodel + i WEND END IF NEXT
'Output PRINT "Saving primes..." OPEN "primeslist.dat" FOR WRITING AS outfile
FOR i = 2 TO maxnum IF number[i] THEN WRITELN STR$(i) TO outfile NEXT
CLOSE FILE outfile
PRINT "Execution complete"
timer = NOW - timer PRINT "Elapsed time: ", timer, " seconds"
|
|
|
Post by Pjot on Sept 12, 2010 8:24:04 GMT 1
Hi ffweb, Impressive! Interestingly, in my case saving the file takes longer than generating the prime numbers Thanks, Peter
|
|