def largest_palindrome(m, largest=0): for x in xrange(100, m): for y in xrange(100, m): p = x*y if palindrome(p) and p > largest: largest = p return largest
def lychrel(r, p=0): for x in xrange(r): for i in xrange(50): x += int(str(x)[::-1]) if palindrome(x): p += 1 break return r - p
def lychrel(x): path = [ ] for i in range(MAX_ITER): y = int(str(x)[::-1]) z = x + y path.append((x,y,z)) if palindrome(str(z)): return True, tuple(path) x = z else: return False, (x,)
def lychrel(n): cnt = 0 while 1: a = euler.intToSeq(n) b = list(reversed(a)) a = euler.seqToInt(a) b = euler.seqToInt(b) n = a + b if euler.palindrome(n): return True if cnt > 50: return False cnt += 1 return True
def lychrel(n): cnt = 0 while 1: a = euler.intToSeq(n) b = list(reversed(a)) a = euler.seqToInt(a) b = euler.seqToInt(b) n = a+b if euler.palindrome(n): return True if cnt >50: return False cnt += 1 return True
def tern(c, v0, v1): if c: return v0 else: return v1 def reverse(s): lst = list(s) lst.reverse() return "".join(lst) def binaryString(n): lst = [(1 << i) for i in range(20)] res = [tern(n & i == 0, 0, 1) for i in lst] res = [str(r) for r in res] res = "".join(res) res = res.rstrip("0") return reverse(res) lst = [i for i in range(1000000) if euler.palindrome(i)] lst = [i for i in lst if euler.palindrome(binaryString(i))] for i in lst: print i, binaryString(i) print sum(lst)
def tern(c, v0, v1): if c: return v0 else: return v1 def reverse(s): lst = list(s) lst.reverse() return ''.join(lst) def binaryString(n): lst = [(1 << i) for i in range(20)] res = [tern(n & i == 0, 0, 1) for i in lst] res = [str(r) for r in res] res = ''.join(res) res = res.rstrip('0') return reverse(res) lst = [i for i in range(1000000) if euler.palindrome(i)] lst = [i for i in lst if euler.palindrome(binaryString(i))] for i in lst: print i, binaryString(i) print sum(lst)
import euler top = 1000 pals = [ a * b for a in range(top) for b in range(top) if euler.palindrome(a * b) ] print max(pals)
import euler lst = [] for i in range(999, 99, -1): for j in range(999, 99, -1): if euler.palindrome(i * j): lst.append(i * j) print max(lst)
#!/usr/bin/env python # -*- coding: utf-8 -*- from euler import palindrome # 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.) MAX = 1000*1000 bstr = lambda n: n>0 and bstr(n>>1)+str(n&1) or '' p = ( x for x in xrange(1, MAX) if palindrome(x) and palindrome(bstr(x)) ) print sum(p)
import euler top = 1000 pals = [a*b for a in range(top) for b in range(top) if euler.palindrome(a*b)] print max(pals)
""" If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindromes so quickly. For example, 349 + 943 = 1292, 1292 + 2921 = 4213 4213 + 3124 = 7337 That is, 349 took three iterations to arrive at a palindrome. Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits). Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994. How many Lychrel numbers are there below ten-thousand? NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers. """ from euler import palindrome result = 0 for n in range(10000): for _ in range(50): n = n + int(str(n)[::-1]) if palindrome(n): break else: result += 1
def palindrome_base_2_and_10(r, s=0): for x in xrange(r): if palindrome(x) and palindrome(bin(x)[2:]): s += x return s
import euler lst = [] for i in range(999,99,-1): for j in range(999,99,-1): if euler.palindrome(i*j): lst.append( i*j ) print max(lst)
""" The decimal number, 585 = 1001001001 (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.) """ from euler import palindrome result = sum(n for n in range(1000000) if palindrome(n) and palindrome(bin(n)[2:]))