Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Made by TurtleHunter
- --bigInt from http://www.computercraft.info/forums2/index.php?/topic/12137-bigint-arbitrary-precision-unsigned-integers/ KillaVanilla
- local yieldTime
- 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
- if yieldTime then
- if os.clock() - yieldTime > 2 then
- print("Yielding...")
- sleep(2)
- yieldTime = nil
- end
- else
- yieldTime = os.clock() -- store the time
- end
- end
- function download(name, url)
- term.write("Downloading "..name)
- local sResponse = http.get(url)
- if fs.exists(name) then error("Download: File exist", 0) end
- local file = fs.open(name, "w")
- file.write(sResponse.readAll())
- sResponse.close()
- file.close()
- term.write(" Done")
- end
- if not fs.exists("bigInt") then download("bigInt", "http://www.pastebin.com/raw.php?i=th3Vrppb") end
- os.loadAPI("bigInt")
- function pi(precision) --localized :P ported to cclua and bigInt from http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=16546
- local pival = 1
- local isNegative = 1
- local k = 0
- local calc = 3
- while k < precision do
- pival = bigInt.mul(bigInt.sub(pival, bigInt.div(1, calc)), isNegative)
- calc = bigInt.add(calc, 2)
- isNegative = -isNegative
- k = k+1
- if k==precision then
- term.clear()
- term.setCursorPos(1,1)
- print("Finished")
- print(bigInt.toStr(bigInt.mul(pival, 4)))
- else
- term.clear()
- term.setCursorPos(1,1)
- print(bigInt.toStr(bigInt.mul(pival, 4)))
- end
- yield()
- end
- end
- print("Precision(How many times to run): ")
- local a = read()
- pi(tonumber(a))
Advertisement
Add Comment
Please, Sign In to add comment