The Wayback Machine - https://web.archive.org/web/20201006212609/https://github.com/TheAlgorithms/Python/pull/2884/files
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

Project Euler problem 92 Solution #2884

Open
wants to merge 6 commits into
base: master
from
@@ -652,6 +652,8 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_67/sol1.py)
* Problem 76
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py)
* Problem 92
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_92/sol1.py)
* Problem 97
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_97/sol1.py)
* Problem 99
No changes.
@@ -0,0 +1,57 @@
"""
Euler Problem : 92
@author : sandeep gupta
@time : 5 October 2020, 18:30
@Solution: Tried doing a top bottom memoziation, to calculate the squares
at each step and run through 10 million numbers to calculate the numbers
ending at 89, returning the answer at the end.
@answer : 8581146
"""


def solution() -> int:
square_hash = {}
answer = 0
square_hash[89] = 89
square_hash[1] = 1
square = {}
square[0] = 0
square[1] = 1
square[2] = 4
square[3] = 9
square[4] = 16
square[5] = 25
square[6] = 36
square[7] = 49
square[8] = 64
square[9] = 81
for i in range(1, 10000000):
number = i
while True:
if number in square_hash:
if square_hash[number] == 89:
square_hash[i] = 89
answer += 1
break
elif square_hash[number] == 1:
square_hash[i] = 1
break
else:
number = square_hash[number]
string_number = str(number)
number = 0
for j in string_number:
number += square[int(j)]
square_hash[int(string_number)] = number
return answer


print(solution())


# Tests
if __name__ == "__main__":
import doctest

doctest.testmod()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.