Advertisement
Ham62

Primefinder.bas

Mar 17th, 2019
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const INITIAL_PRIMES = 2000 ' ~4kb initial buffer size
  2. const MAX_NUMBER = 1000
  3. dim as integer iTotalPrimes = 1 ' number of primes found
  4. redim as integer iPrimes(INITIAL_PRIMES) ' list of primes
  5.  
  6. dim as double iStartTime, iEndTime
  7. iPrimes(0) = 2 ' first prime is 2
  8.  
  9. iStartTime = timer
  10. dim as integer iNum = 1 ' Starts counting at 3 (1+2)
  11. while iNum < MAX_NUMBER
  12.     iNum += 2
  13.     for n as integer = 0 to iTotalPrimes-1
  14.         if (iNum MOD iPrimes(n)) = 0 then continue while ' not prime
  15.        
  16.         if iPrimes(n) >= sqr(iNum) then
  17.             'print "prime!", iNum
  18.             iPrimes(iTotalPrimes) = iNum
  19.             iTotalPrimes += 1
  20.             if iTotalPrimes > ubound(iPrimes) then
  21.                 redim preserve iPrimes(ubound(iPrimes) + INITIAL_PRIMES)
  22.             end if
  23.             continue while
  24.         end if
  25.     next n
  26. wend
  27. iEndTime = timer
  28.  
  29.  
  30. print str(iTotalPrimes)+" primes found in "+str(iEndTime-iStartTime)+" seconds"
  31. print "Press any key to list primes..."
  32. sleep
  33. open "primes.txt" for output as #1
  34. for i as integer = 0 to iTotalPrimes-1
  35.     print #1, iPrimes(i);
  36. next i
  37. close #1
  38.  
  39. sleep
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement