vendredi 15 octobre 2010

Euler #36

#!/usr/bin/env python

def is_palyndrome(s): return (s == reduce(lambda x,y: x+y, reversed(s), ''))

def right_number(n):
    c1 = is_palyndrome(str(n))
    c2 = is_palyndrome(bin(n)[2:])
    if c1 and c2:
        return n
    else:
        return 0
    
if __name__ == '__main__':
    print reduce(lambda x, y: x+y, map(right_number, range(1000000)), 0) 

Aucun commentaire:

Enregistrer un commentaire