-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp051.py
65 lines (50 loc) · 2.15 KB
/
p051.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
################################################################################
# P51: Prime digit replacements #
################################################################################
# #
# 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. #
# #
################################################################################
# Problem found at projecteuler.net #
# Author: ncfgrill #
################################################################################
from copy import deepcopy
from math import ceil, sqrt
zero = 5
def check_prime(num):
if num % 6 != 1 and num % 6 != 5: return False
for i in range(2, ceil(sqrt(num)) + 1):
if num % i == 0: return False
return True
def convert(num):
return int(''.join(n for n in num))
def variations(num):
global zero
for i in range(1, 2 ** zero):
primes, switch = 0, ('{:0'+ str(zero) + 'b}').format(i)
if list(switch).count('1') % 3 != 0: continue
for r in range(1, 10):
save = deepcopy(num)
for j in range(zero):
if switch[j] == '1': save[j] = str(r)
if check_prime(convert(save)): primes += 1
if primes < r - 1: break
if primes == 8:
s = ''
for k in range(len(num) - 1):
if switch[k] == '1': s += '1'
else: s += num[k]
s += num[-1]
return s
return -1
def find_prime():
global zero
n = '100001'
while True:
zero = len(n) - 1
prime = variations(list(n))
if prime != -1: return prime
n = str(int(n) + 2)
print('Smallest prime:', find_prime())