Guest User

Pi Calculator

a guest
Mar 15th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. --Made by TurtleHunter
  2.  
  3. --bigInt from http://www.computercraft.info/forums2/index.php?/topic/12137-bigint-arbitrary-precision-unsigned-integers/ KillaVanilla
  4.  
  5. local yieldTime
  6. local function yield() --http://www.computercraft.info/forums2/index.php?/topic/11245-avoiding-too-long-without-yielding-the-efficient-way modified to sleep(so you can see the result) instead of pulling a event
  7. if yieldTime then
  8. if os.clock() - yieldTime > 2 then
  9. print("Yielding...")
  10. sleep(2)
  11. yieldTime = nil
  12. end
  13. else
  14. yieldTime = os.clock() -- store the time
  15. end
  16. end
  17. function download(name, url)
  18. term.write("Downloading "..name)
  19. local sResponse = http.get(url)
  20. if fs.exists(name) then error("Download: File exist", 0) end
  21. local file = fs.open(name, "w")
  22. file.write(sResponse.readAll())
  23. sResponse.close()
  24. file.close()
  25. term.write(" Done")
  26. end
  27.  
  28. if not fs.exists("bigInt") then download("bigInt", "http://www.pastebin.com/raw.php?i=th3Vrppb") end
  29.  
  30. os.loadAPI("bigInt")
  31.  
  32. function pi(precision) --localized :P ported to cclua and bigInt from http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=16546
  33. local pival = 1
  34. local isNegative = 1
  35. local k = 0
  36. local calc = 3
  37. while k < precision do
  38. pival = bigInt.mul(bigInt.sub(pival, bigInt.div(1, calc)), isNegative)
  39. calc = bigInt.add(calc, 2)
  40. isNegative = -isNegative
  41. k = k+1
  42. if k==precision then
  43. term.clear()
  44. term.setCursorPos(1,1)
  45. print("Finished")
  46. print(bigInt.toStr(bigInt.mul(pival, 4)))
  47. else
  48. term.clear()
  49. term.setCursorPos(1,1)
  50. print(bigInt.toStr(bigInt.mul(pival, 4)))
  51. end
  52. yield()
  53. end
  54. end
  55.  
  56. print("Precision(How many times to run): ")
  57. local a = read()
  58. pi(tonumber(a))
Advertisement
Add Comment
Please, Sign In to add comment