Skip to content

Commit c07b82f

Browse files
sandeepgupta007github-actionsdhruvmanila
authored
Add Project Euler Problem 80 (TheAlgorithms#2885)
* adding solution to problem 80 * updating DIRECTORY.md * fixing spell check * updating sol as per comments * Add reference link to the problem Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Dhruv <[email protected]>
1 parent 3324bbb commit c07b82f

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@
662662
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_71/sol1.py)
663663
* Problem 76
664664
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py)
665+
* Problem 80
666+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_80/sol1.py)
665667
* Problem 97
666668
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_97/sol1.py)
667669
* Problem 99

project_euler/problem_80/__init__.py

Whitespace-only changes.

project_euler/problem_80/sol1.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Project Euler Problem 80: https://projecteuler.net/problem=80
3+
Author: Sandeep Gupta
4+
Problem statement: For the first one hundred natural numbers, find the total of
5+
the digital sums of the first one hundred decimal digits for all the irrational
6+
square roots.
7+
Time: 5 October 2020, 18:30
8+
"""
9+
import decimal
10+
11+
12+
def solution() -> int:
13+
"""
14+
To evaluate the sum, Used decimal python module to calculate the decimal
15+
places up to 100, the most important thing would be take calculate
16+
a few extra places for decimal otherwise there will be rounding
17+
error.
18+
19+
>>> solution()
20+
40886
21+
"""
22+
answer = 0
23+
decimal_context = decimal.Context(prec=105)
24+
for i in range(2, 100):
25+
number = decimal.Decimal(i)
26+
sqrt_number = number.sqrt(decimal_context)
27+
if len(str(sqrt_number)) > 1:
28+
answer += int(str(sqrt_number)[0])
29+
sqrt_number = str(sqrt_number)[2:101]
30+
answer += sum([int(x) for x in sqrt_number])
31+
return answer
32+
33+
34+
if __name__ == "__main__":
35+
import doctest
36+
37+
doctest.testmod()

0 commit comments

Comments
 (0)