Square root digital expansion | Euler 80
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getRoot(n): | |
a = 5*n | |
b = 5 | |
decLen = len(str(int(n**.5))) | |
while(len(str(b))-decLen < 120): | |
if(a>b): | |
a = a-b | |
b = b+10 | |
else: | |
a = a*100 | |
b = long(str(b)[:len(str(b))-1] + "05") | |
return [b,decLen] | |
s = 0 | |
for x in range(100): | |
if x**.5==int(x**0.5): | |
continue | |
ans = getRoot(x) | |
req = str(ans[0])[:100] | |
s += sum([int(x) for x in list(req)]) | |
print s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://www.afjarvis.staff.shef.ac.uk/maths/jarvisspec02.pdf //refer to this link for the 100% accurate non floating point root calculation | |
Let a = 5n | |
(this multiplication by 5 is the only time when an operation other than addition and subtraction is involved!), and put | |
b = 5. | |
If a≥b, replace a with a−b, and add 10 to b | |
If a < b, add two zeroes to the end of a, and add a zero to b just before the final digit (which will always be ‘5’) | |
The answer will be given from the value in b, but for the real calculation of the root the number of decimal digits will have to be known which is calculated by newtons method |
Comments
Post a Comment