Skip to content

[Project Euler] Fix code style in Problem 41 #2992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions project_euler/problem_41/sol1.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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
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:
Expand All @@ -35,20 +35,22 @@ def is_prime(n: int) -> bool:
return True


def compute_pandigital_primes(n: int) -> list[int]:
def solution(n: int = 7) -> int:
"""
Returns a list of all n-digit pandigital primes.
>>> compute_pandigital_primes(2)
[]
>>> max(compute_pandigital_primes(4))
Returns the maximum pandigital prime number of length n.
If there are none, then it will return 0.
>>> solution(2)
0
>>> solution(4)
4231
>>> max(compute_pandigital_primes(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(compute_pandigital_primes(7)) = }")
print(f"{solution() = }")