r/dailyprogrammer • u/oskar_s • Jul 30 '12
[7/30/2012] Challenge #83 [difficult] (Digits of the square-root of 2)
The square-root of 2 is, as Hippasus of Metapontum discovered to his sorrow, irrational. Among other things, this means that its decimal expansion goes on forever and never repeats.
Here, for instance, is the first 100000 digits of the square-root of 2.
Except that it's not!
I, evil genius that I am, have changed exactly one of those 100000 digits to something else, so that it is slightly wrong. Write a program that finds what digit I changed, what I changed it from and what I changed it to.
Now, there are a number of places online where you can get a gigantic decimal expansion of sqrt(2), and the easiest way to solve this problem would be to simply load one of those files in as a string and compare it to this file, and the number would pop right out. But the point of this challenge is to try and do it with math, not the internet, so that solution is prohibited!
- Thanks to MmmVomit for suggesting (a version of) this problem at /r/dailyprogrammer_ideas! If you have a problem that you think would be good for us, head on over there and suggest it!
4
u/andkerosine Jul 30 '12
Bash and
bc:I realize it's not exactly in the spirit of the thing, but I also very much doubt that anybody's going to be rolling their own bignum library to solve this challenge. Given the conspicuous offset of the modification, though, I imagine I've overlooked something that would have sped the calculation up significantly. Then again, Newton's method was used to calculate the current record, so I'm pretty eager to see what sorts of tricks might be applied to this problem.
4
u/[deleted] Jul 30 '12 edited Jul 30 '12
Java in the spirit of the problem. Takes about 12 seconds to compute the sqrt to the required precision.
Output
EDIT: Accidentally counted the decimal point itself because I am a scrub.
3
u/Cosmologicon 2 3 Jul 30 '12
I think I have a python solution in the spirit of the problem. No bignums, purely integer math, O(1) storage beyond storing the input in a list. It's pretty slow, and it's not perfect - it probably will fail if the changed digit is too close to the beginning or the end.
Result:
2
u/skeeto -9 8 Aug 01 '12 edited Aug 01 '12
Wow, this is really difficult to do without an arbitrary-precision library. Here's my attempt in pure ANSI C, no libraries,
https://gist.github.com/3221201
I got most of the way there in just 123 LOC, but not quite. I can compute to 100,000 digits of accuracy but I can't figure out how to actually output the result in decimal form. I'm just left with a giant numerator and denominator.
1
u/verhoevenv Jul 30 '12 edited Jul 30 '12
With Python Decimal (which might or might not be cheating):
Running time about a minute.
*EDIT: on review, the last line should probably be
1
u/push_ecx_0x00 Jul 31 '12 edited Jul 31 '12
Here's a simple (but slow) solution in ruby using only built-in things. The precision from the call to sqrt isn't really accurate (it generates over 100000 digits), but the incorrect digit is found before that.
Runs for about 15 seconds total. I'm guessing that there is an actual expression that converges to sqrt(2) and can be computed quickly (at the very least, quicker than this).
1
u/Ledrug 0 2 Aug 04 '12
Using GMP:
result: