-
Notifications
You must be signed in to change notification settings - Fork 0
/
e051.py
50 lines (47 loc) · 1.69 KB
/
e051.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
#! /usr/bin/env python3
# Solution to http://projecteuler.net/?section=problems&id=51
from script.maths import nextprime
from script.math2 import digits, digits_to_number
from script.isprime import isprime
def solve():
p = 1
while True:
p = nextprime(p)
digs = digits(p)
numdigs = len(digs)
uniquedigs = list(set(digs))
uniquedigs.sort()
indexes = [None] * 10
for i in range(numdigs):
d = digs[i]
if indexes[d] is None:
indexes[d] = []
indexes[d].append(i)
for d in uniquedigs:
# The first member of a family have recurring digits,
# each of which is one of 0, 1, and 2.
if d > 2:
continue
inds = indexes[d]
# If the number of recurring digits is not a multiple of 3 or
# one of the digits is the least significant, then the family
# can't have 8 members.
if len(inds) % 3 == 0 and inds[-1] != numdigs - 1:
start = 1 if inds[0] == 0 else 0
numgenerated = 10 - start
numprimes = 0
numcomposites = 0
# Generate family candidates
for e in range(start, 10):
for i in inds:
digs[i] = e
number = digits_to_number(digs)
if isprime(number):
numprimes += 1
else:
numcomposites += 1
if numcomposites > numgenerated - 8:
break
if numprimes == 8:
return p
print(solve())