77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
'''
|
|
By replacing the 1st digit of the 2-digit number *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.
|
|
|
|
By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
|
|
|
|
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
|
|
'''
|
|
|
|
import numpy as np
|
|
import math
|
|
import time
|
|
|
|
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:]
|
|
|
|
# Does generate all families that can be made for that number, so that consists of multiple families together
|
|
def families(num):
|
|
result = []
|
|
variedDigits = set()
|
|
digits = [int(d) for d in str(num)]
|
|
|
|
for i in range(len(digits)):
|
|
if not i in variedDigits:
|
|
buddies = []
|
|
|
|
for j in range(len(digits)):
|
|
if digits[i] == digits[j]:
|
|
buddies.append(j)
|
|
variedDigits.add(j)
|
|
|
|
r = digits[:]
|
|
|
|
for n in range(10):
|
|
for bud in buddies:
|
|
r[bud] = n
|
|
maybeResult = int("".join([str(_r) for _r in r]))
|
|
result.append(maybeResult)
|
|
|
|
return result
|
|
|
|
def findPrimeFamily(length):
|
|
primes = sieve(1000000)
|
|
primeSet = set(primes)
|
|
|
|
for p in primes:
|
|
primeFamilies = families(p)
|
|
|
|
for i in range(len(primeFamilies) // 10):
|
|
counter = 0
|
|
for pf in primeFamilies[10*i:10*i+10]:
|
|
if pf in primeSet and len(str(pf)) == len(str(p)):
|
|
counter += 1
|
|
if counter >= length:
|
|
return [x for x in primeFamilies[10*i:10*i+10] if x in primeSet]
|
|
|
|
def main():
|
|
print("Hello this is Patrick")
|
|
|
|
t0 = time.time()
|
|
|
|
print(findPrimeFamily(8), time.time() - t0)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|