From 98e9d6bdb6ce34dbdf47912803b979c11486d850 Mon Sep 17 00:00:00 2001 From: Michael D Date: Sun, 25 Oct 2020 04:23:16 +0100 Subject: [PATCH] Fix style of the first ten solutions for Project Euler (#3242) * 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) * Fix docstrings and main function as per Style Guide --- project_euler/problem_001/sol1.py | 13 ++-- project_euler/problem_001/sol2.py | 13 ++-- project_euler/problem_001/sol3.py | 10 +++- project_euler/problem_001/sol4.py | 99 ++++++++++++++++--------------- project_euler/problem_001/sol5.py | 15 +++-- project_euler/problem_001/sol6.py | 13 ++-- project_euler/problem_001/sol7.py | 13 ++-- project_euler/problem_002/sol1.py | 25 +++++--- project_euler/problem_002/sol2.py | 85 ++++++++++++++------------ project_euler/problem_002/sol3.py | 21 ++++--- project_euler/problem_002/sol4.py | 37 +++++++----- project_euler/problem_002/sol5.py | 24 +++++--- project_euler/problem_003/sol1.py | 44 ++++++++------ project_euler/problem_003/sol2.py | 32 ++++++---- project_euler/problem_003/sol3.py | 32 ++++++---- project_euler/problem_004/sol1.py | 26 ++++---- 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 | 27 ++++----- project_euler/problem_006/sol2.py | 27 ++++----- project_euler/problem_006/sol3.py | 27 ++++----- project_euler/problem_006/sol4.py | 27 ++++----- project_euler/problem_007/sol1.py | 35 ++++++++--- project_euler/problem_007/sol2.py | 46 ++++++++------ project_euler/problem_007/sol3.py | 35 ++++++++--- project_euler/problem_008/sol1.py | 59 ++++++++++-------- 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 | 32 ++++++---- project_euler/problem_009/sol3.py | 21 ++++--- project_euler/problem_010/sol1.py | 23 ++++--- project_euler/problem_010/sol2.py | 26 +++++--- project_euler/problem_010/sol3.py | 30 ++++++---- 35 files changed, 717 insertions(+), 469 deletions(-) diff --git a/project_euler/problem_001/sol1.py b/project_euler/problem_001/sol1.py index 385bbbbf4..85ad32294 100644 --- a/project_euler/problem_001/sol1.py +++ b/project_euler/problem_001/sol1.py @@ -1,13 +1,18 @@ """ -Problem Statement: +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + 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. """ 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 @@ -25,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 f08f548cb..7093d3513 100644 --- a/project_euler/problem_001/sol2.py +++ b/project_euler/problem_001/sol2.py @@ -1,13 +1,18 @@ """ -Problem Statement: +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + 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. """ 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 @@ -30,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 67cb83faf..8267fec84 100644 --- a/project_euler/problem_001/sol3.py +++ b/project_euler/problem_001/sol3.py @@ -1,8 +1,12 @@ """ -Problem Statement: +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + 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. """ @@ -57,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 77f323695..a0643c05b 100644 --- a/project_euler/problem_001/sol4.py +++ b/project_euler/problem_001/sol4.py @@ -1,47 +1,52 @@ -""" -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()))) +""" +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + +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. +""" + + +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(f"{solution() = }") diff --git a/project_euler/problem_001/sol5.py b/project_euler/problem_001/sol5.py index 256516802..7f0b0bd1b 100644 --- a/project_euler/problem_001/sol5.py +++ b/project_euler/problem_001/sol5.py @@ -1,14 +1,19 @@ """ -Problem Statement: +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + 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. """ 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 @@ -24,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 5f60512a7..8ddce18ce 100644 --- a/project_euler/problem_001/sol6.py +++ b/project_euler/problem_001/sol6.py @@ -1,13 +1,18 @@ """ -Problem Statement: +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + 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. """ 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 @@ -31,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 5761c00f2..8f5d1977f 100644 --- a/project_euler/problem_001/sol7.py +++ b/project_euler/problem_001/sol7.py @@ -1,13 +1,18 @@ """ -Problem Statement: +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + 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. """ 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 @@ -29,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 2acc93b0a..539f68fb6 100644 --- a/project_euler/problem_002/sol1.py +++ b/project_euler/problem_002/sol1.py @@ -1,19 +1,25 @@ """ -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: +Project Euler Problem 2: https://projecteuler.net/problem=2 - 1,2,3,5,8,13,21,34,55,89,.. +Even Fibonacci Numbers + +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: + - 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 +32,7 @@ def solution(n: int = 4000000) -> int: >>> solution(34) 44 """ + i = 1 j = 2 total = 0 @@ -38,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 01fc552b9..9033d0a69 100644 --- a/project_euler/problem_002/sol2.py +++ b/project_euler/problem_002/sol2.py @@ -1,39 +1,46 @@ -""" -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()))) +""" +Project Euler Problem 2: https://projecteuler.net/problem=2 + +Even Fibonacci Numbers + +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: + - 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(f"{solution() = }") diff --git a/project_euler/problem_002/sol3.py b/project_euler/problem_002/sol3.py index 53d8ca6f1..3ae175a99 100644 --- a/project_euler/problem_002/sol3.py +++ b/project_euler/problem_002/sol3.py @@ -1,19 +1,25 @@ """ -Problem: +Project Euler Problem 2: https://projecteuler.net/problem=2 + +Even Fibonacci Numbers + 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: + - 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 +32,7 @@ def solution(n: int = 4000000) -> int: >>> solution(34) 44 """ + if n <= 1: return 0 a = 0 @@ -38,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 a87410b70..70b7d6a80 100644 --- a/project_euler/problem_002/sol4.py +++ b/project_euler/problem_002/sol4.py @@ -1,21 +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: +Project Euler Problem 2: https://projecteuler.net/problem=2 - 1,2,3,5,8,13,21,34,55,89,.. +Even Fibonacci Numbers + +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: + - 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 +38,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) @@ -62,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 dcf6eae85..390fd19ef 100644 --- a/project_euler/problem_002/sol5.py +++ b/project_euler/problem_002/sol5.py @@ -1,19 +1,25 @@ """ -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: +Project Euler Problem 2: https://projecteuler.net/problem=2 - 1,2,3,5,8,13,21,34,55,89,.. +Even Fibonacci Numbers + +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: + - 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 @@ -43,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 22efeb2c4..3441dbf9e 100644 --- a/project_euler/problem_003/sol1.py +++ b/project_euler/problem_003/sol1.py @@ -1,16 +1,22 @@ """ -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor -of a given number N? +Project Euler Problem 3: https://projecteuler.net/problem=3 -e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. +Largest prime factor + +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - 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 +28,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 +48,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 +62,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 @@ -91,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 f28232109..0af0dacee 100644 --- a/project_euler/problem_003/sol2.py +++ b/project_euler/problem_003/sol2.py @@ -1,14 +1,21 @@ """ -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor -of a given number N? +Project Euler Problem 3: https://projecteuler.net/problem=3 -e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. +Largest prime factor + +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - 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 +27,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: @@ -53,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 676717cce..bc6f1d2f6 100644 --- a/project_euler/problem_003/sol3.py +++ b/project_euler/problem_003/sol3.py @@ -1,14 +1,21 @@ """ -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor -of a given number N? +Project Euler Problem 3: https://projecteuler.net/problem=3 -e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. +Largest prime factor + +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - 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 +27,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: @@ -55,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 42f56f3ef..db6133a1a 100644 --- a/project_euler/problem_004/sol1.py +++ b/project_euler/problem_004/sol1.py @@ -1,15 +1,21 @@ """ -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. +Project Euler Problem 4: https://projecteuler.net/problem=4 -Find the largest palindrome made from the product of two 3-digit numbers which -is less than N. +Largest palindrome product + +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: + - 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 +29,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. @@ -44,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 8ee082ad2..abc880966 100644 --- a/project_euler/problem_004/sol2.py +++ b/project_euler/problem_004/sol2.py @@ -1,15 +1,21 @@ """ -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. +Project Euler Problem 4: https://projecteuler.net/problem=4 -Find the largest palindrome made from the product of two 3-digit numbers which -is less than N. +Largest palindrome product + +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: + - 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 +25,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): @@ -29,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 a347d6564..f272c102d 100644 --- a/project_euler/problem_005/sol1.py +++ b/project_euler/problem_005/sol1.py @@ -1,23 +1,28 @@ """ -Problem: -2520 is the smallest number that can be divided by each of the numbers from 1 -to 10 without any remainder. +Project Euler Problem 5: https://projecteuler.net/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? +Smallest multiple + +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: + - 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 +30,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) @@ -60,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 57b4cc823..c88044487 100644 --- a/project_euler/problem_005/sol2.py +++ b/project_euler/problem_005/sol2.py @@ -1,38 +1,70 @@ """ -Problem: -2520 is the smallest number that can be divided by each of the numbers from 1 -to 10 without any remainder. +Project Euler Problem 5: https://projecteuler.net/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? +Smallest multiple + +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: + - https://en.wiktionary.org/wiki/evenly_divisible + - https://en.wikipedia.org/wiki/Euclidean_algorithm + - https://en.wikipedia.org/wiki/Least_common_multiple """ -""" Euclidean GCD Algorithm """ def gcd(x: int, y: int) -> int: + """ + Euclidean GCD algorithm (Greatest Common Divisor) + + >>> gcd(0, 0) + 0 + >>> gcd(23, 42) + 1 + >>> gcd(15, 33) + 3 + >>> gcd(12345, 67890) + 15 + """ + return x if y == 0 else gcd(y, x % y) -""" Using the property lcm*gcd of two numbers = product of them """ - - 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) @@ -40,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 38f995bbf..61dd7a321 100644 --- a/project_euler/problem_006/sol1.py +++ b/project_euler/problem_006/sol1.py @@ -1,22 +1,25 @@ """ -Problem 6: https://projecteuler.net/problem=6 +Project Euler Problem 6: https://projecteuler.net/problem=6 + +Sum square difference 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. """ 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 +30,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): @@ -39,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 f4d74c993..cd1bc5071 100644 --- a/project_euler/problem_006/sol2.py +++ b/project_euler/problem_006/sol2.py @@ -1,22 +1,25 @@ """ -Problem 6: https://projecteuler.net/problem=6 +Project Euler Problem 6: https://projecteuler.net/problem=6 + +Sum square difference 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. """ 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,16 +30,12 @@ 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 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 8b5c5d3ba..c87931309 100644 --- a/project_euler/problem_006/sol3.py +++ b/project_euler/problem_006/sol3.py @@ -1,23 +1,26 @@ """ -Problem 6: https://projecteuler.net/problem=6 +Project Euler Problem 6: https://projecteuler.net/problem=6 + +Sum square difference 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. """ 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,16 +31,12 @@ 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 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 5fae84008..748b14149 100644 --- a/project_euler/problem_006/sol4.py +++ b/project_euler/problem_006/sol4.py @@ -1,22 +1,25 @@ """ -Problem 6: https://projecteuler.net/problem=6 +Project Euler Problem 6: https://projecteuler.net/problem=6 + +Sum square difference 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. """ 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,16 +30,12 @@ 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) 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 727d7fb7f..78fbcb511 100644 --- a/project_euler/problem_007/sol1.py +++ b/project_euler/problem_007/sol1.py @@ -1,17 +1,34 @@ """ -Problem 7: https://projecteuler.net/problem=7 +Project Euler Problem 7: https://projecteuler.net/problem=7 -By listing the first six prime numbers: +10001st prime - 2, 3, 5, 7, 11, and 13 +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. -We can see that the 6th prime is 13. What is the Nth prime number? +What is the 10001st prime number? + +References: + - 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 +42,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 +57,8 @@ def solution(nth: int = 10001) -> int: 229 >>> solution(100) 541 - >>> solution() - 104743 """ + count = 0 number = 1 while count != nth and number < 3: @@ -56,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 62806e1e2..b395c631b 100644 --- a/project_euler/problem_007/sol2.py +++ b/project_euler/problem_007/sol2.py @@ -1,16 +1,30 @@ """ -Problem 7: https://projecteuler.net/problem=7 +Project Euler Problem 7: https://projecteuler.net/problem=7 -By listing the first six prime numbers: +10001st prime - 2, 3, 5, 7, 11, and 13 +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. -We can see that the 6th prime is 13. What is the Nth prime number? +What is the 10001st prime number? + +References: + - 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 +32,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 +47,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: @@ -73,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 1182875c0..7911fa3e9 100644 --- a/project_euler/problem_007/sol3.py +++ b/project_euler/problem_007/sol3.py @@ -1,24 +1,42 @@ """ -Project 7: https://projecteuler.net/problem=7 +Project Euler Problem 7: https://projecteuler.net/problem=7 -By listing the first six prime numbers: +10001st prime - 2, 3, 5, 7, 11, and 13 +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. -We can see that the 6th prime is 13. What is the Nth prime number? +What is the 10001st prime number? + +References: + - 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 +45,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,11 +60,9 @@ def solution(nth: int = 10001) -> int: 229 >>> solution(100) 541 - >>> solution() - 104743 """ return next(itertools.islice(prime_generator(), nth - 1, nth)) 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 db15907b3..796080127 100644 --- a/project_euler/problem_008/sol1.py +++ b/project_euler/problem_008/sol1.py @@ -1,33 +1,36 @@ """ -Problem 8: https://projecteuler.net/problem=8 +Project Euler Problem 8: https://projecteuler.net/problem=8 + +Largest product in a series 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? """ + import sys N = """73167176531330624919225119674426574742355349194934\ @@ -53,12 +56,18 @@ N = """73167176531330624919225119674426574742355349194934\ 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 @@ -70,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 1b338a955..d2c1b4f7c 100644 --- a/project_euler/problem_008/sol2.py +++ b/project_euler/problem_008/sol2.py @@ -1,34 +1,35 @@ """ -Problem 8: https://projecteuler.net/problem=8 +Project Euler Problem 8: https://projecteuler.net/problem=8 + +Largest product in a series 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? """ - from functools import reduce N = ( @@ -56,12 +57,18 @@ N = ( 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]) @@ -71,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 17f68cba5..4b99d0ea6 100644 --- a/project_euler/problem_008/sol3.py +++ b/project_euler/problem_008/sol3.py @@ -1,29 +1,31 @@ """ -Problem 8: https://projecteuler.net/problem=8 +Project Euler Problem 8: https://projecteuler.net/problem=8 + +Largest product in a series 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? @@ -53,13 +55,15 @@ N = """73167176531330624919225119674426574742355349194934\ 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 +71,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 - the greatest product and returns it. - - >>> solution(N) - 23514624000 """ + Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. + """ + largest_product = -sys.maxsize - 1 substr = n[:13] cur_index = 13 @@ -88,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 1ab3376ca..a58ea943e 100644 --- a/project_euler/problem_009/sol1.py +++ b/project_euler/problem_009/sol1.py @@ -1,26 +1,35 @@ """ -Problem 9: https://projecteuler.net/problem=9 +Project Euler Problem 9: https://projecteuler.net/problem=9 + +Special Pythagorean triplet 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: + - 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 +41,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 @@ -66,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 e22ed45e8..722ad522e 100644 --- a/project_euler/problem_009/sol2.py +++ b/project_euler/problem_009/sol2.py @@ -1,30 +1,40 @@ """ -Problem 9: https://projecteuler.net/problem=9 +Project Euler Problem 9: https://projecteuler.net/problem=9 + +Special Pythagorean triplet 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: + - 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 + 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 + >>> 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): @@ -35,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 0900a76e6..03aed4b70 100644 --- a/project_euler/problem_009/sol3.py +++ b/project_euler/problem_009/sol3.py @@ -1,5 +1,7 @@ """ -Problem 9: https://projecteuler.net/problem=9 +Project Euler Problem 9: https://projecteuler.net/problem=9 + +Special Pythagorean triplet A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, @@ -8,22 +10,25 @@ A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, 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: + - 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) @@ -33,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 4f3b3a4a4..bd49b3523 100644 --- a/project_euler/problem_010/sol1.py +++ b/project_euler/problem_010/sol1.py @@ -1,16 +1,23 @@ """ -https://projecteuler.net/problem=10 +Project Euler Problem 10: https://projecteuler.net/problem=10 + +Summation of primes -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: + - 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 +27,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 +36,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 +48,7 @@ def solution(n: int = 2000000) -> int: >>> solution(7) 10 """ + if n > 2: sum_of_primes = 2 else: @@ -55,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 39f5f5604..3a2f485dd 100644 --- a/project_euler/problem_010/sol2.py +++ b/project_euler/problem_010/sol2.py @@ -1,10 +1,14 @@ """ -https://projecteuler.net/problem=10 +Project Euler Problem 10: https://projecteuler.net/problem=10 + +Summation of primes -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: + - https://en.wikipedia.org/wiki/Prime_number """ import math from itertools import takewhile @@ -12,7 +16,9 @@ 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 +28,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 +47,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,8 +59,9 @@ def solution(n: int = 2000000) -> int: >>> solution(7) 10 """ + return sum(takewhile(lambda x: x < n, prime_generator())) 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 ef895f546..f49d9393c 100644 --- a/project_euler/problem_010/sol3.py +++ b/project_euler/problem_010/sol3.py @@ -1,43 +1,47 @@ """ -https://projecteuler.net/problem=10 +Project Euler Problem 10: https://projecteuler.net/problem=10 + +Summation of primes -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: + - https://en.wikipedia.org/wiki/Prime_number + - 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 @@ -54,4 +58,4 @@ def solution(n: int = 2000000) -> int: if __name__ == "__main__": - print(solution(int(input().strip()))) + print(f"{solution() = }")