示例#1
0
def euler36(upper_bound=10 ** 6):
    """http://projecteuler.net/index.php?section=problems&id=36
    
    Find the sum of all numbers, less than one million, which are palindromic
    in base 10 and base 2."""
    p_sum = 0
    # note: we can skip even numbers since they're never binary palindromes
    for n in range(1, upper_bound, 2):
        if is_palindrome(n) and is_palindrome(to_binary(n)):
            p_sum += n
    return p_sum
示例#2
0
def euler125(upper_bound=10**8):
    """http://projecteuler.net/problem=125

    The palindromic number 595 is interesting because it can be written as the
    sum of consecutive squares: 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2.

    There are exactly eleven palindromes below one-thousand that can be written
    as consecutive square sums, and the sum of these palindromes is 4164. Note
    that 1 = 0^2 + 1^2 has not been included as this problem is concerned with
    the squares of positive integers.

    Find the sum of all the numbers less than 108 that are both palindromic and
    can be written as the sum of consecutive squares.
    """
    top_square = int(upper_bound**0.5)
    square_sums = [0]*top_square
    for i in xrange(1, top_square):
        square_sums[i] = square_sums[i-1] + i*i

    answers = {}
    for i in xrange(1, len(square_sums)):
        for j in xrange(i-1):
            candidate = square_sums[i] - square_sums[j]
            if candidate < upper_bound and is_palindrome(candidate):
                answers[candidate] = True
    return sum(answers.keys())
示例#3
0
def euler4(upper_bound=1000):
    """http://projecteuler.net/problem=4

    A palindromic number reads the same both ways. The largest palindrome made
    from the product of two 2-digit numbers is 9009 = 91 * 99.

    Find the largest palindrome made from the product of two 3-digit numbers.
    """
    max_pal = 0
    for x in xrange(0, 1000):
        for y in xrange(x, 1000):
            if x * y > max_pal and is_palindrome(x * y):
                max_pal = x * y
    return max_pal
示例#4
0
def euler55():
    """http://projecteuler.net/problem=55

    2520 is the smallest number that can be divided by each of the numbers from
    1 to 10 without any remainder.

    What is the smallest positive number that is evenly divisible by all of the
    numbers from 1 to 20?
    """
    lychrel = []
    for n in range(10000):
        candidate = n
        is_lychrel = True
        for round in range(50):
            candidate += int(str(candidate)[::-1])
            if is_palindrome(candidate):
                is_lychrel = False
                break
        if is_lychrel:
            lychrel.append(n)
    return len(lychrel)
示例#5
0
from euler_util import is_palindrome

if __name__ == "__main__":
	count = 0
	for n in range(1, 10**4):
		m = n
		for i in range(53):
			m = m + int(str(m)[::-1])
			if is_palindrome(str(m)):
				count += 1
				if n == 47:
					print "OK"
				break
	print count