From 3e7ee2cdbea0675f8c1d42fde389bcd0f8d5608f Mon Sep 17 00:00:00 2001 From: Michael D Date: Mon, 12 Oct 2020 23:44:32 +0200 Subject: [PATCH 1/2] Fix style of the first ten solutions for Project Euler - Unify the header docstring, and add reference URLs to wikipedia or similar - Fix docstrings to be properly multilined - Add newlines where appropriate - Add doctests where they were missing - Remove doctests that test for the correct solution - fix obvious spelling or grammar mistakes in comments and exception messages - Fix line endings to be UNIX. This makes two of the files seem to have changed completely - no functional changes in any of the solutions were done (except for the spelling fixes mentioned above) --- project_euler/problem_001/sol1.py | 14 +++- project_euler/problem_001/sol2.py | 14 +++- project_euler/problem_001/sol3.py | 11 +++- project_euler/problem_001/sol4.py | 102 ++++++++++++++++-------------- project_euler/problem_001/sol5.py | 16 +++-- project_euler/problem_001/sol6.py | 14 +++- project_euler/problem_001/sol7.py | 14 +++- project_euler/problem_002/sol1.py | 25 +++++--- project_euler/problem_002/sol2.py | 87 +++++++++++++------------ project_euler/problem_002/sol3.py | 21 ++++-- project_euler/problem_002/sol4.py | 38 +++++++---- project_euler/problem_002/sol5.py | 24 ++++--- project_euler/problem_003/sol1.py | 43 ++++++++----- project_euler/problem_003/sol2.py | 32 ++++++---- project_euler/problem_003/sol3.py | 32 ++++++---- project_euler/problem_004/sol1.py | 22 +++++-- project_euler/problem_004/sol2.py | 21 ++++-- project_euler/problem_005/sol1.py | 36 +++++++---- project_euler/problem_005/sol2.py | 58 +++++++++++++---- project_euler/problem_006/sol1.py | 25 +++++--- project_euler/problem_006/sol2.py | 25 +++++--- project_euler/problem_006/sol3.py | 26 +++++--- project_euler/problem_006/sol4.py | 25 +++++--- project_euler/problem_007/sol1.py | 35 +++++++--- project_euler/problem_007/sol2.py | 46 +++++++++----- project_euler/problem_007/sol3.py | 36 ++++++++--- project_euler/problem_008/sol1.py | 60 +++++++++++------- project_euler/problem_008/sol2.py | 59 ++++++++++------- project_euler/problem_008/sol3.py | 59 +++++++++-------- project_euler/problem_009/sol1.py | 36 +++++++---- project_euler/problem_009/sol2.py | 34 ++++++---- project_euler/problem_009/sol3.py | 21 ++++-- project_euler/problem_010/sol1.py | 23 +++++-- project_euler/problem_010/sol2.py | 27 ++++++-- project_euler/problem_010/sol3.py | 31 +++++---- 35 files changed, 775 insertions(+), 417 deletions(-) diff --git a/project_euler/problem_001/sol1.py b/project_euler/problem_001/sol1.py index 385bbbbf43b3..3ffeed31d1dc 100644 --- a/project_euler/problem_001/sol1.py +++ b/project_euler/problem_001/sol1.py @@ -1,13 +1,21 @@ """ -Problem Statement: +Multiples of 3 and 5 +Problem 1 + If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. + +Find the sum of all the multiples of 3 or 5 below 1000. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=1 """ def solution(n: int = 1000) -> int: - """Returns the sum of all the multiples of 3 or 5 below n. + """ + Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) 0 diff --git a/project_euler/problem_001/sol2.py b/project_euler/problem_001/sol2.py index f08f548cb752..cd77785b1e0a 100644 --- a/project_euler/problem_001/sol2.py +++ b/project_euler/problem_001/sol2.py @@ -1,13 +1,21 @@ """ -Problem Statement: +Multiples of 3 and 5 +Problem 1 + If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. + +Find the sum of all the multiples of 3 or 5 below 1000. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=1 """ def solution(n: int = 1000) -> int: - """Returns the sum of all the multiples of 3 or 5 below n. + """ + Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) 0 diff --git a/project_euler/problem_001/sol3.py b/project_euler/problem_001/sol3.py index 67cb83faf238..602e70119bc9 100644 --- a/project_euler/problem_001/sol3.py +++ b/project_euler/problem_001/sol3.py @@ -1,8 +1,15 @@ """ -Problem Statement: +Multiples of 3 and 5 +Problem 1 + If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. + +Find the sum of all the multiples of 3 or 5 below 1000. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=1 """ diff --git a/project_euler/problem_001/sol4.py b/project_euler/problem_001/sol4.py index 77f323695898..846e87b1221f 100644 --- a/project_euler/problem_001/sol4.py +++ b/project_euler/problem_001/sol4.py @@ -1,47 +1,55 @@ -""" -Problem Statement: -If we list all the natural numbers below 10 that are multiples of 3 or 5, -we get 3, 5, 6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. -""" - - -def solution(n: int = 1000) -> int: - """Returns the sum of all the multiples of 3 or 5 below n. - - >>> solution(3) - 0 - >>> solution(4) - 3 - >>> solution(10) - 23 - >>> solution(600) - 83700 - """ - - xmulti = [] - zmulti = [] - z = 3 - x = 5 - temp = 1 - while True: - result = z * temp - if result < n: - zmulti.append(result) - temp += 1 - else: - temp = 1 - break - while True: - result = x * temp - if result < n: - xmulti.append(result) - temp += 1 - else: - break - collection = list(set(xmulti + zmulti)) - return sum(collection) - - -if __name__ == "__main__": - print(solution(int(input().strip()))) +""" +Multiples of 3 and 5 +Problem 1 + +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3, 5, 6 and 9. The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=1 +""" + + +def solution(n: int = 1000) -> int: + """ + Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + xmulti = [] + zmulti = [] + z = 3 + x = 5 + temp = 1 + while True: + result = z * temp + if result < n: + zmulti.append(result) + temp += 1 + else: + temp = 1 + break + while True: + result = x * temp + if result < n: + xmulti.append(result) + temp += 1 + else: + break + collection = list(set(xmulti + zmulti)) + return sum(collection) + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_001/sol5.py b/project_euler/problem_001/sol5.py index 256516802ca0..82bc43a348bb 100644 --- a/project_euler/problem_001/sol5.py +++ b/project_euler/problem_001/sol5.py @@ -1,14 +1,22 @@ """ -Problem Statement: +Multiples of 3 and 5 +Problem 1 + If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. + +Find the sum of all the multiples of 3 or 5 below 1000. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=1 """ def solution(n: int = 1000) -> int: - """Returns the sum of all the multiples of 3 or 5 below n. - A straightforward pythonic solution using list comprehension. + """ + Returns the sum of all the multiples of 3 or 5 below n. + A straightforward pythonic solution using list comprehension. >>> solution(3) 0 diff --git a/project_euler/problem_001/sol6.py b/project_euler/problem_001/sol6.py index 5f60512a73fb..05f551c0b9d7 100644 --- a/project_euler/problem_001/sol6.py +++ b/project_euler/problem_001/sol6.py @@ -1,13 +1,21 @@ """ -Problem Statement: +Multiples of 3 and 5 +Problem 1 + If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. + +Find the sum of all the multiples of 3 or 5 below 1000. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=1 """ def solution(n: int = 1000) -> int: - """Returns the sum of all the multiples of 3 or 5 below n. + """ + Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) 0 diff --git a/project_euler/problem_001/sol7.py b/project_euler/problem_001/sol7.py index 5761c00f2996..e43ab958ac62 100644 --- a/project_euler/problem_001/sol7.py +++ b/project_euler/problem_001/sol7.py @@ -1,13 +1,21 @@ """ -Problem Statement: +Multiples of 3 and 5 +Problem 1 + If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. + +Find the sum of all the multiples of 3 or 5 below 1000. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=1 """ def solution(n: int = 1000) -> int: - """Returns the sum of all the multiples of 3 or 5 below n. + """ + Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) 0 diff --git a/project_euler/problem_002/sol1.py b/project_euler/problem_002/sol1.py index 2acc93b0affc..9e6c5aaf880b 100644 --- a/project_euler/problem_002/sol1.py +++ b/project_euler/problem_002/sol1.py @@ -1,19 +1,27 @@ """ -Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two -terms. By starting with 1 and 2, the first 10 terms will be: +Even Fibonacci Numbers +Problem 2 - 1,2,3,5,8,13,21,34,55,89,.. +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed -n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is -10. +four million, find the sum of the even-valued terms. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=2 + - Wikipedia page for Fibonacci Numbers: + https://en.wikipedia.org/wiki/Fibonacci_number """ def solution(n: int = 4000000) -> int: - """Returns the sum of all fibonacci sequence even elements that are lower - or equals to n. + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. >>> solution(10) 10 @@ -26,6 +34,7 @@ def solution(n: int = 4000000) -> int: >>> solution(34) 44 """ + i = 1 j = 2 total = 0 diff --git a/project_euler/problem_002/sol2.py b/project_euler/problem_002/sol2.py index 01fc552b9b21..20f4e6a7710e 100644 --- a/project_euler/problem_002/sol2.py +++ b/project_euler/problem_002/sol2.py @@ -1,39 +1,48 @@ -""" -Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two -terms. By starting with 1 and 2, the first 10 terms will be: - - 1,2,3,5,8,13,21,34,55,89,.. - -By considering the terms in the Fibonacci sequence whose values do not exceed -n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is -10. -""" - - -def solution(n: int = 4000000) -> int: - """Returns the sum of all fibonacci sequence even elements that are lower - or equals to n. - - >>> solution(10) - 10 - >>> solution(15) - 10 - >>> solution(2) - 2 - >>> solution(1) - 0 - >>> solution(34) - 44 - """ - even_fibs = [] - a, b = 0, 1 - while b <= n: - if b % 2 == 0: - even_fibs.append(b) - a, b = b, a + b - return sum(even_fibs) - - -if __name__ == "__main__": - print(solution(int(input().strip()))) +""" +Even Fibonacci Numbers +Problem 2 + +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + +By considering the terms in the Fibonacci sequence whose values do not exceed +four million, find the sum of the even-valued terms. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=2 + - Wikipedia page for Fibonacci Numbers: + https://en.wikipedia.org/wiki/Fibonacci_number +""" + + +def solution(n: int = 4000000) -> int: + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + + even_fibs = [] + a, b = 0, 1 + while b <= n: + if b % 2 == 0: + even_fibs.append(b) + a, b = b, a + b + return sum(even_fibs) + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_002/sol3.py b/project_euler/problem_002/sol3.py index 53d8ca6f1b68..c87a8f3aeaaf 100644 --- a/project_euler/problem_002/sol3.py +++ b/project_euler/problem_002/sol3.py @@ -1,19 +1,27 @@ """ -Problem: +Even Fibonacci Numbers +Problem 2 + Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: - 1,2,3,5,8,13,21,34,55,89,.. +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed -n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is -10. +four million, find the sum of the even-valued terms. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=2 + - Wikipedia page for Fibonacci Numbers: + https://en.wikipedia.org/wiki/Fibonacci_number """ def solution(n: int = 4000000) -> int: - """Returns the sum of all fibonacci sequence even elements that are lower - or equals to n. + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. >>> solution(10) 10 @@ -26,6 +34,7 @@ def solution(n: int = 4000000) -> int: >>> solution(34) 44 """ + if n <= 1: return 0 a = 0 diff --git a/project_euler/problem_002/sol4.py b/project_euler/problem_002/sol4.py index a87410b7006d..17dec6b7aee6 100644 --- a/project_euler/problem_002/sol4.py +++ b/project_euler/problem_002/sol4.py @@ -1,21 +1,30 @@ """ -Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two -terms. By starting with 1 and 2, the first 10 terms will be: +Even Fibonacci Numbers +Problem 2 - 1,2,3,5,8,13,21,34,55,89,.. +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed -n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is -10. +four million, find the sum of the even-valued terms. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=2 + - Wikipedia page for Fibonacci Numbers: + https://en.wikipedia.org/wiki/Fibonacci_number """ + import math from decimal import Decimal, getcontext def solution(n: int = 4000000) -> int: - """Returns the sum of all fibonacci sequence even elements that are lower - or equals to n. + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. >>> solution(10) 10 @@ -32,26 +41,27 @@ def solution(n: int = 4000000) -> int: >>> solution(0) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution(-17) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution([]) Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. >>> solution("asd") Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. """ + try: n = int(n) except (TypeError, ValueError): - raise TypeError("Parameter n must be int or passive of cast to int.") + raise TypeError("Parameter n must be int or castable to int.") if n <= 0: - raise ValueError("Parameter n must be greater or equal to one.") + raise ValueError("Parameter n must be greater than or equal to one.") getcontext().prec = 100 phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2) diff --git a/project_euler/problem_002/sol5.py b/project_euler/problem_002/sol5.py index dcf6eae85891..04a59f19b778 100644 --- a/project_euler/problem_002/sol5.py +++ b/project_euler/problem_002/sol5.py @@ -1,19 +1,27 @@ """ -Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two -terms. By starting with 1 and 2, the first 10 terms will be: +Even Fibonacci Numbers +Problem 2 - 1,2,3,5,8,13,21,34,55,89,.. +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed -n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is -10. +four million, find the sum of the even-valued terms. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=2 + - Wikipedia page for Fibonacci Numbers: + https://en.wikipedia.org/wiki/Fibonacci_number """ def solution(n: int = 4000000) -> int: - """Returns the sum of all fibonacci sequence even elements that are lower - or equals to n. + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. >>> solution(10) 10 diff --git a/project_euler/problem_003/sol1.py b/project_euler/problem_003/sol1.py index 22efeb2c4e90..ec6ccef43c40 100644 --- a/project_euler/problem_003/sol1.py +++ b/project_euler/problem_003/sol1.py @@ -1,16 +1,25 @@ """ -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor -of a given number N? +Largest prime factor +Problem 3 -e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=3 + - Wikipedia page for Prime factors + https://en.wikipedia.org/wiki/Prime_number#Unique_factorization """ import math def isprime(num: int) -> bool: - """Returns boolean representing primality of given number num. + """ + Returns boolean representing primality of given number num. + >>> isprime(2) True >>> isprime(3) @@ -22,14 +31,15 @@ def isprime(num: int) -> bool: >>> isprime(0) Traceback (most recent call last): ... - ValueError: Parameter num must be greater or equal to two. + ValueError: Parameter num must be greater than or equal to two. >>> isprime(1) Traceback (most recent call last): ... - ValueError: Parameter num must be greater or equal to two. + ValueError: Parameter num must be greater than or equal to two. """ + if num <= 1: - raise ValueError("Parameter num must be greater or equal to two.") + raise ValueError("Parameter num must be greater than or equal to two.") if num == 2: return True elif num % 2 == 0: @@ -41,7 +51,9 @@ def isprime(num: int) -> bool: def solution(n: int = 600851475143) -> int: - """Returns the largest prime factor of a given number n. + """ + Returns the largest prime factor of a given number n. + >>> solution(13195) 29 >>> solution(10) @@ -53,26 +65,27 @@ def solution(n: int = 600851475143) -> int: >>> solution(0) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution(-17) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution([]) Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. >>> solution("asd") Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. """ + try: n = int(n) except (TypeError, ValueError): - raise TypeError("Parameter n must be int or passive of cast to int.") + raise TypeError("Parameter n must be int or castable to int.") if n <= 0: - raise ValueError("Parameter n must be greater or equal to one.") + raise ValueError("Parameter n must be greater than or equal to one.") max_number = 0 if isprime(n): return n diff --git a/project_euler/problem_003/sol2.py b/project_euler/problem_003/sol2.py index f28232109a84..58aff987df2c 100644 --- a/project_euler/problem_003/sol2.py +++ b/project_euler/problem_003/sol2.py @@ -1,14 +1,23 @@ """ -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor -of a given number N? +Largest prime factor +Problem 3 -e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=3 + - Wikipedia page for Prime factors + https://en.wikipedia.org/wiki/Prime_number#Unique_factorization """ def solution(n: int = 600851475143) -> int: - """Returns the largest prime factor of a given number n. + """ + Returns the largest prime factor of a given number n. + >>> solution(13195) 29 >>> solution(10) @@ -20,26 +29,27 @@ def solution(n: int = 600851475143) -> int: >>> solution(0) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution(-17) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution([]) Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. >>> solution("asd") Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. """ + try: n = int(n) except (TypeError, ValueError): - raise TypeError("Parameter n must be int or passive of cast to int.") + raise TypeError("Parameter n must be int or castable to int.") if n <= 0: - raise ValueError("Parameter n must be greater or equal to one.") + raise ValueError("Parameter n must be greater than or equal to one.") prime = 1 i = 2 while i * i <= n: diff --git a/project_euler/problem_003/sol3.py b/project_euler/problem_003/sol3.py index 676717cceca8..6227c243e219 100644 --- a/project_euler/problem_003/sol3.py +++ b/project_euler/problem_003/sol3.py @@ -1,14 +1,23 @@ """ -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor -of a given number N? +Largest prime factor +Problem 3 -e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=3 + - Wikipedia page for Prime factors + https://en.wikipedia.org/wiki/Prime_number#Unique_factorization """ def solution(n: int = 600851475143) -> int: - """Returns the largest prime factor of a given number n. + """ + Returns the largest prime factor of a given number n. + >>> solution(13195) 29 >>> solution(10) @@ -20,26 +29,27 @@ def solution(n: int = 600851475143) -> int: >>> solution(0) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution(-17) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution([]) Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. >>> solution("asd") Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. """ + try: n = int(n) except (TypeError, ValueError): - raise TypeError("Parameter n must be int or passive of cast to int.") + raise TypeError("Parameter n must be int or castable to int.") if n <= 0: - raise ValueError("Parameter n must be greater or equal to one.") + raise ValueError("Parameter n must be greater than or equal to one.") i = 2 ans = 0 if n == 2: diff --git a/project_euler/problem_004/sol1.py b/project_euler/problem_004/sol1.py index 42f56f3ef389..4e60a64fb15a 100644 --- a/project_euler/problem_004/sol1.py +++ b/project_euler/problem_004/sol1.py @@ -1,15 +1,23 @@ """ -Problem: -A palindromic number reads the same both ways. The largest palindrome made from -the product of two 2-digit numbers is 9009 = 91 x 99. +Largest palindrome product +Problem 4 -Find the largest palindrome made from the product of two 3-digit numbers which -is less than N. +A palindromic number reads the same both ways. The largest palindrome made +from the product of two 2-digit numbers is 9009 = 91 × 99. + +Find the largest palindrome made from the product of two 3-digit numbers. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=4 + - Wikipedia page for palindromic numbers: + https://en.wikipedia.org/wiki/Palindromic_number """ def solution(n: int = 998001) -> int: - """Returns the largest palindrome made from the product of two 3-digit + """ + Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. >>> solution(20000) @@ -23,10 +31,10 @@ def solution(n: int = 998001) -> int: ... ValueError: That number is larger than our acceptable range. """ + # fetches the next number for number in range(n - 1, 9999, -1): - # converts number into string. str_number = str(number) # checks whether 'str_number' is a palindrome. diff --git a/project_euler/problem_004/sol2.py b/project_euler/problem_004/sol2.py index 8ee082ad2f6a..412c59a9b784 100644 --- a/project_euler/problem_004/sol2.py +++ b/project_euler/problem_004/sol2.py @@ -1,15 +1,23 @@ """ -Problem: -A palindromic number reads the same both ways. The largest palindrome made from -the product of two 2-digit numbers is 9009 = 91 x 99. +Largest palindrome product +Problem 4 -Find the largest palindrome made from the product of two 3-digit numbers which -is less than N. +A palindromic number reads the same both ways. The largest palindrome made +from the product of two 2-digit numbers is 9009 = 91 × 99. + +Find the largest palindrome made from the product of two 3-digit numbers. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=4 + - Wikipedia page for palindromic numbers: + https://en.wikipedia.org/wiki/Palindromic_number """ def solution(n: int = 998001) -> int: - """Returns the largest palindrome made from the product of two 3-digit + """ + Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. >>> solution(20000) @@ -19,6 +27,7 @@ def solution(n: int = 998001) -> int: >>> solution(40000) 39893 """ + answer = 0 for i in range(999, 99, -1): # 3 digit numbers range from 999 down to 100 for j in range(999, 99, -1): diff --git a/project_euler/problem_005/sol1.py b/project_euler/problem_005/sol1.py index a347d6564fa7..1848caafe672 100644 --- a/project_euler/problem_005/sol1.py +++ b/project_euler/problem_005/sol1.py @@ -1,23 +1,30 @@ """ -Problem: -2520 is the smallest number that can be divided by each of the numbers from 1 -to 10 without any remainder. +Smallest multiple +Problem 5 -What is the smallest positive number that is evenly divisible(divisible with no -remainder) by all of the numbers from 1 to N? +2520 is the smallest number that can be divided by each of the numbers +from 1 to 10 without any remainder. + +What is the smallest positive number that is _evenly divisible_ by all +of the numbers from 1 to 20? + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=5 + - Definition of 'evenly divisible': + https://en.wiktionary.org/wiki/evenly_divisible """ def solution(n: int = 20) -> int: - """Returns the smallest positive number that is evenly divisible(divisible + """ + Returns the smallest positive number that is evenly divisible (divisible with no remainder) by all of the numbers from 1 to n. >>> solution(10) 2520 >>> solution(15) 360360 - >>> solution(20) - 232792560 >>> solution(22) 232792560 >>> solution(3.4) @@ -25,26 +32,27 @@ def solution(n: int = 20) -> int: >>> solution(0) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution(-17) Traceback (most recent call last): ... - ValueError: Parameter n must be greater or equal to one. + ValueError: Parameter n must be greater than or equal to one. >>> solution([]) Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. >>> solution("asd") Traceback (most recent call last): ... - TypeError: Parameter n must be int or passive of cast to int. + TypeError: Parameter n must be int or castable to int. """ + try: n = int(n) except (TypeError, ValueError): - raise TypeError("Parameter n must be int or passive of cast to int.") + raise TypeError("Parameter n must be int or castable to int.") if n <= 0: - raise ValueError("Parameter n must be greater or equal to one.") + raise ValueError("Parameter n must be greater than or equal to one.") i = 0 while 1: i += n * (n - 1) diff --git a/project_euler/problem_005/sol2.py b/project_euler/problem_005/sol2.py index 57b4cc823d82..80fdc0293a3c 100644 --- a/project_euler/problem_005/sol2.py +++ b/project_euler/problem_005/sol2.py @@ -1,38 +1,74 @@ """ -Problem: -2520 is the smallest number that can be divided by each of the numbers from 1 -to 10 without any remainder. +Smallest multiple +Problem 5 -What is the smallest positive number that is evenly divisible(divisible with no -remainder) by all of the numbers from 1 to N? +2520 is the smallest number that can be divided by each of the numbers +from 1 to 10 without any remainder. + +What is the smallest positive number that is _evenly divisible_ by all +of the numbers from 1 to 20? + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=5 + - Definition of 'evenly divisible': + https://en.wiktionary.org/wiki/evenly_divisible + - Wikipedia page for the Euklidean algorithm for the GCD: + https://en.wikipedia.org/wiki/Euclidean_algorithm + - Wikipedia page for the Least common Multiple (LCM): + https://en.wikipedia.org/wiki/Least_common_multiple """ -""" Euclidean GCD Algorithm """ def gcd(x: int, y: int) -> int: - return x if y == 0 else gcd(y, x % y) + """ + Euclidean GCD algorithm (Greatest Common Divisor) + >>> gcd(0, 0) + 0 + >>> gcd(23, 42) + 1 + >>> gcd(15, 33) + 3 + >>> gcd(12345, 67890) + 15 + """ -""" Using the property lcm*gcd of two numbers = product of them """ + return x if y == 0 else gcd(y, x % y) def lcm(x: int, y: int) -> int: + """ + Least Common Multiple. + + Using the property that lcm(a, b) * gcd(a, b) = a*b + + >>> lcm(3, 15) + 15 + >>> lcm(1, 27) + 27 + >>> lcm(13, 27) + 351 + >>> lcm(64, 48) + 192 + """ + return (x * y) // gcd(x, y) def solution(n: int = 20) -> int: - """Returns the smallest positive number that is evenly divisible(divisible + """ + Returns the smallest positive number that is evenly divisible (divisible with no remainder) by all of the numbers from 1 to n. >>> solution(10) 2520 >>> solution(15) 360360 - >>> solution(20) - 232792560 >>> solution(22) 232792560 """ + g = 1 for i in range(1, n + 1): g = lcm(g, i) diff --git a/project_euler/problem_006/sol1.py b/project_euler/problem_006/sol1.py index 38f995bbf822..72abf7ff7b6f 100644 --- a/project_euler/problem_006/sol1.py +++ b/project_euler/problem_006/sol1.py @@ -1,22 +1,28 @@ """ -Problem 6: https://projecteuler.net/problem=6 +Sum square difference +Problem 6 The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 + 1^2 + 2^2 + ... + 10^2 = 385 The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 552 = 3025 + (1 + 2 + ... + 10)^2 = 55^2 = 3025 -Hence the difference between the sum of the squares of the first ten natural -numbers and the square of the sum is 3025 − 385 = 2640. +Hence the difference between the sum of the squares of the first ten +natural numbers and the square of the sum is 3025 - 385 = 2640. -Find the difference between the sum of the squares of the first N natural -numbers and the square of the sum. +Find the difference between the sum of the squares of the first one +hundred natural numbers and the square of the sum. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=6 """ def solution(n: int = 100) -> int: - """Returns the difference between the sum of the squares of the first n + """ + Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. >>> solution(10) @@ -27,9 +33,8 @@ def solution(n: int = 100) -> int: 41230 >>> solution(50) 1582700 - >>> solution() - 25164150 """ + sum_of_squares = 0 sum_of_ints = 0 for i in range(1, n + 1): diff --git a/project_euler/problem_006/sol2.py b/project_euler/problem_006/sol2.py index f4d74c993f0d..08d7aaad4308 100644 --- a/project_euler/problem_006/sol2.py +++ b/project_euler/problem_006/sol2.py @@ -1,22 +1,28 @@ """ -Problem 6: https://projecteuler.net/problem=6 +Sum square difference +Problem 6 The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 + 1^2 + 2^2 + ... + 10^2 = 385 The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 552 = 3025 + (1 + 2 + ... + 10)^2 = 55^2 = 3025 -Hence the difference between the sum of the squares of the first ten natural -numbers and the square of the sum is 3025 − 385 = 2640. +Hence the difference between the sum of the squares of the first ten +natural numbers and the square of the sum is 3025 - 385 = 2640. -Find the difference between the sum of the squares of the first N natural -numbers and the square of the sum. +Find the difference between the sum of the squares of the first one +hundred natural numbers and the square of the sum. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=6 """ def solution(n: int = 100) -> int: - """Returns the difference between the sum of the squares of the first n + """ + Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. >>> solution(10) @@ -27,9 +33,8 @@ def solution(n: int = 100) -> int: 41230 >>> solution(50) 1582700 - >>> solution() - 25164150 """ + sum_cubes = (n * (n + 1) // 2) ** 2 sum_squares = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares diff --git a/project_euler/problem_006/sol3.py b/project_euler/problem_006/sol3.py index 8b5c5d3ba4aa..cbd23fccc9d5 100644 --- a/project_euler/problem_006/sol3.py +++ b/project_euler/problem_006/sol3.py @@ -1,23 +1,30 @@ """ -Problem 6: https://projecteuler.net/problem=6 +Sum square difference +Problem 6 The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 + 1^2 + 2^2 + ... + 10^2 = 385 The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 552 = 3025 + (1 + 2 + ... + 10)^2 = 55^2 = 3025 -Hence the difference between the sum of the squares of the first ten natural -numbers and the square of the sum is 3025 − 385 = 2640. +Hence the difference between the sum of the squares of the first ten +natural numbers and the square of the sum is 3025 - 385 = 2640. -Find the difference between the sum of the squares of the first N natural -numbers and the square of the sum. +Find the difference between the sum of the squares of the first one +hundred natural numbers and the square of the sum. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=6 """ + import math def solution(n: int = 100) -> int: - """Returns the difference between the sum of the squares of the first n + """ + Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. >>> solution(10) @@ -28,9 +35,8 @@ def solution(n: int = 100) -> int: 41230 >>> solution(50) 1582700 - >>> solution() - 25164150 """ + sum_of_squares = sum([i * i for i in range(1, n + 1)]) square_of_sum = int(math.pow(sum(range(1, n + 1)), 2)) return square_of_sum - sum_of_squares diff --git a/project_euler/problem_006/sol4.py b/project_euler/problem_006/sol4.py index 5fae84008448..6693e7603192 100644 --- a/project_euler/problem_006/sol4.py +++ b/project_euler/problem_006/sol4.py @@ -1,22 +1,28 @@ """ -Problem 6: https://projecteuler.net/problem=6 +Sum square difference +Problem 6 The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 + 1^2 + 2^2 + ... + 10^2 = 385 The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 552 = 3025 + (1 + 2 + ... + 10)^2 = 55^2 = 3025 -Hence the difference between the sum of the squares of the first ten natural -numbers and the square of the sum is 3025 − 385 = 2640. +Hence the difference between the sum of the squares of the first ten +natural numbers and the square of the sum is 3025 - 385 = 2640. -Find the difference between the sum of the squares of the first N natural -numbers and the square of the sum. +Find the difference between the sum of the squares of the first one +hundred natural numbers and the square of the sum. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=6 """ def solution(n: int = 100) -> int: - """Returns the difference between the sum of the squares of the first n + """ + Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. >>> solution(10) @@ -27,9 +33,8 @@ def solution(n: int = 100) -> int: 41230 >>> solution(50) 1582700 - >>> solution() - 25164150 """ + sum_of_squares = n * (n + 1) * (2 * n + 1) / 6 square_of_sum = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares) diff --git a/project_euler/problem_007/sol1.py b/project_euler/problem_007/sol1.py index 727d7fb7fac6..c3da5a4e5f0a 100644 --- a/project_euler/problem_007/sol1.py +++ b/project_euler/problem_007/sol1.py @@ -1,17 +1,36 @@ """ -Problem 7: https://projecteuler.net/problem=7 +10001st prime +Problem 7 -By listing the first six prime numbers: +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. - 2, 3, 5, 7, 11, and 13 +What is the 10001st prime number? -We can see that the 6th prime is 13. What is the Nth prime number? +References: + - The Project Euler problem page: + https://projecteuler.net/problem=7 + - Wikipedia page for prime numbers: + https://en.wikipedia.org/wiki/Prime_number """ + from math import sqrt def is_prime(num: int) -> bool: - """Determines whether the given number is prime or not""" + """ + Determines whether the given number is prime or not + + >>> is_prime(2) + True + >>> is_prime(15) + False + >>> is_prime(29) + True + >>> is_prime(0) + False + """ + if num == 2: return True elif num % 2 == 0: @@ -25,7 +44,8 @@ def is_prime(num: int) -> bool: def solution(nth: int = 10001) -> int: - """Returns the n-th prime number. + """ + Returns the n-th prime number. >>> solution(6) 13 @@ -39,9 +59,8 @@ def solution(nth: int = 10001) -> int: 229 >>> solution(100) 541 - >>> solution() - 104743 """ + count = 0 number = 1 while count != nth and number < 3: diff --git a/project_euler/problem_007/sol2.py b/project_euler/problem_007/sol2.py index 62806e1e2e5d..419046ed7d7a 100644 --- a/project_euler/problem_007/sol2.py +++ b/project_euler/problem_007/sol2.py @@ -1,16 +1,32 @@ """ -Problem 7: https://projecteuler.net/problem=7 +10001st prime +Problem 7 -By listing the first six prime numbers: +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. - 2, 3, 5, 7, 11, and 13 +What is the 10001st prime number? -We can see that the 6th prime is 13. What is the Nth prime number? +References: + - The Project Euler problem page: + https://projecteuler.net/problem=7 + - Wikipedia page for prime numbers: + https://en.wikipedia.org/wiki/Prime_number """ def isprime(number: int) -> bool: - """Determines whether the given number is prime or not""" + """ + Determines whether the given number is prime or not + + >>> isprime(2) + True + >>> isprime(15) + False + >>> isprime(29) + True + """ + for i in range(2, int(number ** 0.5) + 1): if number % i == 0: return False @@ -18,7 +34,8 @@ def isprime(number: int) -> bool: def solution(nth: int = 10001) -> int: - """Returns the n-th prime number. + """ + Returns the n-th prime number. >>> solution(6) 13 @@ -32,35 +49,32 @@ def solution(nth: int = 10001) -> int: 229 >>> solution(100) 541 - >>> solution() - 104743 >>> solution(3.4) 5 >>> solution(0) Traceback (most recent call last): ... - ValueError: Parameter nth must be greater or equal to one. + ValueError: Parameter nth must be greater than or equal to one. >>> solution(-17) Traceback (most recent call last): ... - ValueError: Parameter nth must be greater or equal to one. + ValueError: Parameter nth must be greater than or equal to one. >>> solution([]) Traceback (most recent call last): ... - TypeError: Parameter nth must be int or passive of cast to int. + TypeError: Parameter nth must be int or castable to int. >>> solution("asd") Traceback (most recent call last): ... - TypeError: Parameter nth must be int or passive of cast to int. + TypeError: Parameter nth must be int or castable to int. """ + try: nth = int(nth) except (TypeError, ValueError): - raise TypeError( - "Parameter nth must be int or passive of cast to int." - ) from None + raise TypeError("Parameter nth must be int or castable to int.") from None if nth <= 0: - raise ValueError("Parameter nth must be greater or equal to one.") + raise ValueError("Parameter nth must be greater than or equal to one.") primes = [] num = 2 while len(primes) < nth: diff --git a/project_euler/problem_007/sol3.py b/project_euler/problem_007/sol3.py index 1182875c05c9..39893897da9c 100644 --- a/project_euler/problem_007/sol3.py +++ b/project_euler/problem_007/sol3.py @@ -1,24 +1,45 @@ """ -Project 7: https://projecteuler.net/problem=7 +10001st prime +Problem 7 -By listing the first six prime numbers: +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. - 2, 3, 5, 7, 11, and 13 +What is the 10001st prime number? -We can see that the 6th prime is 13. What is the Nth prime number? +References: + - The Project Euler problem page: + https://projecteuler.net/problem=7 + - Wikipedia page for prime numbers: + https://en.wikipedia.org/wiki/Prime_number """ + import itertools import math def prime_check(number: int) -> bool: - """Determines whether a given number is prime or not""" + """ + Determines whether a given number is prime or not + + >>> prime_check(2) + True + >>> prime_check(15) + False + >>> prime_check(29) + True + """ + if number % 2 == 0 and number > 2: return False return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) def prime_generator(): + """ + Generate a sequence of prime numbers + """ + num = 2 while True: if prime_check(num): @@ -27,7 +48,8 @@ def prime_generator(): def solution(nth: int = 10001) -> int: - """Returns the n-th prime number. + """ + Returns the n-th prime number. >>> solution(6) 13 @@ -41,8 +63,6 @@ def solution(nth: int = 10001) -> int: 229 >>> solution(100) 541 - >>> solution() - 104743 """ return next(itertools.islice(prime_generator(), nth - 1, nth)) diff --git a/project_euler/problem_008/sol1.py b/project_euler/problem_008/sol1.py index db15907b3fbd..d9bcab68d5f2 100644 --- a/project_euler/problem_008/sol1.py +++ b/project_euler/problem_008/sol1.py @@ -1,33 +1,39 @@ """ -Problem 8: https://projecteuler.net/problem=8 +Largest product in a series +Problem 8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. -73167176531330624919225119674426574742355349194934 -96983520312774506326239578318016984801869478851843 -85861560789112949495459501737958331952853208805511 -12540698747158523863050715693290963295227443043557 -66896648950445244523161731856403098711121722383113 -62229893423380308135336276614282806444486645238749 -30358907296290491560440772390713810515859307960866 -70172427121883998797908792274921901699720888093776 -65727333001053367881220235421809751254540594752243 -52584907711670556013604839586446706324415722155397 -53697817977846174064955149290862569321978468622482 -83972241375657056057490261407972968652414535100474 -82166370484403199890008895243450658541227588666881 -16427171479924442928230863465674813919123162824586 -17866458359124566529476545682848912883142607690042 -24219022671055626321111109370544217506941658960408 -07198403850962455444362981230987879927244284909188 -84580156166097919133875499200524063689912560717606 -05886116467109405077541002256983155200055935729725 -71636269561882670428252483600823257530420752963450 + 73167176531330624919225119674426574742355349194934 + 96983520312774506326239578318016984801869478851843 + 85861560789112949495459501737958331952853208805511 + 12540698747158523863050715693290963295227443043557 + 66896648950445244523161731856403098711121722383113 + 62229893423380308135336276614282806444486645238749 + 30358907296290491560440772390713810515859307960866 + 70172427121883998797908792274921901699720888093776 + 65727333001053367881220235421809751254540594752243 + 52584907711670556013604839586446706324415722155397 + 53697817977846174064955149290862569321978468622482 + 83972241375657056057490261407972968652414535100474 + 82166370484403199890008895243450658541227588666881 + 16427171479924442928230863465674813919123162824586 + 17866458359124566529476545682848912883142607690042 + 24219022671055626321111109370544217506941658960408 + 07198403850962455444362981230987879927244284909188 + 84580156166097919133875499200524063689912560717606 + 05886116467109405077541002256983155200055935729725 + 71636269561882670428252483600823257530420752963450 Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=8 """ + import sys N = """73167176531330624919225119674426574742355349194934\ @@ -53,12 +59,18 @@ def solution(n: str = N) -> int: - """Find the thirteen adjacent digits in the 1000-digit number n that have + """ + Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. - >>> solution(N) - 23514624000 + >>> solution("13978431290823798458352374") + 609638400 + >>> solution("13978431295823798458352374") + 2612736000 + >>> solution("1397843129582379841238352374") + 209018880 """ + largest_product = -sys.maxsize - 1 for i in range(len(n) - 12): product = 1 diff --git a/project_euler/problem_008/sol2.py b/project_euler/problem_008/sol2.py index 1b338a9553d7..a9bf2a2244ab 100644 --- a/project_euler/problem_008/sol2.py +++ b/project_euler/problem_008/sol2.py @@ -1,32 +1,37 @@ """ -Problem 8: https://projecteuler.net/problem=8 +Largest product in a series +Problem 8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. -73167176531330624919225119674426574742355349194934 -96983520312774506326239578318016984801869478851843 -85861560789112949495459501737958331952853208805511 -12540698747158523863050715693290963295227443043557 -66896648950445244523161731856403098711121722383113 -62229893423380308135336276614282806444486645238749 -30358907296290491560440772390713810515859307960866 -70172427121883998797908792274921901699720888093776 -65727333001053367881220235421809751254540594752243 -52584907711670556013604839586446706324415722155397 -53697817977846174064955149290862569321978468622482 -83972241375657056057490261407972968652414535100474 -82166370484403199890008895243450658541227588666881 -16427171479924442928230863465674813919123162824586 -17866458359124566529476545682848912883142607690042 -24219022671055626321111109370544217506941658960408 -07198403850962455444362981230987879927244284909188 -84580156166097919133875499200524063689912560717606 -05886116467109405077541002256983155200055935729725 -71636269561882670428252483600823257530420752963450 + 73167176531330624919225119674426574742355349194934 + 96983520312774506326239578318016984801869478851843 + 85861560789112949495459501737958331952853208805511 + 12540698747158523863050715693290963295227443043557 + 66896648950445244523161731856403098711121722383113 + 62229893423380308135336276614282806444486645238749 + 30358907296290491560440772390713810515859307960866 + 70172427121883998797908792274921901699720888093776 + 65727333001053367881220235421809751254540594752243 + 52584907711670556013604839586446706324415722155397 + 53697817977846174064955149290862569321978468622482 + 83972241375657056057490261407972968652414535100474 + 82166370484403199890008895243450658541227588666881 + 16427171479924442928230863465674813919123162824586 + 17866458359124566529476545682848912883142607690042 + 24219022671055626321111109370544217506941658960408 + 07198403850962455444362981230987879927244284909188 + 84580156166097919133875499200524063689912560717606 + 05886116467109405077541002256983155200055935729725 + 71636269561882670428252483600823257530420752963450 Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=8 """ from functools import reduce @@ -56,12 +61,18 @@ def solution(n: str = N) -> int: - """Find the thirteen adjacent digits in the 1000-digit number n that have + """ + Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. - >>> solution(N) - 23514624000 + >>> solution("13978431290823798458352374") + 609638400 + >>> solution("13978431295823798458352374") + 2612736000 + >>> solution("1397843129582379841238352374") + 209018880 """ + return max( [ reduce(lambda x, y: int(x) * int(y), n[i : i + 13]) diff --git a/project_euler/problem_008/sol3.py b/project_euler/problem_008/sol3.py index 17f68cba57d3..357b2f0e36a5 100644 --- a/project_euler/problem_008/sol3.py +++ b/project_euler/problem_008/sol3.py @@ -1,33 +1,39 @@ """ -Problem 8: https://projecteuler.net/problem=8 +Largest product in a series +Problem 8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. -73167176531330624919225119674426574742355349194934 -96983520312774506326239578318016984801869478851843 -85861560789112949495459501737958331952853208805511 -12540698747158523863050715693290963295227443043557 -66896648950445244523161731856403098711121722383113 -62229893423380308135336276614282806444486645238749 -30358907296290491560440772390713810515859307960866 -70172427121883998797908792274921901699720888093776 -65727333001053367881220235421809751254540594752243 -52584907711670556013604839586446706324415722155397 -53697817977846174064955149290862569321978468622482 -83972241375657056057490261407972968652414535100474 -82166370484403199890008895243450658541227588666881 -16427171479924442928230863465674813919123162824586 -17866458359124566529476545682848912883142607690042 -24219022671055626321111109370544217506941658960408 -07198403850962455444362981230987879927244284909188 -84580156166097919133875499200524063689912560717606 -05886116467109405077541002256983155200055935729725 -71636269561882670428252483600823257530420752963450 + 73167176531330624919225119674426574742355349194934 + 96983520312774506326239578318016984801869478851843 + 85861560789112949495459501737958331952853208805511 + 12540698747158523863050715693290963295227443043557 + 66896648950445244523161731856403098711121722383113 + 62229893423380308135336276614282806444486645238749 + 30358907296290491560440772390713810515859307960866 + 70172427121883998797908792274921901699720888093776 + 65727333001053367881220235421809751254540594752243 + 52584907711670556013604839586446706324415722155397 + 53697817977846174064955149290862569321978468622482 + 83972241375657056057490261407972968652414535100474 + 82166370484403199890008895243450658541227588666881 + 16427171479924442928230863465674813919123162824586 + 17866458359124566529476545682848912883142607690042 + 24219022671055626321111109370544217506941658960408 + 07198403850962455444362981230987879927244284909188 + 84580156166097919133875499200524063689912560717606 + 05886116467109405077541002256983155200055935729725 + 71636269561882670428252483600823257530420752963450 Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=8 """ + import sys N = """73167176531330624919225119674426574742355349194934\ @@ -53,13 +59,15 @@ def str_eval(s: str) -> int: - """Returns product of digits in given string n + """ + Returns product of digits in given string n >>> str_eval("987654321") 362880 >>> str_eval("22222222") 256 """ + product = 1 for digit in s: product *= int(digit) @@ -67,12 +75,11 @@ def str_eval(s: str) -> int: def solution(n: str = N) -> int: - """Find the thirteen adjacent digits in the 1000-digit number n that have + """ + Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. - - >>> solution(N) - 23514624000 """ + largest_product = -sys.maxsize - 1 substr = n[:13] cur_index = 13 diff --git a/project_euler/problem_009/sol1.py b/project_euler/problem_009/sol1.py index 1ab3376cae33..76a5c5aa73ec 100644 --- a/project_euler/problem_009/sol1.py +++ b/project_euler/problem_009/sol1.py @@ -1,26 +1,37 @@ """ -Problem 9: https://projecteuler.net/problem=9 +Special Pythagorean triplet +Problem 9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + a^2 + b^2 = c^2 + For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. -Find the product abc. +Find the product a*b*c. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=9 + - Wikipedia page for Pythagorean triples: + https://en.wikipedia.org/wiki/Pythagorean_triple """ def solution() -> int: """ - Returns the product of a,b,c which are Pythagorean Triplet that satisfies - the following: - 1. a < b < c - 2. a**2 + b**2 = c**2 - 3. a + b + c = 1000 + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = 1000 + # The code below has been commented due to slow execution affecting Travis. # >>> solution() # 31875000 """ + for a in range(300): for b in range(400): for c in range(500): @@ -32,16 +43,17 @@ def solution() -> int: def solution_fast() -> int: """ - Returns the product of a,b,c which are Pythagorean Triplet that satisfies - the following: - 1. a < b < c - 2. a**2 + b**2 = c**2 - 3. a + b + c = 1000 + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = 1000 # The code below has been commented due to slow execution affecting Travis. # >>> solution_fast() # 31875000 """ + for a in range(300): for b in range(400): c = 1000 - a - b diff --git a/project_euler/problem_009/sol2.py b/project_euler/problem_009/sol2.py index e22ed45e8644..6fec5e64935d 100644 --- a/project_euler/problem_009/sol2.py +++ b/project_euler/problem_009/sol2.py @@ -1,30 +1,42 @@ """ -Problem 9: https://projecteuler.net/problem=9 +Special Pythagorean triplet +Problem 9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + a^2 + b^2 = c^2 + For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. -Find the product abc. +Find the product a*b*c. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=9 + - Wikipedia page for Pythagorean triples: + https://en.wikipedia.org/wiki/Pythagorean_triple """ def solution(n: int = 1000) -> int: """ - Return the product of a,b,c which are Pythagorean Triplet that satisfies - the following: - 1. a < b < c - 2. a**2 + b**2 = c**2 - 3. a + b + c = n - - >>> solution(1000) - 31875000 + Return the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = n + + >>> solution(36) + 1620 + >>> solution(126) + 66780 """ + product = -1 candidate = 0 for a in range(1, n // 3): - """Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c""" + # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c b = (n * n - 2 * a * n) // (2 * n - 2 * a) c = n - a - b if c * c == (a * a + b * b): diff --git a/project_euler/problem_009/sol3.py b/project_euler/problem_009/sol3.py index 0900a76e6c56..5eac19b3a4fd 100644 --- a/project_euler/problem_009/sol3.py +++ b/project_euler/problem_009/sol3.py @@ -1,5 +1,6 @@ """ -Problem 9: https://projecteuler.net/problem=9 +Special Pythagorean triplet +Problem 9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, @@ -8,22 +9,28 @@ For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. -Find the product abc. +Find the product a*b*c. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=9 + - Wikipedia page for Pythagorean triples: + https://en.wikipedia.org/wiki/Pythagorean_triple """ def solution() -> int: """ - Returns the product of a,b,c which are Pythagorean Triplet that satisfies - the following: - - 1. a**2 + b**2 = c**2 - 2. a + b + c = 1000 + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a**2 + b**2 = c**2 + 2. a + b + c = 1000 # The code below has been commented due to slow execution affecting Travis. # >>> solution() # 31875000 """ + return [ a * b * (1000 - a - b) for a in range(1, 999) diff --git a/project_euler/problem_010/sol1.py b/project_euler/problem_010/sol1.py index 4f3b3a4a42f5..181e24eac6e6 100644 --- a/project_euler/problem_010/sol1.py +++ b/project_euler/problem_010/sol1.py @@ -1,16 +1,25 @@ """ -https://projecteuler.net/problem=10 +Summation of primes +Problem 10 -Problem Statement: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=10 + - Wikipedia page for prime numbers: + https://en.wikipedia.org/wiki/Prime_number """ + from math import sqrt def is_prime(n: int) -> bool: - """Returns boolean representing primality of given number num. + """ + Returns boolean representing primality of given number num. + >>> is_prime(2) True >>> is_prime(3) @@ -20,6 +29,7 @@ def is_prime(n: int) -> bool: >>> is_prime(2999) True """ + for i in range(2, int(sqrt(n)) + 1): if n % i == 0: return False @@ -28,11 +38,9 @@ def is_prime(n: int) -> bool: def solution(n: int = 2000000) -> int: - """Returns the sum of all the primes below n. + """ + Returns the sum of all the primes below n. - # The code below has been commented due to slow execution affecting Travis. - # >>> solution(2000000) - # 142913828922 >>> solution(1000) 76127 >>> solution(5000) @@ -42,6 +50,7 @@ def solution(n: int = 2000000) -> int: >>> solution(7) 10 """ + if n > 2: sum_of_primes = 2 else: diff --git a/project_euler/problem_010/sol2.py b/project_euler/problem_010/sol2.py index 39f5f5604053..615d9adf1541 100644 --- a/project_euler/problem_010/sol2.py +++ b/project_euler/problem_010/sol2.py @@ -1,18 +1,27 @@ """ -https://projecteuler.net/problem=10 +Summation of primes +Problem 10 -Problem Statement: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=10 + - Wikipedia page for prime numbers: + https://en.wikipedia.org/wiki/Prime_number """ + import math from itertools import takewhile from typing import Iterator def is_prime(number: int) -> bool: - """Returns boolean representing primality of given number num. + """ + Returns boolean representing primality of given number num. + >>> is_prime(2) True >>> is_prime(3) @@ -22,12 +31,17 @@ def is_prime(number: int) -> bool: >>> is_prime(2999) True """ + if number % 2 == 0 and number > 2: return False return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) def prime_generator() -> Iterator[int]: + """ + Generate a list sequence of prime numbers + """ + num = 2 while True: if is_prime(num): @@ -36,11 +50,9 @@ def prime_generator() -> Iterator[int]: def solution(n: int = 2000000) -> int: - """Returns the sum of all the primes below n. + """ + Returns the sum of all the primes below n. - # The code below has been commented due to slow execution affecting Travis. - # >>> solution(2000000) - # 142913828922 >>> solution(1000) 76127 >>> solution(5000) @@ -50,6 +62,7 @@ def solution(n: int = 2000000) -> int: >>> solution(7) 10 """ + return sum(takewhile(lambda x: x < n, prime_generator())) diff --git a/project_euler/problem_010/sol3.py b/project_euler/problem_010/sol3.py index ef895f546fa5..6deb7763e510 100644 --- a/project_euler/problem_010/sol3.py +++ b/project_euler/problem_010/sol3.py @@ -1,43 +1,50 @@ """ -https://projecteuler.net/problem=10 +Summation of primes +Problem 10 -Problem Statement: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. + +References: + - The Project Euler problem page: + https://projecteuler.net/problem=10 + - Wikipedia page for prime numbers: + https://en.wikipedia.org/wiki/Prime_number + - Wikipedia page for Sieve of Erathostenes + https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes """ def solution(n: int = 2000000) -> int: - """Returns the sum of all the primes below n using Sieve of Eratosthenes: + """ + Returns the sum of all the primes below n using Sieve of Eratosthenes: - https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million. Only for positive numbers. - >>> solution(2_000_000) - 142913828922 - >>> solution(1_000) + >>> solution(1000) 76127 - >>> solution(5_000) + >>> solution(5000) 1548136 - >>> solution(10_000) + >>> solution(10000) 5736396 >>> solution(7) 10 - >>> solution(7.1) # doctest: +ELLIPSIS + >>> solution(7.1) # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: 'float' object cannot be interpreted as an integer - >>> solution(-7) # doctest: +ELLIPSIS + >>> solution(-7) # doctest: +ELLIPSIS Traceback (most recent call last): ... IndexError: list assignment index out of range - >>> solution("seven") # doctest: +ELLIPSIS + >>> solution("seven") # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: can only concatenate str (not "int") to str """ + primality_list = [0 for i in range(n + 1)] primality_list[0] = 1 primality_list[1] = 1 From 05cf59dec5dd4a7980e61f57b7a5f79d494a9dc5 Mon Sep 17 00:00:00 2001 From: Michael D Date: Sat, 17 Oct 2020 00:00:02 +0200 Subject: [PATCH 2/2] Fix docstrings and main function as per Style Guide --- project_euler/problem_001/sol1.py | 9 +++------ project_euler/problem_001/sol2.py | 9 +++------ project_euler/problem_001/sol3.py | 9 +++------ project_euler/problem_001/sol4.py | 9 +++------ project_euler/problem_001/sol5.py | 9 +++------ project_euler/problem_001/sol6.py | 9 +++------ project_euler/problem_001/sol7.py | 9 +++------ project_euler/problem_002/sol1.py | 10 ++++------ project_euler/problem_002/sol2.py | 10 ++++------ project_euler/problem_002/sol3.py | 10 ++++------ project_euler/problem_002/sol4.py | 11 ++++------- project_euler/problem_002/sol5.py | 10 ++++------ project_euler/problem_003/sol1.py | 11 ++++------- project_euler/problem_003/sol2.py | 10 ++++------ project_euler/problem_003/sol3.py | 10 ++++------ project_euler/problem_004/sol1.py | 14 ++++---------- project_euler/problem_004/sol2.py | 10 ++++------ project_euler/problem_005/sol1.py | 10 ++++------ project_euler/problem_005/sol2.py | 16 ++++++---------- project_euler/problem_006/sol1.py | 12 +++--------- project_euler/problem_006/sol2.py | 12 +++--------- project_euler/problem_006/sol3.py | 13 +++---------- project_euler/problem_006/sol4.py | 12 +++--------- project_euler/problem_007/sol1.py | 10 ++++------ project_euler/problem_007/sol2.py | 10 ++++------ project_euler/problem_007/sol3.py | 11 ++++------- project_euler/problem_008/sol1.py | 9 +++------ project_euler/problem_008/sol2.py | 10 +++------- project_euler/problem_008/sol3.py | 10 +++------- project_euler/problem_009/sol1.py | 10 ++++------ project_euler/problem_009/sol2.py | 10 ++++------ project_euler/problem_009/sol3.py | 10 ++++------ project_euler/problem_010/sol1.py | 10 ++++------ project_euler/problem_010/sol2.py | 11 ++++------- project_euler/problem_010/sol3.py | 13 +++++-------- 35 files changed, 129 insertions(+), 239 deletions(-) diff --git a/project_euler/problem_001/sol1.py b/project_euler/problem_001/sol1.py index 3ffeed31d1dc..85ad32294c9b 100644 --- a/project_euler/problem_001/sol1.py +++ b/project_euler/problem_001/sol1.py @@ -1,15 +1,12 @@ """ +Project Euler Problem 1: https://projecteuler.net/problem=1 + Multiples of 3 and 5 -Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=1 """ @@ -33,4 +30,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol2.py b/project_euler/problem_001/sol2.py index cd77785b1e0a..7093d3513378 100644 --- a/project_euler/problem_001/sol2.py +++ b/project_euler/problem_001/sol2.py @@ -1,15 +1,12 @@ """ +Project Euler Problem 1: https://projecteuler.net/problem=1 + Multiples of 3 and 5 -Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=1 """ @@ -38,4 +35,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol3.py b/project_euler/problem_001/sol3.py index 602e70119bc9..8267fec84155 100644 --- a/project_euler/problem_001/sol3.py +++ b/project_euler/problem_001/sol3.py @@ -1,15 +1,12 @@ """ +Project Euler Problem 1: https://projecteuler.net/problem=1 + Multiples of 3 and 5 -Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=1 """ @@ -64,4 +61,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol4.py b/project_euler/problem_001/sol4.py index 846e87b1221f..a0643c05b34f 100644 --- a/project_euler/problem_001/sol4.py +++ b/project_euler/problem_001/sol4.py @@ -1,15 +1,12 @@ """ +Project Euler Problem 1: https://projecteuler.net/problem=1 + Multiples of 3 and 5 -Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=1 """ @@ -52,4 +49,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol5.py b/project_euler/problem_001/sol5.py index 82bc43a348bb..7f0b0bd1bc7c 100644 --- a/project_euler/problem_001/sol5.py +++ b/project_euler/problem_001/sol5.py @@ -1,15 +1,12 @@ """ +Project Euler Problem 1: https://projecteuler.net/problem=1 + Multiples of 3 and 5 -Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=1 """ @@ -32,4 +29,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol6.py b/project_euler/problem_001/sol6.py index 05f551c0b9d7..8ddce18ced04 100644 --- a/project_euler/problem_001/sol6.py +++ b/project_euler/problem_001/sol6.py @@ -1,15 +1,12 @@ """ +Project Euler Problem 1: https://projecteuler.net/problem=1 + Multiples of 3 and 5 -Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=1 """ @@ -39,4 +36,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol7.py b/project_euler/problem_001/sol7.py index e43ab958ac62..8f5d1977fdde 100644 --- a/project_euler/problem_001/sol7.py +++ b/project_euler/problem_001/sol7.py @@ -1,15 +1,12 @@ """ +Project Euler Problem 1: https://projecteuler.net/problem=1 + Multiples of 3 and 5 -Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=1 """ @@ -37,4 +34,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol1.py b/project_euler/problem_002/sol1.py index 9e6c5aaf880b..539f68fb6bc1 100644 --- a/project_euler/problem_002/sol1.py +++ b/project_euler/problem_002/sol1.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 2: https://projecteuler.net/problem=2 + Even Fibonacci Numbers -Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: @@ -11,10 +12,7 @@ four million, find the sum of the even-valued terms. References: - - The Project Euler problem page: - https://projecteuler.net/problem=2 - - Wikipedia page for Fibonacci Numbers: - https://en.wikipedia.org/wiki/Fibonacci_number + - https://en.wikipedia.org/wiki/Fibonacci_number """ @@ -47,4 +45,4 @@ def solution(n: int = 4000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol2.py b/project_euler/problem_002/sol2.py index 20f4e6a7710e..9033d0a69bcf 100644 --- a/project_euler/problem_002/sol2.py +++ b/project_euler/problem_002/sol2.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 2: https://projecteuler.net/problem=2 + Even Fibonacci Numbers -Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: @@ -11,10 +12,7 @@ four million, find the sum of the even-valued terms. References: - - The Project Euler problem page: - https://projecteuler.net/problem=2 - - Wikipedia page for Fibonacci Numbers: - https://en.wikipedia.org/wiki/Fibonacci_number + - https://en.wikipedia.org/wiki/Fibonacci_number """ @@ -45,4 +43,4 @@ def solution(n: int = 4000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol3.py b/project_euler/problem_002/sol3.py index c87a8f3aeaaf..3ae175a99815 100644 --- a/project_euler/problem_002/sol3.py +++ b/project_euler/problem_002/sol3.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 2: https://projecteuler.net/problem=2 + Even Fibonacci Numbers -Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: @@ -11,10 +12,7 @@ four million, find the sum of the even-valued terms. References: - - The Project Euler problem page: - https://projecteuler.net/problem=2 - - Wikipedia page for Fibonacci Numbers: - https://en.wikipedia.org/wiki/Fibonacci_number + - https://en.wikipedia.org/wiki/Fibonacci_number """ @@ -47,4 +45,4 @@ def solution(n: int = 4000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol4.py b/project_euler/problem_002/sol4.py index 17dec6b7aee6..70b7d6a80a1d 100644 --- a/project_euler/problem_002/sol4.py +++ b/project_euler/problem_002/sol4.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 2: https://projecteuler.net/problem=2 + Even Fibonacci Numbers -Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: @@ -11,12 +12,8 @@ four million, find the sum of the even-valued terms. References: - - The Project Euler problem page: - https://projecteuler.net/problem=2 - - Wikipedia page for Fibonacci Numbers: - https://en.wikipedia.org/wiki/Fibonacci_number + - https://en.wikipedia.org/wiki/Fibonacci_number """ - import math from decimal import Decimal, getcontext @@ -72,4 +69,4 @@ def solution(n: int = 4000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol5.py b/project_euler/problem_002/sol5.py index 04a59f19b778..390fd19ef638 100644 --- a/project_euler/problem_002/sol5.py +++ b/project_euler/problem_002/sol5.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 2: https://projecteuler.net/problem=2 + Even Fibonacci Numbers -Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: @@ -11,10 +12,7 @@ four million, find the sum of the even-valued terms. References: - - The Project Euler problem page: - https://projecteuler.net/problem=2 - - Wikipedia page for Fibonacci Numbers: - https://en.wikipedia.org/wiki/Fibonacci_number + - https://en.wikipedia.org/wiki/Fibonacci_number """ @@ -51,4 +49,4 @@ def solution(n: int = 4000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_003/sol1.py b/project_euler/problem_003/sol1.py index ec6ccef43c40..3441dbf9e0b3 100644 --- a/project_euler/problem_003/sol1.py +++ b/project_euler/problem_003/sol1.py @@ -1,18 +1,15 @@ """ +Project Euler Problem 3: https://projecteuler.net/problem=3 + Largest prime factor -Problem 3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? References: - - The Project Euler problem page: - https://projecteuler.net/problem=3 - - Wikipedia page for Prime factors - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization + - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization """ - import math @@ -104,4 +101,4 @@ def solution(n: int = 600851475143) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_003/sol2.py b/project_euler/problem_003/sol2.py index 58aff987df2c..0af0daceed06 100644 --- a/project_euler/problem_003/sol2.py +++ b/project_euler/problem_003/sol2.py @@ -1,16 +1,14 @@ """ +Project Euler Problem 3: https://projecteuler.net/problem=3 + Largest prime factor -Problem 3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? References: - - The Project Euler problem page: - https://projecteuler.net/problem=3 - - Wikipedia page for Prime factors - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization + - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization """ @@ -63,4 +61,4 @@ def solution(n: int = 600851475143) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_003/sol3.py b/project_euler/problem_003/sol3.py index 6227c243e219..bc6f1d2f61ca 100644 --- a/project_euler/problem_003/sol3.py +++ b/project_euler/problem_003/sol3.py @@ -1,16 +1,14 @@ """ +Project Euler Problem 3: https://projecteuler.net/problem=3 + Largest prime factor -Problem 3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? References: - - The Project Euler problem page: - https://projecteuler.net/problem=3 - - Wikipedia page for Prime factors - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization + - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization """ @@ -65,4 +63,4 @@ def solution(n: int = 600851475143) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_004/sol1.py b/project_euler/problem_004/sol1.py index 4e60a64fb15a..db6133a1a1d2 100644 --- a/project_euler/problem_004/sol1.py +++ b/project_euler/problem_004/sol1.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 4: https://projecteuler.net/problem=4 + Largest palindrome product -Problem 4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. @@ -8,10 +9,7 @@ Find the largest palindrome made from the product of two 3-digit numbers. References: - - The Project Euler problem page: - https://projecteuler.net/problem=4 - - Wikipedia page for palindromic numbers: - https://en.wikipedia.org/wiki/Palindromic_number + - https://en.wikipedia.org/wiki/Palindromic_number """ @@ -52,8 +50,4 @@ def solution(n: int = 998001) -> int: if __name__ == "__main__": - import doctest - - doctest.testmod() - - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_004/sol2.py b/project_euler/problem_004/sol2.py index 412c59a9b784..abc880966d58 100644 --- a/project_euler/problem_004/sol2.py +++ b/project_euler/problem_004/sol2.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 4: https://projecteuler.net/problem=4 + Largest palindrome product -Problem 4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. @@ -8,10 +9,7 @@ Find the largest palindrome made from the product of two 3-digit numbers. References: - - The Project Euler problem page: - https://projecteuler.net/problem=4 - - Wikipedia page for palindromic numbers: - https://en.wikipedia.org/wiki/Palindromic_number + - https://en.wikipedia.org/wiki/Palindromic_number """ @@ -38,4 +36,4 @@ def solution(n: int = 998001) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_005/sol1.py b/project_euler/problem_005/sol1.py index 1848caafe672..f272c102d2bb 100644 --- a/project_euler/problem_005/sol1.py +++ b/project_euler/problem_005/sol1.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 5: https://projecteuler.net/problem=5 + Smallest multiple -Problem 5 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. @@ -9,10 +10,7 @@ of the numbers from 1 to 20? References: - - The Project Euler problem page: - https://projecteuler.net/problem=5 - - Definition of 'evenly divisible': - https://en.wiktionary.org/wiki/evenly_divisible + - https://en.wiktionary.org/wiki/evenly_divisible """ @@ -68,4 +66,4 @@ def solution(n: int = 20) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_005/sol2.py b/project_euler/problem_005/sol2.py index 80fdc0293a3c..c88044487d20 100644 --- a/project_euler/problem_005/sol2.py +++ b/project_euler/problem_005/sol2.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 5: https://projecteuler.net/problem=5 + Smallest multiple -Problem 5 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. @@ -9,14 +10,9 @@ of the numbers from 1 to 20? References: - - The Project Euler problem page: - https://projecteuler.net/problem=5 - - Definition of 'evenly divisible': - https://en.wiktionary.org/wiki/evenly_divisible - - Wikipedia page for the Euklidean algorithm for the GCD: - https://en.wikipedia.org/wiki/Euclidean_algorithm - - Wikipedia page for the Least common Multiple (LCM): - https://en.wikipedia.org/wiki/Least_common_multiple + - https://en.wiktionary.org/wiki/evenly_divisible + - https://en.wikipedia.org/wiki/Euclidean_algorithm + - https://en.wikipedia.org/wiki/Least_common_multiple """ @@ -76,4 +72,4 @@ def solution(n: int = 20) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_006/sol1.py b/project_euler/problem_006/sol1.py index 72abf7ff7b6f..61dd7a321011 100644 --- a/project_euler/problem_006/sol1.py +++ b/project_euler/problem_006/sol1.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 6: https://projecteuler.net/problem=6 + Sum square difference -Problem 6 The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 @@ -13,10 +14,6 @@ Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=6 """ @@ -44,7 +41,4 @@ def solution(n: int = 100) -> int: if __name__ == "__main__": - import doctest - - doctest.testmod() - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_006/sol2.py b/project_euler/problem_006/sol2.py index 08d7aaad4308..cd1bc5071e0e 100644 --- a/project_euler/problem_006/sol2.py +++ b/project_euler/problem_006/sol2.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 6: https://projecteuler.net/problem=6 + Sum square difference -Problem 6 The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 @@ -13,10 +14,6 @@ Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=6 """ @@ -41,7 +38,4 @@ def solution(n: int = 100) -> int: if __name__ == "__main__": - import doctest - - doctest.testmod() - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_006/sol3.py b/project_euler/problem_006/sol3.py index cbd23fccc9d5..c87931309574 100644 --- a/project_euler/problem_006/sol3.py +++ b/project_euler/problem_006/sol3.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 6: https://projecteuler.net/problem=6 + Sum square difference -Problem 6 The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 @@ -13,12 +14,7 @@ Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=6 """ - import math @@ -43,7 +39,4 @@ def solution(n: int = 100) -> int: if __name__ == "__main__": - import doctest - - doctest.testmod() - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_006/sol4.py b/project_euler/problem_006/sol4.py index 6693e7603192..748b141490a0 100644 --- a/project_euler/problem_006/sol4.py +++ b/project_euler/problem_006/sol4.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 6: https://projecteuler.net/problem=6 + Sum square difference -Problem 6 The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 @@ -13,10 +14,6 @@ Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=6 """ @@ -41,7 +38,4 @@ def solution(n: int = 100) -> int: if __name__ == "__main__": - import doctest - - doctest.testmod() - print(solution(int(input("Enter a number: ").strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_007/sol1.py b/project_euler/problem_007/sol1.py index c3da5a4e5f0a..78fbcb511611 100644 --- a/project_euler/problem_007/sol1.py +++ b/project_euler/problem_007/sol1.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 7: https://projecteuler.net/problem=7 + 10001st prime -Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. @@ -8,10 +9,7 @@ What is the 10001st prime number? References: - - The Project Euler problem page: - https://projecteuler.net/problem=7 - - Wikipedia page for prime numbers: - https://en.wikipedia.org/wiki/Prime_number + - https://en.wikipedia.org/wiki/Prime_number """ from math import sqrt @@ -75,4 +73,4 @@ def solution(nth: int = 10001) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_007/sol2.py b/project_euler/problem_007/sol2.py index 419046ed7d7a..b395c631b766 100644 --- a/project_euler/problem_007/sol2.py +++ b/project_euler/problem_007/sol2.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 7: https://projecteuler.net/problem=7 + 10001st prime -Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. @@ -8,10 +9,7 @@ What is the 10001st prime number? References: - - The Project Euler problem page: - https://projecteuler.net/problem=7 - - Wikipedia page for prime numbers: - https://en.wikipedia.org/wiki/Prime_number + - https://en.wikipedia.org/wiki/Prime_number """ @@ -87,4 +85,4 @@ def solution(nth: int = 10001) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_007/sol3.py b/project_euler/problem_007/sol3.py index 39893897da9c..7911fa3e9d6f 100644 --- a/project_euler/problem_007/sol3.py +++ b/project_euler/problem_007/sol3.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 7: https://projecteuler.net/problem=7 + 10001st prime -Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. @@ -8,12 +9,8 @@ What is the 10001st prime number? References: - - The Project Euler problem page: - https://projecteuler.net/problem=7 - - Wikipedia page for prime numbers: - https://en.wikipedia.org/wiki/Prime_number + - https://en.wikipedia.org/wiki/Prime_number """ - import itertools import math @@ -68,4 +65,4 @@ def solution(nth: int = 10001) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_008/sol1.py b/project_euler/problem_008/sol1.py index d9bcab68d5f2..796080127778 100644 --- a/project_euler/problem_008/sol1.py +++ b/project_euler/problem_008/sol1.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 8: https://projecteuler.net/problem=8 + Largest product in a series -Problem 8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. @@ -28,10 +29,6 @@ Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=8 """ import sys @@ -82,4 +79,4 @@ def solution(n: str = N) -> int: if __name__ == "__main__": - print(solution(N)) + print(f"{solution() = }") diff --git a/project_euler/problem_008/sol2.py b/project_euler/problem_008/sol2.py index a9bf2a2244ab..d2c1b4f7ca48 100644 --- a/project_euler/problem_008/sol2.py +++ b/project_euler/problem_008/sol2.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 8: https://projecteuler.net/problem=8 + Largest product in a series -Problem 8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. @@ -28,12 +29,7 @@ Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=8 """ - from functools import reduce N = ( @@ -82,4 +78,4 @@ def solution(n: str = N) -> int: if __name__ == "__main__": - print(solution(str(N))) + print(f"{solution() = }") diff --git a/project_euler/problem_008/sol3.py b/project_euler/problem_008/sol3.py index 357b2f0e36a5..4b99d0ea6e76 100644 --- a/project_euler/problem_008/sol3.py +++ b/project_euler/problem_008/sol3.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 8: https://projecteuler.net/problem=8 + Largest product in a series -Problem 8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. @@ -28,12 +29,7 @@ Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? - -References: - - The Project Euler problem page: - https://projecteuler.net/problem=8 """ - import sys N = """73167176531330624919225119674426574742355349194934\ @@ -95,4 +91,4 @@ def solution(n: str = N) -> int: if __name__ == "__main__": - print(solution(N)) + print(f"{solution() = }") diff --git a/project_euler/problem_009/sol1.py b/project_euler/problem_009/sol1.py index 76a5c5aa73ec..a58ea943e48b 100644 --- a/project_euler/problem_009/sol1.py +++ b/project_euler/problem_009/sol1.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 9: https://projecteuler.net/problem=9 + Special Pythagorean triplet -Problem 9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, @@ -12,10 +13,7 @@ Find the product a*b*c. References: - - The Project Euler problem page: - https://projecteuler.net/problem=9 - - Wikipedia page for Pythagorean triples: - https://en.wikipedia.org/wiki/Pythagorean_triple + - https://en.wikipedia.org/wiki/Pythagorean_triple """ @@ -78,4 +76,4 @@ def benchmark() -> None: if __name__ == "__main__": - benchmark() + print(f"{solution() = }") diff --git a/project_euler/problem_009/sol2.py b/project_euler/problem_009/sol2.py index 6fec5e64935d..722ad522ee45 100644 --- a/project_euler/problem_009/sol2.py +++ b/project_euler/problem_009/sol2.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 9: https://projecteuler.net/problem=9 + Special Pythagorean triplet -Problem 9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, @@ -12,10 +13,7 @@ Find the product a*b*c. References: - - The Project Euler problem page: - https://projecteuler.net/problem=9 - - Wikipedia page for Pythagorean triples: - https://en.wikipedia.org/wiki/Pythagorean_triple + - https://en.wikipedia.org/wiki/Pythagorean_triple """ @@ -47,4 +45,4 @@ def solution(n: int = 1000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_009/sol3.py b/project_euler/problem_009/sol3.py index 5eac19b3a4fd..03aed4b70761 100644 --- a/project_euler/problem_009/sol3.py +++ b/project_euler/problem_009/sol3.py @@ -1,6 +1,7 @@ """ +Project Euler Problem 9: https://projecteuler.net/problem=9 + Special Pythagorean triplet -Problem 9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, @@ -12,10 +13,7 @@ Find the product a*b*c. References: - - The Project Euler problem page: - https://projecteuler.net/problem=9 - - Wikipedia page for Pythagorean triples: - https://en.wikipedia.org/wiki/Pythagorean_triple + - https://en.wikipedia.org/wiki/Pythagorean_triple """ @@ -40,4 +38,4 @@ def solution() -> int: if __name__ == "__main__": - print(solution()) + print(f"{solution() = }") diff --git a/project_euler/problem_010/sol1.py b/project_euler/problem_010/sol1.py index 181e24eac6e6..bd49b3523c97 100644 --- a/project_euler/problem_010/sol1.py +++ b/project_euler/problem_010/sol1.py @@ -1,16 +1,14 @@ """ +Project Euler Problem 10: https://projecteuler.net/problem=10 + Summation of primes -Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. References: - - The Project Euler problem page: - https://projecteuler.net/problem=10 - - Wikipedia page for prime numbers: - https://en.wikipedia.org/wiki/Prime_number + - https://en.wikipedia.org/wiki/Prime_number """ from math import sqrt @@ -64,4 +62,4 @@ def solution(n: int = 2000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_010/sol2.py b/project_euler/problem_010/sol2.py index 615d9adf1541..3a2f485dde50 100644 --- a/project_euler/problem_010/sol2.py +++ b/project_euler/problem_010/sol2.py @@ -1,18 +1,15 @@ """ +Project Euler Problem 10: https://projecteuler.net/problem=10 + Summation of primes -Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. References: - - The Project Euler problem page: - https://projecteuler.net/problem=10 - - Wikipedia page for prime numbers: - https://en.wikipedia.org/wiki/Prime_number + - https://en.wikipedia.org/wiki/Prime_number """ - import math from itertools import takewhile from typing import Iterator @@ -67,4 +64,4 @@ def solution(n: int = 2000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }") diff --git a/project_euler/problem_010/sol3.py b/project_euler/problem_010/sol3.py index 6deb7763e510..f49d9393c7af 100644 --- a/project_euler/problem_010/sol3.py +++ b/project_euler/problem_010/sol3.py @@ -1,18 +1,15 @@ """ +Project Euler Problem 10: https://projecteuler.net/problem=10 + Summation of primes -Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. References: - - The Project Euler problem page: - https://projecteuler.net/problem=10 - - Wikipedia page for prime numbers: - https://en.wikipedia.org/wiki/Prime_number - - Wikipedia page for Sieve of Erathostenes - https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + - https://en.wikipedia.org/wiki/Prime_number + - https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes """ @@ -61,4 +58,4 @@ def solution(n: int = 2000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }")