mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
38
other_resources/Project-Euler/092-square_dig_chains.py
Normal file
38
other_resources/Project-Euler/092-square_dig_chains.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
def calculate_chain(n):
|
||||
n_str = str(n)
|
||||
while n_str != 1 or n_str != 89:
|
||||
n_str = str(n_str)
|
||||
sum_here = 0
|
||||
for d in n_str:
|
||||
sum_here += int(d)**2
|
||||
n_str = sum_here
|
||||
if n_str == 89:
|
||||
return 1
|
||||
if n_str == 1:
|
||||
return 0
|
||||
|
||||
|
||||
def square_dig_chains(n):
|
||||
count = 0
|
||||
for i in range(1, n+1):
|
||||
count += calculate_chain(i)
|
||||
return count
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
import time
|
||||
start = time.time()
|
||||
|
||||
print(square_dig_chains(10**7))
|
||||
|
||||
elapsed = (time.time() - start)
|
||||
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue