Oof I feel ashamed by having written this code, 41
This commit is contained in:
49
41/main.py
Normal file
49
41/main.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
'''
|
||||||
|
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?
|
||||||
|
'''
|
||||||
|
|
||||||
|
import math
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def sieve(n):
|
||||||
|
assert n > 1
|
||||||
|
|
||||||
|
ns = [True] * n
|
||||||
|
|
||||||
|
for i in range(2, math.ceil(np.sqrt(n))):
|
||||||
|
if ns[i]:
|
||||||
|
j = pow(i, 2)
|
||||||
|
|
||||||
|
while j < n:
|
||||||
|
ns[j] = False
|
||||||
|
j = j + i
|
||||||
|
|
||||||
|
return [i for i,val in enumerate(ns) if val][2:]
|
||||||
|
|
||||||
|
def isPandigital(n):
|
||||||
|
l = len(str(n))
|
||||||
|
|
||||||
|
digits = set(range(1, l+1))
|
||||||
|
|
||||||
|
if digits == set(map(int, str(n))):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("Hello this is Patrick")
|
||||||
|
|
||||||
|
primes = sieve(987654321)
|
||||||
|
|
||||||
|
print("Calculated enough primes")
|
||||||
|
|
||||||
|
r = 1
|
||||||
|
|
||||||
|
while not isPandigital(primes[-1 * r]):
|
||||||
|
r += 1
|
||||||
|
|
||||||
|
print(primes[-1 * r])
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user