Skip to content

Commit

Permalink
Add problem 51 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryukh committed Jan 28, 2012
1 parent 8b5e312 commit adf4ef4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
7 changes: 4 additions & 3 deletions euler.py
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
#-*- encoding: utf8 -*- #-*- encoding: utf8 -*-
#pylint: disable=W0511
""" __author__ = "Valentin Bryukhanov"


""" """
#TODO: need docstring #TODO: need docstring
"""

import os import os
import re import re


Expand All @@ -16,7 +18,6 @@ class SolutionError(Exception):
""" """
Solution exception Solution exception
""" """

def __init__(self, ertext, *args, **kwargs): def __init__(self, ertext, *args, **kwargs):
Exception.__init__(self, *args, **kwargs) Exception.__init__(self, *args, **kwargs)
self.text = ertext self.text = ertext
Expand Down
78 changes: 78 additions & 0 deletions solutions/p51.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
"""
By replacing the 1st digit of *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.
"""

__author__ = 'bryukh'

CONST = 0

from eulerfunc import eratosthenes
from itertools import combinations

def repeating_digit(numb, n):
"""
Check numb for n or greater repeating digit
"""
snumb = str(numb)
for i in xrange(10):
if snumb.count(str(i)) >= n:
return True
return False

def find_template(numb, n):
"""
Find template for repeating number
"""
snumb = str(numb)
temp = []
strnumb = "0123456789"
for s in strnumb:
if snumb.count(s) >= n:
wide_temp = [k for k in xrange(len(snumb)) if snumb[k] == s]
temp += combinations(wide_temp, n)
return temp


def generate_numb(base, template):
"""
generate numbers from template
"""
res = []
sbase = str(base)
for i in "0123456789":
res.append(int(''.join([i if k in template else sbase[k]
for k in range(len(sbase))])))
return res

def solution(value=CONST):
"""
Bryukh's solution
We can check only 3 repeat numbers
>>> solution()
"""
prime_lst = [pr for pr in eratosthenes(999999) if repeating_digit(pr, 3)]
for prime in prime_lst:
templates = find_template(prime, 3)
#print prime, template
for temp in templates:
gen_lst = generate_numb(prime, temp)
count = sum([1 for gen in gen_lst if gen in prime_lst])
if count >= 8:
return prime

if __name__ == "__main__":
from doctest import testmod

testmod(verbose=True)

0 comments on commit adf4ef4

Please sign in to comment.