Pithon (Pi calculation)
ebest (574)

I found this code to calculate pi on the web using python:
https://repl.it/@ebest/pi
I want to add a percentage which says how complete the calculator is with calculating. I'm not that good with math, so how would I add the percentage?
I'm using this to find 2 million digits of pi. Gonna be fun.
Thanks!

  • Ebest
You are viewing a single comment. View All
PYer (2380)

The algorithm to find the percent of something is the part divide by whole. percent = p/w * 100
In this case the whole would be decimal.getcontext().prec, and the part would be len(str(s).replace('0', ''))-2. So to write the full algorithm, the percent is:

whole = decimal.getcontext().prec
part = len(str(s).replace('0', ''))-2
percent = (part / whole) * 100

Natrually, this would look kind of weird, since it calculates the amount of digits solved, and the amount remaining. When adding these lines in the while loop:

whole = decimal.getcontext().prec
part = len(str(s).replace('0', ''))-2
print(part/whole*100)
replit.clear()

You will notice that the percent sometimes goes backwards. It zooms almost instantly to 99%, and than goes slightly above and slightly below. I think, this is due to you algorithm for solving pi. The percent shows the percent of how many digits are solved, not how complete it is. I have no idea how you would calculate the percent of it being finished, unless you could find some way of calculating the time left and the time remaining, in which case you would do percent = time_left / time_remaining * 100. The () aren't needed because python follows the right -> left rule in PEMDAS (GEMDAS). Hopefully this is helpful, and contact me with a ping or dm if you need any more help!