From ad21975cfc32aeb1e8a967e1db1360a10a356e0e Mon Sep 17 00:00:00 2001 From: Srishtik2310 Date: Thu, 7 Oct 2021 02:15:42 +0530 Subject: [PATCH 1/3] adde solution to problem 092 --- project_euler/problem_092/__init__.py | 0 project_euler/problem_092/sol1.py | 96 +++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 project_euler/problem_092/__init__.py create mode 100755 project_euler/problem_092/sol1.py diff --git a/project_euler/problem_092/__init__.py b/project_euler/problem_092/__init__.py new file mode 100755 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py new file mode 100755 index 000000000000..7b0ba593e65f --- /dev/null +++ b/project_euler/problem_092/sol1.py @@ -0,0 +1,96 @@ +""" +Project Euler Problem 092: https://projecteuler.net/problem=92 +Square digit chains +A number chain is created by continuously adding the square of the digits in +a number to form a new number until it has been seen before. + +For example, + +44 → 32 → 13 → 10 → 1 → 1 +85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 + +Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. +What is most amazing is that EVERY starting number will eventually arrive at 1 or 89. + +How many starting numbers below ten million will arrive at 89? +""" + + +def next_number(n: int) -> int: + """ + Returns the next number of the chain by adding the square of each digit + to form a neww number. + For example if n = 12, next_number() will return 1^2 + 2^2 = 5. + Therefore 5 is the next number of the chain. + >>> next_number(44) + 32 + >>> next_number(10) + 1 + >>> next_number(32) + 13 + """ + num = 0 + for i in range(len(str(n))): + num += int(str(n)[i]) ** 2 + + return num + + +def chain(n: int) -> bool: + """ + Generates the chain of numbers until the nest number generated is 1 0r 89. + for example, if starting number is 44, then the function generates the + following chain of numbers. + chain: 44 → 32 → 13 → 10 → 1 → 1 + once the next number generated is 1 or 89, the function + Returns True if the next number generated by next_number() if 1. + Returns False if the next number generated by next_number() is 89. + + >>> chain(10) + True + >>> chain(58) + False + >>> chain(1) + True + """ + while n != 1 and n != 89: + n = next_number(n) + + if n == 1: + return True + + elif n == 89: + return False + + +def solution(n: int = 10000000) -> int: + """ + The function returns the total numbers that end up in 89 after the chain generation. + The function accepts a range n and the function checks all the values under value n, + if the chain generation leads to the end number as 1 or 89. If the chain() + returns True, then total is incremented, implying that the number we + started with ended up with 1 else total2 is incremented, implying that + the number we started with ended up in 89 after chain generation. + But the function returns total2 as the requirement of question is + to find out how many ended up in 89. + >>> solution(100) + 80 + >>> solution(10000000) + 8581146 + """ + total = 0 + total2 = 0 + for i in range(1, n): + val = chain(i) + + if val is True: + total += 1 + + elif val is False: + total2 += 1 + + return total2 + + +if __name__ == "__main__": + print(f"{solution() = }") From bdef112f254ab17e841bf2396586e7d44496b526 Mon Sep 17 00:00:00 2001 From: Srishtik2310 Date: Thu, 7 Oct 2021 02:41:11 +0530 Subject: [PATCH 2/3] added solution to problem 092 --- project_euler/problem_092/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index 7b0ba593e65f..c421a95b3c29 100755 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -5,7 +5,6 @@ a number to form a new number until it has been seen before. For example, - 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 From 32a0b7fa301786c73607d38ab4419ce3cdd09245 Mon Sep 17 00:00:00 2001 From: Srishtik2310 Date: Fri, 8 Oct 2021 19:56:11 +0530 Subject: [PATCH 3/3] fixed the pre-comit shebang issue --- project_euler/problem_092/__init__.py | 0 project_euler/problem_092/sol1.py | 30 +++++++++++++-------------- 2 files changed, 14 insertions(+), 16 deletions(-) mode change 100755 => 100644 project_euler/problem_092/__init__.py mode change 100755 => 100644 project_euler/problem_092/sol1.py diff --git a/project_euler/problem_092/__init__.py b/project_euler/problem_092/__init__.py old mode 100755 new mode 100644 diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py old mode 100755 new mode 100644 index c421a95b3c29..a02629a7bc7d --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -3,23 +3,20 @@ Square digit chains A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. - For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 - Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89. - How many starting numbers below ten million will arrive at 89? """ -def next_number(n: int) -> int: +def next_number(number: int) -> int: """ Returns the next number of the chain by adding the square of each digit to form a neww number. - For example if n = 12, next_number() will return 1^2 + 2^2 = 5. + For example if number = 12, next_number() will return 1^2 + 2^2 = 5. Therefore 5 is the next number of the chain. >>> next_number(44) 32 @@ -29,13 +26,13 @@ def next_number(n: int) -> int: 13 """ num = 0 - for i in range(len(str(n))): - num += int(str(n)[i]) ** 2 + for i in range(len(str(number))): + num += int(str(number)[i]) ** 2 return num -def chain(n: int) -> bool: +def chain(number: int) -> bool: """ Generates the chain of numbers until the nest number generated is 1 0r 89. for example, if starting number is 44, then the function generates the @@ -44,7 +41,6 @@ def chain(n: int) -> bool: once the next number generated is 1 or 89, the function Returns True if the next number generated by next_number() if 1. Returns False if the next number generated by next_number() is 89. - >>> chain(10) True >>> chain(58) @@ -52,26 +48,28 @@ def chain(n: int) -> bool: >>> chain(1) True """ - while n != 1 and n != 89: - n = next_number(n) + while number != 1 and number != 89: + number = next_number(number) - if n == 1: + if number == 1: return True - elif n == 89: + elif number == 89: return False -def solution(n: int = 10000000) -> int: +def solution(number: int = 10000000) -> int: """ The function returns the total numbers that end up in 89 after the chain generation. - The function accepts a range n and the function checks all the values under value n, + The function accepts a range number and the function checks all the values + under value number. if the chain generation leads to the end number as 1 or 89. If the chain() returns True, then total is incremented, implying that the number we started with ended up with 1 else total2 is incremented, implying that the number we started with ended up in 89 after chain generation. But the function returns total2 as the requirement of question is to find out how many ended up in 89. + >>> solution(100) 80 >>> solution(10000000) @@ -79,7 +77,7 @@ def solution(n: int = 10000000) -> int: """ total = 0 total2 = 0 - for i in range(1, n): + for i in range(1, number): val = chain(i) if val is True: