正确计算平方根数字展开
Correctly compute square root digital expansions
Project Euler's problem number 80 读作:
It is well known that if the square root of a natural number is not an
integer, then it is irrational. The decimal expansion of such square
roots is infinite without any repeating pattern at all.
The square root of two is 1.41421356237309504880...
, and the digital
sum of the first one hundred decimal digits is 475
.
For the first one hundred natural numbers, find the total of the
digital sums of the first one hundred decimal digits for all the
irrational square roots.
这是我为这个问题生成的代码:
from decimal import *
from math import sqrt
getcontext().prec = 100
def digitalsum(n):
sum = 0
for a in n:
sum += int(a)
return sum
total = 0
for a in range(1, 101):
if not sqrt(a) % 1 == 0:
ans = str(Decimal(a).sqrt())
ans = ans[2::]
print(a)
print(digitalsum(ans))
print("-------")
total += digitalsum(ans)
print(total)
它显示了错误的答案,我想我在这个过程中漏掉了一些东西。感谢任何形式的帮助。
问题不是说要忽略数字的整数部分。
通过指定 100 位十进制数字的精度仅意味着在计算过程中将使用 100 位数字 而不是 您将获得前 100 位精确的小数数字。只需提高精度以确保计算产生至少 100 个正确数字:
getcontext().prec = 102
使用101
不足以得到正确答案。
此外,您还必须正确获取小数位数:
ans = str(Decimal(a).sqrt()).replace('.', '')[:100]
最后前 100 个自然数从 0
到 99
包含 not 从 1
到 100
包括在内。
因此您的代码将变为:
from decimal import *
from math import sqrt
getcontext().prec = 102
def digitalsum(n):
sum = 0
for a in n:
sum += int(a)
return sum
total = 0
for a in range(100):
if not sqrt(a) % 1 == 0:
ans = str(Decimal(a).sqrt()).replace('.', '')[:100]
print(a)
print(digitalsum(ans))
print("-------")
这会得出正确答案。
代码可以大大改进和缩短:
from __future__ import print_function #for python2 compatibility.
from math import sqrt
from decimal import Decimal, getcontext
getcontext().prec = 102
total = 0
for a in range(100):
if not sqrt(a) % 1 == 0:
ans = str(Decimal(a).sqrt()).replace('.', '')[:100]
digits = map(int, ans)
print(a, sum(digits), "--------", sep='\n')
total += sum(digits)
print(total)
Project Euler's problem number 80 读作:
It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.
The square root of two is
1.41421356237309504880...
, and the digital sum of the first one hundred decimal digits is475
.For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.
这是我为这个问题生成的代码:
from decimal import *
from math import sqrt
getcontext().prec = 100
def digitalsum(n):
sum = 0
for a in n:
sum += int(a)
return sum
total = 0
for a in range(1, 101):
if not sqrt(a) % 1 == 0:
ans = str(Decimal(a).sqrt())
ans = ans[2::]
print(a)
print(digitalsum(ans))
print("-------")
total += digitalsum(ans)
print(total)
它显示了错误的答案,我想我在这个过程中漏掉了一些东西。感谢任何形式的帮助。
问题不是说要忽略数字的整数部分。
通过指定 100 位十进制数字的精度仅意味着在计算过程中将使用 100 位数字 而不是 您将获得前 100 位精确的小数数字。只需提高精度以确保计算产生至少 100 个正确数字:
getcontext().prec = 102
使用
101
不足以得到正确答案。此外,您还必须正确获取小数位数:
ans = str(Decimal(a).sqrt()).replace('.', '')[:100]
最后前 100 个自然数从
0
到99
包含 not 从1
到100
包括在内。
因此您的代码将变为:
from decimal import *
from math import sqrt
getcontext().prec = 102
def digitalsum(n):
sum = 0
for a in n:
sum += int(a)
return sum
total = 0
for a in range(100):
if not sqrt(a) % 1 == 0:
ans = str(Decimal(a).sqrt()).replace('.', '')[:100]
print(a)
print(digitalsum(ans))
print("-------")
这会得出正确答案。
代码可以大大改进和缩短:
from __future__ import print_function #for python2 compatibility.
from math import sqrt
from decimal import Decimal, getcontext
getcontext().prec = 102
total = 0
for a in range(100):
if not sqrt(a) % 1 == 0:
ans = str(Decimal(a).sqrt()).replace('.', '')[:100]
digits = map(int, ans)
print(a, sum(digits), "--------", sep='\n')
total += sum(digits)
print(total)