-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 51
More file actions
64 lines (56 loc) · 1.88 KB
/
Copy pathproblem 51
File metadata and controls
64 lines (56 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
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.
"""
from collections import Counter
def all_primes(upper_bound):
primes = [2]
for i in range(3, upper_bound):
j = 0
while j < len(primes) and primes[j] <= int(i ** 0.5):
if i % primes[j] == 0:
break
j += 1
else:
primes.append(i)
return primes
def is_prime(x, primes):
sqrt = int(x ** 0.5) + 1
for prime in primes:
if x % prime == 0:
return False
if prime == primes[-1]:
return False
if prime > sqrt:
return True
return True
primes = []
primes = all_primes(1000000)
duplicate_digits_ = [i for i in primes if len(str(i)) - len(set(str(i))) >= 3]
def replacing_digits(num):
families = []
num = str(num)
digits = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
for duplicate in (Counter(num) - Counter(set(num))):
temp = [int(num.replace(duplicate,i)) for i in digits]
families.append(temp)
return families
seen = []
def eight_primes(family):
for i in family:
seen.append(i)
if i not in primes:
family.remove(i)
return family
i = 0
check = 1
while check:
if primes[i] not in seen :
solution = replacing_digits(primes[i])
for k in solution:
if len(eight_primes(k)) == 8:
print(solution)
check = 0
break
i += 1