From 0da5ceafd9867889064e0de59263050e73608625 Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Thu, 20 Feb 2020 18:03:31 +0700 Subject: [PATCH 01/11] added doctests in modular_exponential.py --- dynamic_programming/coin_change.py | 17 +++++++++++++++-- linear_algebra/src/lib.py | 22 ++++++++++++---------- maths/modular_exponential.py | 22 +++++++++++++++++++--- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/dynamic_programming/coin_change.py b/dynamic_programming/coin_change.py index a85d8e08dbb1..2d7106f0cc6f 100644 --- a/dynamic_programming/coin_change.py +++ b/dynamic_programming/coin_change.py @@ -8,6 +8,18 @@ def dp_count(S, m, n): + """ + >>> dp_count([1, 2, 3], 3, 4) + 4 + >>> dp_count([1, 2, 3], 3, 7) + 8 + >>> dp_count([2, 5, 3, 6], 4, 10) + 5 + >>> dp_count([10], 1, 99) + 0 + >>> dp_count([4, 5, 6], 3, 0) + 1 + """ # table[i] represents the number of ways to get to amount i table = [0] * (n + 1) @@ -26,5 +38,6 @@ def dp_count(S, m, n): if __name__ == "__main__": - print(dp_count([1, 2, 3], 3, 4)) # answer 4 - print(dp_count([2, 5, 3, 6], 4, 10)) # answer 5 + import doctest + + doctest.testmod() diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 5ce0f696ad71..bf9e0d302a89 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -27,7 +27,7 @@ class Vector(object): """ - This class represents a vector of arbitray size. + This class represents a vector of arbitrary size. You need to give the vector components. Overview about the methods: @@ -37,7 +37,7 @@ class Vector(object): __str__() : toString method component(i : int): gets the i-th component (start by 0) __len__() : gets the size of the vector (number of components) - euclidLength() : returns the eulidean length of the vector. + euclidLength() : returns the euclidean length of the vector. operator + : vector addition operator - : vector subtraction operator * : scalar multiplication and dot product @@ -46,11 +46,13 @@ class Vector(object): TODO: compare-operator """ - def __init__(self, components=[]): + def __init__(self, components=None): """ input: components or nothing simple constructor for init the vector """ + if components is None: + components = [] self.__components = list(components) def set(self, components): @@ -86,9 +88,9 @@ def __len__(self): """ return len(self.__components) - def eulidLength(self): + def euclidLength(self): """ - returns the eulidean length of the vector + returns the euclidean length of the vector """ summe = 0 for c in self.__components: @@ -112,7 +114,7 @@ def __sub__(self, other): """ input: other vector assumes: other vector has the same size - returns a new vector that represents the differenz. + returns a new vector that represents the difference. """ size = len(self) if size == len(other): @@ -136,7 +138,7 @@ def __mul__(self, other): summe += self.__components[i] * other.component(i) return summe else: # error case - raise Exception("invalide operand!") + raise Exception("invalid operand!") def copy(self): """ @@ -223,7 +225,7 @@ class Matrix(object): def __init__(self, matrix, w, h): """ - simple constructor for initialzes + simple constructor for initializing the matrix with components. """ self.__matrix = matrix @@ -249,7 +251,7 @@ def changeComponent(self, x, y, value): """ changes the x-y component of this matrix """ - if x >= 0 and x < self.__height and y >= 0 and y < self.__width: + if 0 <= x < self.__height and 0 <= y < self.__width: self.__matrix[x][y] = value else: raise Exception("changeComponent: indices out of bounds") @@ -258,7 +260,7 @@ def component(self, x, y): """ returns the specified (x,y) component """ - if x >= 0 and x < self.__height and y >= 0 and y < self.__width: + if 0 <= x < self.__height and 0 <= y < self.__width: return self.__matrix[x][y] else: raise Exception("changeComponent: indices out of bounds") diff --git a/maths/modular_exponential.py b/maths/modular_exponential.py index 8715e17147ff..5a3493669a19 100644 --- a/maths/modular_exponential.py +++ b/maths/modular_exponential.py @@ -1,8 +1,19 @@ -"""Modular Exponential.""" - +""" + Modular Exponential. + Modular exponentiation is a type of exponentiation performed over a modulus. +""" +"""Calculate Modular Exponential.""" def modular_exponential(base, power, mod): - """Calculate Modular Exponential.""" + """ + >>> modular_exponential(5, 0, 10) + 1 + >>> modular_exponential(2, 8, 7) + 4 + >>> modular_exponential(3, -2, 9) + -1 + """ + if power < 0: return -1 base %= mod @@ -13,6 +24,7 @@ def modular_exponential(base, power, mod): result = (result * base) % mod power = power >> 1 base = (base * base) % mod + return result @@ -22,4 +34,8 @@ def main(): if __name__ == "__main__": + import doctest + + doctest.testmod() + main() From 4183239405ad4c20b293774ab74a597c082ff714 Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Thu, 20 Feb 2020 18:11:35 +0700 Subject: [PATCH 02/11] added doctests in modular_exponential.py --- maths/modular_exponential.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/modular_exponential.py b/maths/modular_exponential.py index 5a3493669a19..0fd35d65b780 100644 --- a/maths/modular_exponential.py +++ b/maths/modular_exponential.py @@ -4,7 +4,7 @@ """ """Calculate Modular Exponential.""" -def modular_exponential(base, power, mod): +def modular_exponential(base : int, power : int, mod : int): """ >>> modular_exponential(5, 0, 10) 1 From 3856fa064d97ee70cdfd0db0dfdeabc4db1474e2 Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Thu, 20 Feb 2020 18:19:59 +0700 Subject: [PATCH 03/11] added URL link --- maths/modular_exponential.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/modular_exponential.py b/maths/modular_exponential.py index 0fd35d65b780..91fa0e462a49 100644 --- a/maths/modular_exponential.py +++ b/maths/modular_exponential.py @@ -1,6 +1,7 @@ """ Modular Exponential. Modular exponentiation is a type of exponentiation performed over a modulus. + For more explanation, please check https://en.wikipedia.org/wiki/Modular_exponentiation """ """Calculate Modular Exponential.""" From cb8ab389ee70461911c35f8d48bfdf95a972224c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 3 Oct 2020 15:27:05 +0000 Subject: [PATCH 04/11] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 37eaadb89786..9e33eb6c00ba 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -547,6 +547,8 @@ * Problem 11 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_11/sol1.py) * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_11/sol2.py) + * Problem 112 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_112/sol1.py) * Problem 12 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol1.py) * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol2.py) From 7824282d8885db24ab6d92ea8cb2c2f91c651026 Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Sat, 3 Oct 2020 22:27:40 +0700 Subject: [PATCH 05/11] Add problem 50 solution --- project_euler/problem_50/__init__.py | 0 project_euler/problem_50/sol1.py | 100 +++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 project_euler/problem_50/__init__.py create mode 100644 project_euler/problem_50/sol1.py diff --git a/project_euler/problem_50/__init__.py b/project_euler/problem_50/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py new file mode 100644 index 000000000000..c7f21a3baf9c --- /dev/null +++ b/project_euler/problem_50/sol1.py @@ -0,0 +1,100 @@ +from math import floor, sqrt + +""" +Consecutive prime sum + +Problem 50 + +The prime 41, can be written as the sum of six consecutive primes: + +41 = 2 + 3 + 5 + 7 + 11 + 13 + +This is the longest sum of consecutive primes that adds to +a prime below one-hundred. + +The longest sum of consecutive primes below one-thousand that adds to a prime, +contains 21 terms, and is equal to 953. + +Which prime, below one-million, can be written as the sum of +the most consecutive primes? +""" + +""" +Solution: + +First of all, we need to generate all prime numbers +from 2 to the closest prime number with 1000000. +Then, use sliding window to get the answer. +""" + + +def is_prime(number: int) -> bool: + """ + function to check whether a number is a prime or not. + >>> is_prime(2) + True + >>> is_prime(6) + False + >>> is_prime(1) + False + """ + + if number < 2: + return False + + for n in range(2, floor(sqrt(number)) + 1): + if number % n == 0: + return False + + return True + + +def solution(): + """ + Return the problem solution. + >>> solution() + 997651 + """ + + prime_list = [2] + [x for x in range(3, 10 ** 6, 2) if is_prime(x)] + + cumulative_sum = [] + tmp = 0 + for x in prime_list: + tmp += x + if tmp < 10 ** 6: + cumulative_sum.append(tmp) + else: + break + + upper_limit_idx = 0 + for i in range(len(prime_list)): + if prime_list[i] < 10 ** 6: + upper_limit_idx = i + else: + break + + max_count = -1 + answer = 0 + for number in reversed(cumulative_sum): + count_prime = cumulative_sum.index(number) + 1 + + if not is_prime(number): + tmp = number + + for i in range(upper_limit_idx): + count_prime -= 1 + tmp -= prime_list[i] + + if is_prime(tmp) or tmp < 0: + break + + if max_count < count_prime: + max_count = count_prime + answer = tmp + + return answer + + +if __name__ == "__main__": + print(solution()) From c933fe88804f4e89c67c7d942ef2f52048b2e365 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 3 Oct 2020 15:28:26 +0000 Subject: [PATCH 06/11] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e33eb6c00ba..16f0688f957c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -633,6 +633,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_47/sol1.py) * Problem 48 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_48/sol1.py) + * Problem 50 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_50/sol1.py) * Problem 52 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_52/sol1.py) * Problem 53 From a2acaf88184620fd8606ac9ef68b4d10c6a4f9d5 Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Sun, 4 Oct 2020 21:46:55 +0700 Subject: [PATCH 07/11] Fix some mistakes --- project_euler/problem_50/sol1.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py index c7f21a3baf9c..7760ff9c5df4 100644 --- a/project_euler/problem_50/sol1.py +++ b/project_euler/problem_50/sol1.py @@ -17,9 +17,7 @@ Which prime, below one-million, can be written as the sum of the most consecutive primes? -""" -""" Solution: First of all, we need to generate all prime numbers @@ -37,6 +35,10 @@ def is_prime(number: int) -> bool: False >>> is_prime(1) False + >>> is_prime(-7000) + False + >>> is_prime(104729) + True """ if number < 2: @@ -60,8 +62,8 @@ def solution(): cumulative_sum = [] tmp = 0 - for x in prime_list: - tmp += x + for prime in prime_list: + tmp += prime if tmp < 10 ** 6: cumulative_sum.append(tmp) else: From e75158b3b0fde2e424cdb42aa578a9dc79d6b5a0 Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Mon, 5 Oct 2020 08:04:30 +0700 Subject: [PATCH 08/11] Move the import statement lower --- project_euler/problem_50/sol1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py index 7760ff9c5df4..760f49260091 100644 --- a/project_euler/problem_50/sol1.py +++ b/project_euler/problem_50/sol1.py @@ -1,5 +1,3 @@ -from math import floor, sqrt - """ Consecutive prime sum @@ -25,6 +23,8 @@ Then, use sliding window to get the answer. """ +from math import floor, sqrt + def is_prime(number: int) -> bool: """ From eebca1e45af4d6e7708f1e7403e0b629cd93bc42 Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Sun, 11 Oct 2020 14:56:30 +0700 Subject: [PATCH 09/11] Add default argument into main function --- project_euler/problem_50/sol1.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py index 760f49260091..3febfc68bdd2 100644 --- a/project_euler/problem_50/sol1.py +++ b/project_euler/problem_50/sol1.py @@ -51,27 +51,27 @@ def is_prime(number: int) -> bool: return True -def solution(): +def solution(constraint=10 ** 6): """ Return the problem solution. - >>> solution() - 997651 + >>> solution(1000) + 953 """ - prime_list = [2] + [x for x in range(3, 10 ** 6, 2) if is_prime(x)] + prime_list = [2] + [x for x in range(3, constraint, 2) if is_prime(x)] cumulative_sum = [] tmp = 0 for prime in prime_list: tmp += prime - if tmp < 10 ** 6: + if tmp < constraint: cumulative_sum.append(tmp) else: break upper_limit_idx = 0 for i in range(len(prime_list)): - if prime_list[i] < 10 ** 6: + if prime_list[i] < constraint: upper_limit_idx = i else: break From c45bfe36ecdaf004c7b8b07369426c8641dfc9fc Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Mon, 12 Oct 2020 12:54:04 +0700 Subject: [PATCH 10/11] Improve the code * Add type hint * Add more tests * Add problem link --- project_euler/problem_50/sol1.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py index 3febfc68bdd2..64436a2d436b 100644 --- a/project_euler/problem_50/sol1.py +++ b/project_euler/problem_50/sol1.py @@ -2,6 +2,7 @@ Consecutive prime sum Problem 50 +https://projecteuler.net/problem=50 The prime 41, can be written as the sum of six consecutive primes: @@ -51,11 +52,15 @@ def is_prime(number: int) -> bool: return True -def solution(constraint=10 ** 6): +def solution(constraint: int = 10 ** 6): """ Return the problem solution. + >>> solution(10000) + 9521 >>> solution(1000) 953 + >>> solution(50) + 58 """ prime_list = [2] + [x for x in range(3, constraint, 2) if is_prime(x)] @@ -99,4 +104,4 @@ def solution(constraint=10 ** 6): if __name__ == "__main__": - print(solution()) + print(solution(50)) From e51429f1785481e7cefb7b87ab05760302d66cee Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Mon, 12 Oct 2020 12:58:17 +0700 Subject: [PATCH 11/11] Small fix --- project_euler/problem_50/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py index 64436a2d436b..de5f81ad2b89 100644 --- a/project_euler/problem_50/sol1.py +++ b/project_euler/problem_50/sol1.py @@ -104,4 +104,4 @@ def solution(constraint: int = 10 ** 6): if __name__ == "__main__": - print(solution(50)) + print(solution())