예제 #1
0
def curious(x, primes):
    for i in range(1,8):
        subnumber = euler_util.undigits(x[i:i+3])
        q,r = divmod(subnumber, primes[i - 1])
        if r != 0:
            return False
    return True
예제 #2
0
def run(target=8):
    pgen = eu.GenFinder(eu.gen_primes())

    # Loop over all prime numbers
    for prime_idx in eu.numbers():
        curr_prime = pgen[prime_idx]
        curr_digits = eu.digits(curr_prime) # digits in current prime

        # Get all selections of indices from current digits
        for rindices in eu.selections(range(len(curr_digits))):
            rzero = 0 in rindices

            if len(rindices) == 0:
                continue

            found_primes = []
            work_digs = curr_digits[:] # copy digits of current prime

            # Choose the digit which we will insert at each index in rindices (the current selection)
            for replacement in range(10):
                if replacement == 0 and rzero:
                     continue

                for rindex in rindices:
                    work_digs[rindex] = replacement

                prime_idx = pgen.find(eu.undigits(work_digs))
                if prime_idx != -1:
                    found_primes.append(pgen[prime_idx])

            if len(found_primes) >= target:
                # print "answer =",min(found_primes),found_primes,rindices
                return min(found_primes)
예제 #3
0
def is_lychrel(x):
    digs = eu.digits(x)
    for i in range(49):
        rdigs = digs[:]
        rdigs.reverse()
        x += eu.undigits(rdigs)
        digs = eu.digits(x)
        if eu.palindrome(digs):
            return False
    return True
예제 #4
0
def run():
    primes = [p for p in euler_util.primes(17)]

    sum = 0
    for c in euler_util.combos(range(10)):
        if c[0] == 0:
            continue
        if curious(c, primes):
            # print c
            sum += euler_util.undigits(c)
예제 #5
0
def run():
    primes = [p for p in euler_util.primes(int(math.ceil(math.sqrt(987654321))))]

    max = 0
    toks = []
    for i in [1,2,3,4,5,6,7,8,9]:
        toks.append(i)
        for c in euler_util.combos(toks):
            x = euler_util.undigits(c)
            if is_prime(x, primes) and x > max:
                max = x
예제 #6
0
def run():
    data = {}
    for i in range(1, 10000):
        val = pow(i,3)

        nval = eu.digits(val)
        nval.sort()
        nval.reverse()
        nval = eu.undigits(nval)

        try:
            data[nval].append(val)
        except KeyError:
            data[nval] = [val]

    min = None
    for k,v in data.items():
        if len(v) == 5:
            if min is None or v[0] < min:
                min = v[0]
    return min
예제 #7
0
import euler_util

def complete(x):
    y = x[:]
    y.sort()
    return y == range(1, 10)

max_val = 0

for digits in euler_util.permutations(range(1,10), 4):
    base_value = euler_util.undigits(digits)

    for i in euler_util.numbers(2):
        curr_value = base_value * i
        digits += euler_util.digits(curr_value)
        if len(digits) > 9:
            break
        elif len(digits) == 9:
            if complete(digits):
                val = euler_util.undigits(digits)
                # print base_value,i,val
                if val > max_val:
                    max_val = val
            break

# print max_val