コード例 #1
0
ファイル: problems_21_40.py プロジェクト: dzhou/project-euler
def problem36():
    """ 
    The decimal number, 585 = 1001001001_(2) (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.
    """
    max_len = len(str(int(mlib.int2bin(10 ** 6))))
    psum = 0
    for i in xrange(0, 10 ** 6):
        if mlib.is_palindrome(str(i)):
            b = int(mlib.int2bin(i, count=max_len))
            if mlib.is_palindrome(str(b)):
                psum += i

    return psum
コード例 #2
0
ファイル: problems_1_20.py プロジェクト: dzhou/project-euler
def problem4():
    """ 
    A palindromic number reads the same both ways. The largest palindrome 
    made from the product of two 2-digit numbers is 9009 = 91x90
    Find the largest palindrome made from the product of two 3-digit numbers
    """
    # generate all products
    # note: simple nested loop does not work (not sorted)
    # a bit incomplete
    # implement div blocks    
    vals = []
    block = 900
    index = 999
    
    for i in range(index,index-block,-1):
        for j in range(index,index-block,-1):
            vals.append(i*j)
    vals.sort()
    
    # brute force search
    for i in range(len(vals)-1, -1, -1):
        if mlib.is_palindrome(str(vals[i])):
            return vals[i]