Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const INITIAL_PRIMES = 2000 ' ~4kb initial buffer size
- const MAX_NUMBER = 1000
- dim as integer iTotalPrimes = 1 ' number of primes found
- redim as integer iPrimes(INITIAL_PRIMES) ' list of primes
- dim as double iStartTime, iEndTime
- iPrimes(0) = 2 ' first prime is 2
- iStartTime = timer
- dim as integer iNum = 1 ' Starts counting at 3 (1+2)
- while iNum < MAX_NUMBER
- iNum += 2
- for n as integer = 0 to iTotalPrimes-1
- if (iNum MOD iPrimes(n)) = 0 then continue while ' not prime
- if iPrimes(n) >= sqr(iNum) then
- 'print "prime!", iNum
- iPrimes(iTotalPrimes) = iNum
- iTotalPrimes += 1
- if iTotalPrimes > ubound(iPrimes) then
- redim preserve iPrimes(ubound(iPrimes) + INITIAL_PRIMES)
- end if
- continue while
- end if
- next n
- wend
- iEndTime = timer
- print str(iTotalPrimes)+" primes found in "+str(iEndTime-iStartTime)+" seconds"
- print "Press any key to list primes..."
- sleep
- open "primes.txt" for output as #1
- for i as integer = 0 to iTotalPrimes-1
- print #1, iPrimes(i);
- next i
- close #1
- sleep
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement