Пример #1
0
def sum_interestings(number_so_far, primes):
    numbers_unused = alldigits - set(number_so_far)
    if len(numbers_unused) == 1:
        if 0 in numbers_unused:
            return 0
        return digits.collapse(list(numbers_unused) + number_so_far)
    test = number_so_far * 1
    sum = 0
    for number in numbers_unused:
        test = [number] + number_so_far
        if divides(primes[0], digits.collapse(test[:3])):
            sum += sum_interestings(test, primes[1:])
    return sum
Пример #2
0
def test_guess(guess, best):
#    print "Trying ", guess
    multiplier = 0
    list = []
    while True:
        multiplier += 1
        list += digits.get_all(guess * multiplier)
#        print "  list after multiplying by %s" % multiplier, list
        if 0 in list:
#            print "    No zeroes allowed!"
            break
        if digits.collapse(list) < best / (10 ** (9 - len(list))):
#            print "    %s < %s, giving up on %s" % (digits.collapse(list), best / (10 ** (9 - len(list))), guess)
            break
        if len(set(list)) < len(list):
#            print "    list contains duplications, giving up"
            break
        if len(list) == 9:
#            print "***** found a contender! %s results in %s" % (guess, digits.collapse(list))
            return digits.collapse(list)
    return -1
Пример #3
0
l = len(dgs)
done = False

ans = 0

while not done:
    i = l - 1
    dgs[i] += 1
    while dgs[i] > 9:
        dgs[i] = 0
        i -= 1
        if i < 0:
            done = True
            break
        dgs[i] += 1
    for i in xrange(1, l):
        if dgs[i] < dgs[i - 1]:
            dgs[i] = dgs[i - 1]
    spd = digits.get_all(sum_of_powers(dgs, 5))
    n = digits.collapse(spd)
    spd.sort()
    if digits.collapse(dgs) == digits.collapse(spd):
        ans += n
        print n

print ans

assert sum_of_powers(digits.get_all(1634), 4) == 1634
assert sum_of_powers(digits.get_all(8208), 4) == 8208
assert sum_of_powers(digits.get_all(9474), 4) == 9474
Пример #4
0
            i -= 1
            if i < 0:
                raise StopIteration
            l[i] += 1
        for i in xrange(1, length):
            if l[i] > 10 - length + i:
                l[i] = l[i-1] + 1
        yield l * 1

answers = set([])

unique_ascending_lists = lister(4)
for unique_ascending_list in unique_ascending_lists:
    unique_lists = permutations(unique_ascending_list)
    for unique_list in unique_lists:
        number = digits.collapse(unique_list)
        ps = set(unique_list)
#        print "number: %s" % number
        factors = primes.factor(number)
#        print "factors: %s" % factors
        factorpairs = divisions(factors)
#        print unique_list
        for pair in factorpairs:
#            print "  %s" % (pair,)
            multiplicand = functools.reduce(operator.mul, pair[0], 1)
            m1s = set(digits.get_all(multiplicand))
            multiplier = functools.reduce(operator.mul, pair[1], 1)
            m2s = set(digits.get_all(multiplier))
            if len(m1s) + len(m2s) == 5 and m1s | m2s | ps == set([1,2,3,4,5,6,7,8,9]):
                answers.add((multiplicand, multiplier, number))
Пример #5
0
"""
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?
"""

import primes
import digits
from itertools import permutations

for i in xrange(9, 0, -1):
    for p in permutations(xrange(i, 0, -1)):
        potential = digits.collapse(p)
        if primes.is_prime_greedy(potential):
            print potential
            exit(0)

Пример #6
0
pbm = filter(lambda p : 5 not in digits.get_all(p), pbm)
pbm = filter(lambda p : 6 not in digits.get_all(p), pbm)
pbm = filter(lambda p : 8 not in digits.get_all(p), pbm)

cpms = set([2, 5])
ncpms = set([])

def rotations(l):
    l2 = l * 1
    for i in xrange(len(l2)):
        l2 = l2[1:] + [l2[0]]
        yield l2

for prime in pbm:
    if prime not in cpms and prime not in ncpms:
        perms = []
        circular = True
        for ppml in rotations(digits.get_all(prime)):
            ppm = digits.collapse(ppml)
            if not ppm in pbm:
                circular = False
                break
            perms.append(ppm)
        if circular:
#            print perms
            cpms |= set(perms)
        else:
            ncpms |= set(perms)
        
#print cpms
print len(cpms)
Пример #7
0
def get_family(guess):
    candidates = [digits.collapse([(d if d < 10 else nd) for d in guess]) for nd in xrange(10)]
    return [candidate for candidate in candidates if primes.is_prime(candidate)]