From 401fa07e0d6517ac1db0787589a159ca1833728b Mon Sep 17 00:00:00 2001 From: "joan.rosellr" Date: Wed, 7 Oct 2020 17:00:21 +0200 Subject: [PATCH 1/8] add problem title and link, fix f-string Signed-off-by: joan.rosellr --- project_euler/problem_41/sol1.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index b4c0d842ae25..2c6ed9560a82 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -1,15 +1,15 @@ from __future__ import annotations - from itertools import permutations from math import sqrt """ +Pandigital prime +Problem 41: https://projecteuler.net/problem=41 + We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? -""" -""" All pandigital numbers except for 1, 4 ,7 pandigital numbers are divisible by 3. So we will check only 7 digit panddigital numbers to obtain the largest possible pandigital prime. @@ -51,4 +51,4 @@ def compute_pandigital_primes(n: int) -> list[int]: if __name__ == "__main__": - print(f"{max(compute_pandigital_primes(7)) = }") + print(f"{max(compute_pandigital_primes(7))}") From 1ad597091a1ed1c52aaa4671fc84b30c09478ea1 Mon Sep 17 00:00:00 2001 From: "joan.rosellr" Date: Wed, 7 Oct 2020 17:23:26 +0200 Subject: [PATCH 2/8] fix code style and improve doctests Signed-off-by: joan.rosellr --- project_euler/problem_41/sol1.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index 2c6ed9560a82..9b98135ddda5 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -11,7 +11,7 @@ What is the largest n-digit pandigital prime that exists? All pandigital numbers except for 1, 4 ,7 pandigital numbers are divisible by 3. -So we will check only 7 digit panddigital numbers to obtain the largest possible +So we will check only 7 digit pandigital numbers to obtain the largest possible pandigital prime. """ @@ -37,18 +37,31 @@ def is_prime(n: int) -> bool: def compute_pandigital_primes(n: int) -> list[int]: """ - Returns a list of all n-digit pandigital primes. + Returns a list of all pandigital prime numbers of length n. >>> compute_pandigital_primes(2) [] - >>> max(compute_pandigital_primes(4)) - 4231 - >>> max(compute_pandigital_primes(7)) - 7652413 + >>> compute_pandigital_primes(4) + [1423, 2143, 2341, 4231] """ pandigital_str = "".join(str(i) for i in range(1, n + 1)) perm_list = [int("".join(i)) for i in permutations(pandigital_str, n)] return [num for num in perm_list if is_prime(num)] +def solution(n: int = 7) -> int: + """ + Returns the maximum pandigital prime number of length n + If no pandigitals exist the returned value is zero. + >>> solution(2) + 0 + >>> solution(4) + 4231 + >>> solution(7) + 7652413 + """ + pandigital_primes = compute_pandigital_primes(n) + return max(pandigital_primes) if pandigital_primes else 0 + + if __name__ == "__main__": - print(f"{max(compute_pandigital_primes(7))}") + print(solution(int(input("Enter a number: ").strip()))) From b786b6cd79b6a101b65d7b72ad6638ed3bec6b30 Mon Sep 17 00:00:00 2001 From: "joan.rosellr" Date: Wed, 7 Oct 2020 17:29:44 +0200 Subject: [PATCH 3/8] undo changes to the main call Signed-off-by: joan.rosellr --- project_euler/problem_41/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index 9b98135ddda5..cb97e844e5ae 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -64,4 +64,4 @@ def solution(n: int = 7) -> int: if __name__ == "__main__": - print(solution(int(input("Enter a number: ").strip()))) + print(f"{solution() = }") From f32c4c2d9319c292be554fe4fd74a723e1deaf39 Mon Sep 17 00:00:00 2001 From: "joan.rosellr" Date: Wed, 7 Oct 2020 17:50:22 +0200 Subject: [PATCH 4/8] remove assignment operator in f-string Signed-off-by: joan.rosellr --- project_euler/problem_41/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index cb97e844e5ae..a7d7f0d12b99 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -64,4 +64,4 @@ def solution(n: int = 7) -> int: if __name__ == "__main__": - print(f"{solution() = }") + print(f"{solution()}") From f0cf20cbf9909be60dfccf5f4c55843c294966a6 Mon Sep 17 00:00:00 2001 From: "joan.rosellr" Date: Wed, 7 Oct 2020 17:56:03 +0200 Subject: [PATCH 5/8] add newline after first import to attempt to fix pre-commit workflow Signed-off-by: joan.rosellr --- project_euler/problem_41/sol1.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index a7d7f0d12b99..c8275984e4f2 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -1,4 +1,5 @@ from __future__ import annotations + from itertools import permutations from math import sqrt From 91afa736e466fcbc5b48ea76973887225b0f77dc Mon Sep 17 00:00:00 2001 From: "joan.rosellr" Date: Wed, 7 Oct 2020 18:45:43 +0200 Subject: [PATCH 6/8] undo doctest changes, rename compute_pandigital_primes to solution Signed-off-by: joan.rosellr --- project_euler/problem_41/sol1.py | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index c8275984e4f2..70266e272e61 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -36,33 +36,20 @@ def is_prime(n: int) -> bool: return True -def compute_pandigital_primes(n: int) -> list[int]: +def solution(n: int = 7) -> list[int]: """ Returns a list of all pandigital prime numbers of length n. - >>> compute_pandigital_primes(2) + >>> solution(2) [] - >>> compute_pandigital_primes(4) - [1423, 2143, 2341, 4231] + >>> max(solution(4)) + 4231 + >>> max(solution(7)) + 7652413 """ pandigital_str = "".join(str(i) for i in range(1, n + 1)) perm_list = [int("".join(i)) for i in permutations(pandigital_str, n)] return [num for num in perm_list if is_prime(num)] -def solution(n: int = 7) -> int: - """ - Returns the maximum pandigital prime number of length n - If no pandigitals exist the returned value is zero. - >>> solution(2) - 0 - >>> solution(4) - 4231 - >>> solution(7) - 7652413 - """ - pandigital_primes = compute_pandigital_primes(n) - return max(pandigital_primes) if pandigital_primes else 0 - - if __name__ == "__main__": - print(f"{solution()}") + print(f"{max(solution()) = }") From 0c3c157ebcfcb58253155b96a1f8201ec13a7d6c Mon Sep 17 00:00:00 2001 From: "joan.rosellr" Date: Thu, 8 Oct 2020 08:38:52 +0200 Subject: [PATCH 7/8] update solution to return the actual solution instead of a list Signed-off-by: joan.rosellr --- project_euler/problem_41/sol1.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index 70266e272e61..e75a77b6839b 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -36,20 +36,21 @@ def is_prime(n: int) -> bool: return True -def solution(n: int = 7) -> list[int]: +def solution(n: int = 7) -> int: """ Returns a list of all pandigital prime numbers of length n. >>> solution(2) - [] - >>> max(solution(4)) + 0 + >>> solution(4) 4231 - >>> max(solution(7)) + >>> solution(7) 7652413 """ pandigital_str = "".join(str(i) for i in range(1, n + 1)) perm_list = [int("".join(i)) for i in permutations(pandigital_str, n)] - return [num for num in perm_list if is_prime(num)] + pandigitals = [num for num in perm_list if is_prime(num)] + return max(pandigitals) if pandigitals else 0 if __name__ == "__main__": - print(f"{max(solution()) = }") + print(f"{solution() = }") From 4fd1c894683bd9ffc27c218037aa331a9630da81 Mon Sep 17 00:00:00 2001 From: Dhruv Date: Thu, 8 Oct 2020 13:50:51 +0530 Subject: [PATCH 8/8] Update sol1.py --- project_euler/problem_41/sol1.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_41/sol1.py b/project_euler/problem_41/sol1.py index e75a77b6839b..80ef2125b82a 100644 --- a/project_euler/problem_41/sol1.py +++ b/project_euler/problem_41/sol1.py @@ -1,8 +1,3 @@ -from __future__ import annotations - -from itertools import permutations -from math import sqrt - """ Pandigital prime Problem 41: https://projecteuler.net/problem=41 @@ -15,6 +10,10 @@ So we will check only 7 digit pandigital numbers to obtain the largest possible pandigital prime. """ +from __future__ import annotations + +from itertools import permutations +from math import sqrt def is_prime(n: int) -> bool: @@ -38,7 +37,8 @@ def is_prime(n: int) -> bool: def solution(n: int = 7) -> int: """ - Returns a list of all pandigital prime numbers of length n. + Returns the maximum pandigital prime number of length n. + If there are none, then it will return 0. >>> solution(2) 0 >>> solution(4)