예제 #1
0
파일: p33.py 프로젝트: jmathes/projecteuler
def check(n, d):
    nl = digits.get_all(n)
    dl = digits.get_all(d)
    for i in xrange(2):
        for j in xrange(2):
            if nl[1-i] == dl[1-j] and 1.0 * n / nl[i] == 1.0 * d / dl[j]:
                print "%s/%s -> %s/%s" % (n, d, nl[i], dl[j])
                return True
예제 #2
0
파일: p40.py 프로젝트: jmathes/projecteuler
def digit_at(place):
    index_into_power = place
    power = 1
    while index_into_power > get_digits_in_power(power):
        index_into_power -= get_digits_in_power(power)
        power += 1
    index_into_power -= 1
    number_in_power = index_into_power / power
    digit_in_number = index_into_power % power
    number = (10 ** (power-1)) + number_in_power
    digit = digits.get_all(number)[digit_in_number]
    return digit
예제 #3
0
파일: p38.py 프로젝트: jmathes/projecteuler
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
예제 #4
0
파일: p30.py 프로젝트: jmathes/projecteuler
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
예제 #5
0
파일: p32.py 프로젝트: jmathes/projecteuler
        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))

pprint(answers)
print sum(set(eq[2] for eq in answers))

예제 #6
0
파일: p35.py 프로젝트: jmathes/projecteuler
"""
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

"""

import digits
import primes
from itertools import permutations


pbm = filter(lambda p : 0 not in digits.get_all(p), primes.get_primes(1000000))
pbm = filter(lambda p : 2 not in digits.get_all(p), pbm)
pbm = filter(lambda p : 4 not in digits.get_all(p), pbm)
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:
예제 #7
0
파일: p52.py 프로젝트: jmathes/projecteuler
"""
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
"""

import digits
from math import log

i = 1
while True:
    if int(log(6*i, 10)) == int(log(i, 10)):
        c = set(digits.get_all(i))
        nope = False
        for n in xrange(2, 7):
            if c != set(digits.get_all(i*n)):
                nope = True
                break
        if not nope:
            print i
            exit(0)
    i += 1
예제 #8
0
파일: p34.py 프로젝트: jmathes/projecteuler
"""
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

"""

import digits
from math import factorial
from pprint import pprint, pformat

ans = 0
for i in xrange(1, 2000000):
    if sum([factorial(n) for n in digits.get_all(i)]) == i:
        ans += i
        print i
예제 #9
0
파일: p36.py 프로젝트: jmathes/projecteuler
"""
The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)

"""

import digits
ans = 0

for i in xrange(1, 1000000):
    dl = digits.get_all(i)
    dlr = (dl * 1)
    dlr.reverse()
    bl = digits.get_all(i, 2)
    blr = (bl * 1)
    blr.reverse()
    if dl == dlr and bl == blr:
        print i
        ans += i

print ans
예제 #10
0
파일: p43.py 프로젝트: jmathes/projecteuler
def unique_digits(n):
    dgs = digits.get_all(n)
    return len(set(dgs)) == len(dgs)
예제 #11
0
파일: p43.py 프로젝트: jmathes/projecteuler
first_six_primes = primes.primes[:6]
first_six_primes.reverse()

multiples_of_17 = [n for n in xrange(99, 1000) if divides(17, n) and unique_digits(n)]

alldigits = set(xrange(10))


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

sum = 0
for multiple in multiples_of_17:
    number_so_far = digits.get_all(multiple)
    sum += sum_interestings(number_so_far, first_six_primes)
    
print sum