mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
[Project Euler] Fix code style in Problem 41 (#2992)
* add problem title and link, fix f-string Signed-off-by: joan.rosellr <joan.rosellr@gmail.com> * fix code style and improve doctests Signed-off-by: joan.rosellr <joan.rosellr@gmail.com> * undo changes to the main call Signed-off-by: joan.rosellr <joan.rosellr@gmail.com> * remove assignment operator in f-string Signed-off-by: joan.rosellr <joan.rosellr@gmail.com> * add newline after first import to attempt to fix pre-commit workflow Signed-off-by: joan.rosellr <joan.rosellr@gmail.com> * undo doctest changes, rename compute_pandigital_primes to solution Signed-off-by: joan.rosellr <joan.rosellr@gmail.com> * update solution to return the actual solution instead of a list Signed-off-by: joan.rosellr <joan.rosellr@gmail.com> * Update sol1.py Co-authored-by: Dhruv <dhruvmanila@gmail.com>
This commit is contained in:
parent
719c5562d9
commit
7d54056497
|
@ -1,20 +1,20 @@
|
||||||
|
"""
|
||||||
|
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 pandigital numbers to obtain the largest possible
|
||||||
|
pandigital prime.
|
||||||
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from itertools import permutations
|
from itertools import permutations
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
|
||||||
"""
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def is_prime(n: int) -> bool:
|
def is_prime(n: int) -> bool:
|
||||||
"""
|
"""
|
||||||
|
@ -35,20 +35,22 @@ def is_prime(n: int) -> bool:
|
||||||
return True
|
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.
|
Returns the maximum pandigital prime number of length n.
|
||||||
>>> compute_pandigital_primes(2)
|
If there are none, then it will return 0.
|
||||||
[]
|
>>> solution(2)
|
||||||
>>> max(compute_pandigital_primes(4))
|
0
|
||||||
|
>>> solution(4)
|
||||||
4231
|
4231
|
||||||
>>> max(compute_pandigital_primes(7))
|
>>> solution(7)
|
||||||
7652413
|
7652413
|
||||||
"""
|
"""
|
||||||
pandigital_str = "".join(str(i) for i in range(1, n + 1))
|
pandigital_str = "".join(str(i) for i in range(1, n + 1))
|
||||||
perm_list = [int("".join(i)) for i in permutations(pandigital_str, n)]
|
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__":
|
if __name__ == "__main__":
|
||||||
print(f"{max(compute_pandigital_primes(7)) = }")
|
print(f"{solution() = }")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user