From eb9e6ae38b0a9d8d57ff3c6414d82871745c58aa Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 5 Oct 2022 11:12:19 +0000 Subject: [PATCH 1/6] 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 e9d7effe419891e3e50467a9a501bf808584ad9d Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Wed, 5 Oct 2022 23:04:31 +0530 Subject: [PATCH 2/6] Added Project Eular: 060 --- project_euler/problem_060/__init__.py | 0 project_euler/problem_060/sol1.py | 146 ++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 project_euler/problem_060/__init__.py create mode 100644 project_euler/problem_060/sol1.py diff --git a/project_euler/problem_060/__init__.py b/project_euler/problem_060/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py new file mode 100644 index 000000000000..d9d7fced3970 --- /dev/null +++ b/project_euler/problem_060/sol1.py @@ -0,0 +1,146 @@ +""" +Project Euler 60 +https://projecteuler.net/problem=60 + + + +The primes 3, 7, 109, and 673, are quite remarkable. +By taking any two primes and concatenating them in any order the +result will always be prime. +For example, taking 7 and 109, both 7109 and 1097 are prime. +The sum of these four primes, 792, represents the lowest sum for a +set of four primes with this property. + +Find the lowest sum for a set of five primes for which any two primes +concatenate to produce another prime. + +""" + +from math import sqrt + +pairs: list[str] = [] +minimum_sum = 0 +primes: list[int] = [] + + +def is_prime(number: int) -> bool: + """Checks to see if a number is a prime in O(sqrt(n)). + A number is prime if it has exactly two factors: 1 and itself. + + Taken from /maths/prime_check.py + + >>> is_prime(0) + False + >>> is_prime(1) + False + >>> is_prime(2) + True + >>> is_prime(3) + True + >>> is_prime(27) + False + >>> is_prime(87) + False + >>> is_prime(563) + True + >>> is_prime(2999) + True + >>> is_prime(67483) + False + """ + + # precondition + assert isinstance(number, int) and ( + number >= 0 + ), "'number' must been an int and positive" + + if 1 < number < 4: + # 2 and 3 are primes + return True + elif number < 2 or number % 2 == 0 or number % 3 == 0: + # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes + return False + + # All primes number are in format of 6k +/- 1 + for i in range(5, int(sqrt(number) + 1), 6): + if number % i == 0 or number % (i + 2) == 0: + return False + return True + + +def combiner(i, length): + """ + This function goes through all the combinations of the primes list in + ascending order, and finds pairs that satisfy the question until the + minimum_sum is low enough that it can stop and the minimum_sum is the minimum. + """ + + global pairs, minimum_sum + j = i # local variable j retains the original index + + if len(pairs) == length: # if it is one of the pairs we want + s = sum(int(x) for x in pairs) + if s < minimum_sum: + minimum_sum = s + primes.insert(i, pairs.pop(-1)) + return True + + while i < len(primes): + for prime in pairs: + if not is_prime(int(prime + primes[i])): + break + if not is_prime(int(primes[i] + prime)): + break + else: + pairs.append(primes.pop(i)) + if not combiner(i, length): + return False + + if pairs: + i += 1 + + if primes: + if j + len(pairs) - 1 != 0: + primes.insert(j, pairs.pop(-1)) + return True + else: + if int(pairs.pop(-1)) > minimum_sum / ( + 2 * 10 ** (length - 2) + ): # works for most test cases + return False + return True + + +def solution(n=5, limit=10000): + """ + The function returns the lowest sum for a set of n primes (n > 1) for which + any two primes concatenate to produce another prime. + + Limit is the maximum number of natural numbers to be checked up to. + + + >>> solution(2,10) + 10 + >>> solution(3,100) + 107 + >>> solution(4,1000) + 792 + """ + + global minimum_sum, primes, pairs + primes = [] + pairs = [] + for i in range(1, limit + 1): + if is_prime(i): + primes.append(str(i)) + minimum_sum = limit * 5 + combiner(0, n) + return minimum_sum + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + print(f"{solution() = }") From bd2a279a5b8021da483ecf9e2ac9998ef8a87f55 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 5 Oct 2022 17:35:52 +0000 Subject: [PATCH 3/6] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 64e9d5333a2f..32ea97267e07 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -792,6 +792,8 @@ * [Sol1](project_euler/problem_058/sol1.py) * Problem 059 * [Sol1](project_euler/problem_059/sol1.py) + * Problem 060 + * [Sol1](project_euler/problem_060/sol1.py) * Problem 062 * [Sol1](project_euler/problem_062/sol1.py) * Problem 063 From 2edb7da2c30d8f4838ed5e164f45ea91046ad7b4 Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Wed, 5 Oct 2022 23:10:07 +0530 Subject: [PATCH 4/6] Temporary fixed problem-114 --- 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 ba02ee4278b09b69e174e7aaacf6d7f661ff5e05 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 5 Oct 2022 17:41:54 +0000 Subject: [PATCH 5/6] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 32ea97267e07..be5b683398e9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -632,7 +632,7 @@ ## Physics * [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py) - * [Lorenz Transformation Four Vector](physics/lorenz_transformation_four_vector.py) + * [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py) * [N Body Simulation](physics/n_body_simulation.py) * [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py) From aa44c465958c1554061b92b5fc2c050c2d51f599 Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Wed, 5 Oct 2022 23:26:33 +0530 Subject: [PATCH 6/6] added hints/test/descriptive names --- project_euler/problem_060/sol1.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_060/sol1.py b/project_euler/problem_060/sol1.py index d9d7fced3970..2aaccdb9053a 100644 --- a/project_euler/problem_060/sol1.py +++ b/project_euler/problem_060/sol1.py @@ -20,7 +20,7 @@ pairs: list[str] = [] minimum_sum = 0 -primes: list[int] = [] +primes: list[str] = [] def is_prime(number: int) -> bool: @@ -68,15 +68,22 @@ def is_prime(number: int) -> bool: return True -def combiner(i, length): +def combiner(index: int, length: int) -> bool: """ This function goes through all the combinations of the primes list in ascending order, and finds pairs that satisfy the question until the minimum_sum is low enough that it can stop and the minimum_sum is the minimum. + + Returns True if the recursion should continue else False. + + >>> combiner(1,2) + False + >>> combiner(2,3) + False """ global pairs, minimum_sum - j = i # local variable j retains the original index + j = i = index # local variable j retains the original index and i gets changed if len(pairs) == length: # if it is one of the pairs we want s = sum(int(x) for x in pairs) @@ -109,11 +116,12 @@ def combiner(i, length): ): # works for most test cases return False return True + return False -def solution(n=5, limit=10000): +def solution(prime_count: int = 5, limit: int = 10000) -> int: """ - The function returns the lowest sum for a set of n primes (n > 1) for which + The function returns the lowest sum for a set of n primes (prime_count) for which any two primes concatenate to produce another prime. Limit is the maximum number of natural numbers to be checked up to. @@ -134,7 +142,7 @@ def solution(n=5, limit=10000): if is_prime(i): primes.append(str(i)) minimum_sum = limit * 5 - combiner(0, n) + combiner(0, prime_count) return minimum_sum