4 ms·
There are simple math formulas that approximate pi. You can look one up and run it to the right number of digits. It would be hard without Google, but most prog
by computerphage 8y ago
There are simple math formulas that approximate pi. You can look one up and run it to the right number of digits. It would be hard without Google, but most programmers should be able to do it with Google so that they don't need to figure out any of the math.
- leni536 8y agoYou need to use an arbitrary precision math library, I don't know how hard to use those. Those formulas have a hard limit in precision for standard floating point types.
- dhartmei 8y agoYou don't need arbitrary precision, use a spigot https://en.wikipedia.org/wiki/Pi#Spigot_algorithms https://en.wikipedia.org/wiki/Pi#Spigot_algorithms
- ajuc 8y agoWow, this blew my mind. And it's so simple, too. def nthHexDigitOfPI(n): return 4/(8*n+1) - 2/(8*n+4) - 1/(8*n+5) - 1/(8*n+6)
- leni536 8y agoI took a look at a spigot algorithm[1]. I still think it needs arbitrary precision arithmetics but maybe I miss something obvious. [1] https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula#BBP_digit-extraction_algorithm_for_%CF%80 https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%9...
- dhartmei 8y agoHere's an example if you want to try http://www.benzedrine.ch/pi-spigot.c http://www.benzedrine.ch/pi-spigot.c
- leni536 8y agoThis doesn't look like a spigot algorithm. It has a link in the comments[1], it calculates the digits of PI from the beginning. The code you linked seems to implement the exact same algorithm. I don't fully comprehend the code still, but the SCALE parameter suggest that it handles the elements of the array as a fixed point representation of real numbers. I wonder if it's correct for large number of digits. [1] http://www.codecodex.com/wiki/Calculate_digits_of_pi#C http://www.codecodex.com/wiki/Calculate_digits_of_pi#C
- deleted 8y ago[deleted]