Advertisement
CaptainSpaceCat

Prime Calculator

Oct 3rd, 2015
141
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function scanNumber(n)
  2.     for i = 2, math.floor(n/2) + 1 do
  3.         if n % i == 0 then
  4.             return false
  5.         end
  6.     end
  7.     return true
  8. end
  9.  
  10. function getCurrentNumber()
  11.     local num = 3
  12.     nFile = fs.open("primeIndex", "r")
  13.     if nFile then
  14.         num = tonumber(nFile.readAll())
  15.     end
  16.     nFile.close()
  17.     return num
  18. end
  19.  
  20. function saveCurrentNumber(num)
  21.     nFile = fs.open("primeIndex", "w")
  22.     nFile.write(num)
  23.     nFile.close()
  24. end
  25.  
  26. function addPrime(prime)
  27.     pFile = fs.open("primeList", "a")
  28.     pFile.writeLine(prime)
  29.     pFile.close()
  30. end
  31.  
  32. local number = getCurrentNumber()
  33. while true do
  34.     saveCurrentNumber()
  35.     if scanNumber(number) then
  36.         addPrime(number)
  37.         print(number)
  38.     end
  39.     number = number + 1
  40.     if number % 50 == 0 then
  41.         sleep(.01)
  42.     end
  43. end
Advertisement
RAW Paste Data Copied
Advertisement