-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpro51_practice1.py
66 lines (57 loc) · 1.59 KB
/
pro51_practice1.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
# Prime digit replacements
from __future__ import print_function
import sys
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(number**0.5) + 1, 2):
if number % current == 0:
return False
return True
return False
def get_primes(number):
while True:
if is_prime(number):
yield number
number += 2
# more effective version of sameDigits ?
def sameDigits(n):
n = str(n)
groups = []
for num in '0123456789':
if n.count(num) > 1:
group = []
for i in range(len(n)):
if n[i] == num:
group.append(i)
groups.append(group)
return groups
print (sameDigits('111334'))
def replace(n,digits):
n = list(str(n))
family = []
for i in range(10):
if i == 0 and 0 in digits:
continue
for digit in digits:
n[digit] = str(i)
if is_prime(int(''.join(n))):
family.append(int(''.join(n)))
return family
# eliminate primes with distinct digits
for prime in get_primes(56993):
if len(str(prime)) == len(set(str(prime))):
continue
checked = []
if prime not in checked:
digitsGroup = sameDigits(prime)
for group in digitsGroup:
family = replace(prime,group)
for i in family:
checked.append(i)
if len(family) == 9:
print(family,prime)
sys.exit()