- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathsol1.py
114 lines (82 loc) · 2.95 KB
/
sol1.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
https://projecteuler.net/problem=51
Prime digit replacements
Problem 51
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 __future__ importannotations
fromcollectionsimportCounter
defprime_sieve(n: int) ->list[int]:
"""
Sieve of Erotosthenes
Function to return all the prime numbers up to a certain number
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
>>> prime_sieve(3)
[2]
>>> prime_sieve(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
"""
is_prime= [True] *n
is_prime[0] =False
is_prime[1] =False
is_prime[2] =True
foriinrange(3, int(n**0.5+1), 2):
index=i*2
whileindex<n:
is_prime[index] =False
index=index+i
primes= [2]
foriinrange(3, n, 2):
ifis_prime[i]:
primes.append(i)
returnprimes
defdigit_replacements(number: int) ->list[list[int]]:
"""
Returns all the possible families of digit replacements in a number which
contains at least one repeating digit
>>> digit_replacements(544)
[[500, 511, 522, 533, 544, 555, 566, 577, 588, 599]]
>>> digit_replacements(3112)
[[3002, 3112, 3222, 3332, 3442, 3552, 3662, 3772, 3882, 3992]]
"""
number_str=str(number)
replacements= []
digits= ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
forduplicateinCounter(number_str) -Counter(set(number_str)):
family= [int(number_str.replace(duplicate, digit)) fordigitindigits]
replacements.append(family)
returnreplacements
defsolution(family_length: int=8) ->int:
"""
Returns the solution of the problem
>>> solution(2)
229399
>>> solution(3)
221311
"""
numbers_checked=set()
# Filter primes with less than 3 replaceable digits
primes= {
xforxinset(prime_sieve(1_000_000)) iflen(str(x)) -len(set(str(x))) >=3
}
forprimeinprimes:
ifprimeinnumbers_checked:
continue
replacements=digit_replacements(prime)
forfamilyinreplacements:
numbers_checked.update(family)
primes_in_family=primes.intersection(family)
iflen(primes_in_family) !=family_length:
continue
returnmin(primes_in_family)
return-1
if__name__=="__main__":
print(solution())