Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Euler Project - Problem 80 : Fixes #2695 #2777

Closed
wants to merge 8 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions project_euler/problem_80/sol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import decimal
from math import floor

"""
Square root digital expansion
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.

>>> solution()
40886
"""


def solution() -> int:
"""Return the total of the digital sums of the first one hundred decimal digits for
all the irrational square roots.

>>> solution()
40886
"""

n = 100
p = 100
tot = 0

for i in range(1, n + 1):
sm = 0
decimal.getcontext().prec = p + 10
fv = decimal.Decimal(i).sqrt()
if fv - floor(fv) == 0:
continue
else:
fv = str(fv).replace(".", "")
fv = fv[0:p]
fv = int(fv)
while fv != 0:
sm = sm + (fv % 10)
fv = fv // 10
tot += sm
return tot


if __name__ == "__main__":
res = solution()
print(res)