Skip to content

Commit 7d54056

Browse files
[Project Euler] Fix code style in Problem 41 (#2992)
* add problem title and link, fix f-string Signed-off-by: joan.rosellr <[email protected]> * fix code style and improve doctests Signed-off-by: joan.rosellr <[email protected]> * undo changes to the main call Signed-off-by: joan.rosellr <[email protected]> * remove assignment operator in f-string Signed-off-by: joan.rosellr <[email protected]> * add newline after first import to attempt to fix pre-commit workflow Signed-off-by: joan.rosellr <[email protected]> * undo doctest changes, rename compute_pandigital_primes to solution Signed-off-by: joan.rosellr <[email protected]> * update solution to return the actual solution instead of a list Signed-off-by: joan.rosellr <[email protected]> * Update sol1.py Co-authored-by: Dhruv <[email protected]>
1 parent 719c556 commit 7d54056

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

Diff for: project_euler/problem_41/sol1.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
from __future__ import annotations
2-
3-
from itertools import permutations
4-
from math import sqrt
5-
61
"""
2+
Pandigital prime
3+
Problem 41: https://projecteuler.net/problem=41
4+
75
We shall say that an n-digit number is pandigital if it makes use of all the digits
86
1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
97
What is the largest n-digit pandigital prime that exists?
10-
"""
118
12-
"""
139
All pandigital numbers except for 1, 4 ,7 pandigital numbers are divisible by 3.
14-
So we will check only 7 digit panddigital numbers to obtain the largest possible
10+
So we will check only 7 digit pandigital numbers to obtain the largest possible
1511
pandigital prime.
1612
"""
13+
from __future__ import annotations
14+
15+
from itertools import permutations
16+
from math import sqrt
1717

1818

1919
def is_prime(n: int) -> bool:
@@ -35,20 +35,22 @@ def is_prime(n: int) -> bool:
3535
return True
3636

3737

38-
def compute_pandigital_primes(n: int) -> list[int]:
38+
def solution(n: int = 7) -> int:
3939
"""
40-
Returns a list of all n-digit pandigital primes.
41-
>>> compute_pandigital_primes(2)
42-
[]
43-
>>> max(compute_pandigital_primes(4))
40+
Returns the maximum pandigital prime number of length n.
41+
If there are none, then it will return 0.
42+
>>> solution(2)
43+
0
44+
>>> solution(4)
4445
4231
45-
>>> max(compute_pandigital_primes(7))
46+
>>> solution(7)
4647
7652413
4748
"""
4849
pandigital_str = "".join(str(i) for i in range(1, n + 1))
4950
perm_list = [int("".join(i)) for i in permutations(pandigital_str, n)]
50-
return [num for num in perm_list if is_prime(num)]
51+
pandigitals = [num for num in perm_list if is_prime(num)]
52+
return max(pandigitals) if pandigitals else 0
5153

5254

5355
if __name__ == "__main__":
54-
print(f"{max(compute_pandigital_primes(7)) = }")
56+
print(f"{solution() = }")

0 commit comments

Comments
 (0)