4 ms·
Nice list, but ‘anyone’? The first on the list is ”Find PI to the Nth Digit - Enter a number and have the program generate PI up to that many decimal places. Ke
by Someone 8y ago
Nice list, but ‘anyone’? The first on the list is ”Find PI to the Nth Digit - Enter a number and have the program generate PI up to that many decimal places. Keep a limit to how far the program will go”
Easily solvable if you set that limit low and cheat. Just put a string representation of pi in the program, and chop it off at the requested length:
n = Input “num digits?”
if n between 0 and 8 inclusive
print left(“3.1415927”, n+1)
else
print “number out of supported range”
If you don’t cheat or if ”Keep a limit to how far the program will go” doesn’t mean what I read in it, I have my doubts anyone can write that program, even with support from Google.
- computerphage 8y agoThere 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]
- c22 8y agoIf you accept only a finite n this seems easily doable. You just may have to wait awhile for execution to complete.
- garmaine 8y agoThere are fast algorithms for calculating the Nth digit of pi.
- montenegrohugo 8y agoThe entire 'fun' of this is implementing it with as much of your own code as possible. Using existing fast algorithms only leaves you with splitting the output to the nth digit, which is trivial and leaves you without having learned or achieved anything imo.
- garmaine 8y agoYou can implement the algorithm yourself.
- montenegrohugo 8y agoPi was already being approximated by Archimedes and Co. some time ago, I don't think its outside of the realm of possibility that a modern developer looks up following inequality and just approximates pi to some degree of accuracy with P/(2*r) > Pi > P/(2*R) Once you have approximated it, returning a string with the first x digits is trivial
- blattimwind 8y agoSeries for approximating Pi can be found in almost every formulary.
- dahart 8y ago> I have my doubts that anyone can write that program Simon Plouffe did it, years ago, with no help from Google. http://plouffe.fr/Simon%20Plouffe.htm http://plouffe.fr/Simon%20Plouffe.htm https://en.m.wikipedia.org/wiki/Bailey–Borwein–Plouffe_formula https://en.m.wikipedia.org/wiki/Bailey–Borwein–Plouffe_formu...
- Someone 8y agoSorry, didn’t realize that “anyone” switched meaning (from “any person or people” to “a person of importance or authority” (https://en.oxforddictionaries.com/definition/anyone) https://en.oxforddictionaries.com/definition/anyone)) between the two uses. My second use should have been quoted, too, because it refers to the “that anyone can solve” in the page’s title, which (at least in my reading) can only have the first meaning.
- dahart 8y agoI’m not sure I understand what you mean. For me, your comment doesn’t hinge on the meaning of ‘anyone’ at all. You clearly implied the problem wasn’t solvable by any person, important or not, by saying “even with support from Google”. You implied you weren’t aware the solution was available with a Google search. But it was solved, first by Simon Plouffe, and then by others, so now you know. For the lay-programmer, e.g., “any person or people”, everything on that list is an exercise in learning how the problem was solved by someone else, and writing the code for it. The idea is to learn, not to invent the solution from scratch without help. The way to code the spigot algorithm for pi is to lookup the formula, understand it, figure out how to deal with precision and numeric base conversion, and write the code. Pretty much anyone can do that.
- Someone 8y ago”You implied you weren’t aware the solution was available with a Google search. But it was solved, first by Simon Plouffe, and then by others, so now you know.” Where did I imply it? The site being discussed uses “anyone” in the sense “any person”. I questioned that, but didn’t realize that I used the word in the second meaning the second time I used it. ”The way to code the spigot algorithm for pi is to lookup the formula, understand it, figure out how to deal with precision and numeric base conversion, and write the code. Pretty much anyone can do that.” That, we disagree about. We live in a world where “Fizz-Buzz” is a thing, so I’m not that optimistic that anybody can do what you describe.