From 5a67d773740a42d79fa4f27000af24dc56624563 Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Mon, 3 Oct 2022 03:01:46 +0530 Subject: [PATCH 1/5] Added explanation and increased speed of the solution of problem 092 --- project_euler/problem_092/sol1.py | 33 ++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index d326fc33fcca..d41ad4c9c77b 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -11,11 +11,11 @@ How many starting numbers below ten million will arrive at 89? """ - -DIGITS_SQUARED = [digit**2 for digit in range(10)] +DIGITS_SQUARED = [sum(int(c) ** 2 for c in str(i)) for i in range(1000)] def next_number(number: int) -> int: + """ Returns the next number of the chain by adding the square of each digit to form a new number. @@ -28,15 +28,29 @@ def next_number(number: int) -> int: >>> next_number(32) 13 """ + sum_of_digits_squared = 0 while number: - sum_of_digits_squared += DIGITS_SQUARED[number % 10] - number //= 10 + + # Increased Speed Slightly by checking every 3 digits together. + sum_of_digits_squared += DIGITS_SQUARED[number % 1000] + number //= 1000 return sum_of_digits_squared -CHAINS = {1: True, 58: False} +# There are 2 Chains made, +# One ends with 89 with the chain member 58 being the one which when declared first, +# there will be the least number of iterations for all the members to be checked. + +# The other one ends with 1 and has only one element 1. + +# So 58 and 1 are chosen to be declared at the starting. + +# Changed dictionary to an array to quicken the solution +CHAINS: list[bool | None] = [None] * 10000000 +CHAINS[0] = True +CHAINS[57] = False def chain(number: int) -> bool: @@ -54,11 +68,12 @@ def chain(number: int) -> bool: >>> chain(1) True """ - if number in CHAINS: - return CHAINS[number] + + if CHAINS[number - 1] is not None: + return CHAINS[number - 1] # type: ignore number_chain = chain(next_number(number)) - CHAINS[number] = number_chain + CHAINS[number - 1] = number_chain return number_chain @@ -74,6 +89,7 @@ def solution(number: int = 10000000) -> int: >>> solution(10000000) 8581146 """ + return sum(1 for i in range(1, number) if not chain(i)) @@ -81,5 +97,4 @@ def solution(number: int = 10000000) -> int: import doctest doctest.testmod() - print(f"{solution() = }") From ad6276e23eaef17f4312c734abae6c1e9b9c994f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 2 Oct 2022 21:32:28 +0000 Subject: [PATCH 2/5] updating DIRECTORY.md --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1d9e6eff75c6..64e9d5333a2f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -9,6 +9,7 @@ * [Newton Forward Interpolation](arithmetic_analysis/newton_forward_interpolation.py) * [Newton Method](arithmetic_analysis/newton_method.py) * [Newton Raphson](arithmetic_analysis/newton_raphson.py) + * [Newton Raphson New](arithmetic_analysis/newton_raphson_new.py) * [Secant Method](arithmetic_analysis/secant_method.py) ## Audio Filters @@ -107,6 +108,7 @@ * [Lempel Ziv](compression/lempel_ziv.py) * [Lempel Ziv Decompress](compression/lempel_ziv_decompress.py) * [Peak Signal To Noise Ratio](compression/peak_signal_to_noise_ratio.py) + * [Run Length Encoding](compression/run_length_encoding.py) ## Computer Vision * [Cnn Classification](computer_vision/cnn_classification.py) @@ -621,6 +623,7 @@ * [Linear Congruential Generator](other/linear_congruential_generator.py) * [Lru Cache](other/lru_cache.py) * [Magicdiamondpattern](other/magicdiamondpattern.py) + * [Maximum Subarray](other/maximum_subarray.py) * [Nested Brackets](other/nested_brackets.py) * [Password Generator](other/password_generator.py) * [Scoring Algorithm](other/scoring_algorithm.py) @@ -1053,6 +1056,7 @@ * [Fetch Bbc News](web_programming/fetch_bbc_news.py) * [Fetch Github Info](web_programming/fetch_github_info.py) * [Fetch Jobs](web_programming/fetch_jobs.py) + * [Fetch Quotes](web_programming/fetch_quotes.py) * [Fetch Well Rx Price](web_programming/fetch_well_rx_price.py) * [Get Imdb Top 250 Movies Csv](web_programming/get_imdb_top_250_movies_csv.py) * [Get Imdbtop](web_programming/get_imdbtop.py) From 67e25f635b60623231ffbb4d79d9a39bb7a1302c Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Tue, 4 Oct 2022 00:32:40 +0530 Subject: [PATCH 3/5] Added temporary fix to the failing of problem 104 --- project_euler/problem_104/sol.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 0818ac401c3a..10eb6d6e55f3 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -13,6 +13,10 @@ the last nine digits are 1-9 pandigital, find k. """ +import sys + +sys.set_int_max_str_digits(0) # type: ignore + def check(number: int) -> bool: """ From f8889147cad8dc2aa32d178d54ec10b4a0172972 Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Wed, 5 Oct 2022 16:32:54 +0530 Subject: [PATCH 4/5] Reduced few seconds by minor improvements --- project_euler/problem_092/sol1.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index d41ad4c9c77b..33a6c06946f7 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -11,7 +11,7 @@ How many starting numbers below ten million will arrive at 89? """ -DIGITS_SQUARED = [sum(int(c) ** 2 for c in str(i)) for i in range(1000)] +DIGITS_SQUARED = [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(100000)] def next_number(number: int) -> int: @@ -32,9 +32,9 @@ def next_number(number: int) -> int: sum_of_digits_squared = 0 while number: - # Increased Speed Slightly by checking every 3 digits together. - sum_of_digits_squared += DIGITS_SQUARED[number % 1000] - number //= 1000 + # Increased Speed Slightly by checking every 5 digits together. + sum_of_digits_squared += DIGITS_SQUARED[number % 100000] + number //= 100000 return sum_of_digits_squared @@ -75,6 +75,10 @@ def chain(number: int) -> bool: number_chain = chain(next_number(number)) CHAINS[number - 1] = number_chain + while number < 10000000: + CHAINS[number - 1] = number_chain + number *= 10 + return number_chain @@ -89,8 +93,11 @@ def solution(number: int = 10000000) -> int: >>> solution(10000000) 8581146 """ + for i in range(1, number): + if CHAINS[i] is None: + chain(i + 1) - return sum(1 for i in range(1, number) if not chain(i)) + return CHAINS[:number].count(False) if __name__ == "__main__": From dc1a99ce929e60f8520e5465182757993f116742 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 11:00:39 +0100 Subject: [PATCH 5/5] Update sol.py --- project_euler/problem_104/sol.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 10eb6d6e55f3..0818ac401c3a 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -13,10 +13,6 @@ the last nine digits are 1-9 pandigital, find k. """ -import sys - -sys.set_int_max_str_digits(0) # type: ignore - def check(number: int) -> bool: """