Solution to problem 51

This commit is contained in:
David Doblas Jiménez 2021-09-24 19:55:20 +02:00
parent 0d5c33e662
commit 11dada5767

58
src/Python/Problem051.py Normal file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""
Created on 24 Sep 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for problem 52 of Project Euler
https://projecteuler.net/problem=52
"""
from utils import timeit, list_primes, is_prime
@timeit("Problem 52")
def compute():
"""
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.
"""
primes = sorted(set(list_primes(1_000_000)) - set(list_primes(57_000)))
digits = {'0':[], '1':[], '2':[],'3':[], '4':[], '5':[],'6':[], '7':[], '8':[], '9':[]}
for d in digits.keys():
for p in primes:
p = str(p)
if p.count(d) == 3 and p[-1] != d:
digits[d].append(p)
for d in {'0', '1', '2'}:
for p in digits[d]:
res = 0
i = 10
for D in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}-{d}:
i -= 1
q = int(p.replace(d, D))
if is_prime(q) and q > 57_000:
res += 1
if i + res < 7:
break
if res == 7:
return p
if __name__ == "__main__":
print(f"Result for Problem 52: {compute()}")