mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Add Project Euler Problem 80 (#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 <dhruvmanila@gmail.com>
This commit is contained in:
parent
3324bbb94b
commit
c07b82fa63
@ -662,6 +662,8 @@
|
||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_71/sol1.py)
|
||||
* Problem 76
|
||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py)
|
||||
* Problem 80
|
||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_80/sol1.py)
|
||||
* Problem 97
|
||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_97/sol1.py)
|
||||
* Problem 99
|
||||
|
0
project_euler/problem_80/__init__.py
Normal file
0
project_euler/problem_80/__init__.py
Normal file
37
project_euler/problem_80/sol1.py
Normal file
37
project_euler/problem_80/sol1.py
Normal file
@ -0,0 +1,37 @@
|
||||
"""
|
||||
Project Euler Problem 80: https://projecteuler.net/problem=80
|
||||
Author: Sandeep Gupta
|
||||
Problem statement: 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.
|
||||
Time: 5 October 2020, 18:30
|
||||
"""
|
||||
import decimal
|
||||
|
||||
|
||||
def solution() -> int:
|
||||
"""
|
||||
To evaluate the sum, Used decimal python module to calculate the decimal
|
||||
places up to 100, the most important thing would be take calculate
|
||||
a few extra places for decimal otherwise there will be rounding
|
||||
error.
|
||||
|
||||
>>> solution()
|
||||
40886
|
||||
"""
|
||||
answer = 0
|
||||
decimal_context = decimal.Context(prec=105)
|
||||
for i in range(2, 100):
|
||||
number = decimal.Decimal(i)
|
||||
sqrt_number = number.sqrt(decimal_context)
|
||||
if len(str(sqrt_number)) > 1:
|
||||
answer += int(str(sqrt_number)[0])
|
||||
sqrt_number = str(sqrt_number)[2:101]
|
||||
answer += sum([int(x) for x in sqrt_number])
|
||||
return answer
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user