Ejemplo n.º 1
0
Archivo: p55.py Proyecto: icot/euler
def test_lychrel(num):
    n = 1
    candidate = num
    while n <= 50:
        candidate += utils.reverse(candidate)
        if utils.palindrome(candidate):
            return False
        n += 1
    return True
Ejemplo n.º 2
0
Archivo: p125.py Proyecto: icot/euler
def main1():
    #fp = open('palindromes.txt','w')
    cont = 0
    ns = count(2)
    n = ns.next()
    tot = 0
    while n <= 1000:
        if palindrome(n):
            cont+=1
            #fp.write("%d\n" % (n))
            if test_sum(n):
                print ">", n
                tot += n
        n = ns.next()
    print "total", tot
Ejemplo n.º 3
0
Can probably build up a hash map at each index if that number will eventually turn into a palindromic number
"""

CACHE = {}
lychrel_count = 0
for n in range(1, 10000):
    if n in CACHE:
        continue
    # an array to hold all the tested values for this number
    # if the number winds up being a lychrel number, all the other numbers
    # should be cached as "True" to avoid recomputing in a subsequent iteration
    m = n
    rev_add_count = 0  # how many times has it been reversed and added to self
    attempted = [m]
    while rev_add_count < 50:
        m += utils.reverse(m)
        if utils.palindrome(m):
            # update cache for each to True
            for a in attempted:
                CACHE[a] = True
            break
        else:
            # try to reverse again, and add this number to list of those attempted
            rev_add_count += 1
            attempted.append(m)
    if n not in CACHE:
        lychrel_count += 1

print lychrel_count
Ejemplo n.º 4
0
Archivo: p36.py Proyecto: icot/euler
#!/usr/bin/python

import utils

if __name__ == "__main__":
    n = range(1,1000000)
    p1 = filter(lambda x: utils.palindrome(str(x)), n)
    nb = map(lambda x: bin(x)[2:], n)
    p2 = filter(utils.palindrome, nb)
    p2f = map(lambda x: int(x, 2), p2)
    nums = set(p1).intersection(set(p2f))
    print len(nums)
    print nums
    print sum(list(nums))