Guest User

problem_004

a guest
May 24th, 2016
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. import timeit
  2. from primefac import isprime
  3.  
  4. "==============================================="
  5.  
  6. " Largest palindrome product"
  7. " Problem 4 "
  8.  
  9. " A palindromic number reads the same both ways. "
  10. " The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99. "
  11.  
  12. " Find the largest palindrome made from the product of two 3-digit numbers. "
  13.  
  14. "==============================================="
  15.  
  16.  
  17. " Solution 1"
  18.  
  19. def cheat_even(num):
  20.     nine = '9'*(num/2)
  21.     num1 = nine + nine
  22.     num2 = nine + '0'*(-1 + num/2) + '1'
  23.     product = nine + '0'*(num) + nine
  24.     return ((int(num1), int(num2)), int(product))
  25.  
  26.  
  27. def is_palindrome(n):
  28.     """Return True if n is a palindrome, False otherwise."""
  29.     s = str(n)
  30.     return s == s[::-1]
  31.  
  32.  
  33. def product_lst(n):
  34.     if n % 2 == 0:
  35.         return cheat_even(n)
  36.     n = 10**n
  37.     palindrome = max_palindrome = 0
  38.     pair = (0, 0)
  39.     for i in xrange(n - 1, 0, -1):
  40.         # Since all palindromes are divisible by 11, either j or i has to be as well"
  41.         if i % 11 == 0:
  42.             j_max = i
  43.             j_range = xrange(j_max + 1, min(palindrome/i, j_max), -1)
  44.         else:
  45.             j_max = 11 * int(i / 11)
  46.             j_range = xrange(j_max, min(palindrome/i, j_max), -11)
  47.  
  48.         if j_max * i < palindrome:
  49.             break
  50.  
  51.         for j in j_range:
  52.             product = i * j
  53.             if is_palindrome(product):
  54.                 if product > palindrome:
  55.                     palindrome = product
  56.                     pair = (i, j)
  57.                     break
  58.     return (pair, palindrome)
  59.  
  60.  
  61. " Solution 2"
  62.  
  63. def palindrome_product(n):
  64.     if n % 2 == 0:
  65.         return cheat_even(n)
  66.     en = 10 ** n
  67.     en1 = 10 ** (n - 1)
  68.     for p in generate_palindromes(n):
  69.         p //= 11
  70.         if isprime(p):
  71.             continue
  72.         # Generate numbers such that 11*i has length n
  73.         for i in xrange(en // 11, max(p // en, en1 // 11), -1):
  74.             if p % i == 0:
  75.                 return (i * 11, p // i), p * 11
  76.  
  77.  
  78. def generate_palindromes(n, increase=False):
  79.     """
  80.    Generates palindromes of length 2n.
  81.    Changing increase to True causes the function to yield the smallest numbers first.
  82.    """
  83.     en = 10 ** n
  84.     en1 = 10 ** (n - 1)
  85.     range_ = xrange(en1, en) if increase else xrange(en - 1, en1 - 1, -1)
  86.     for i in range_:
  87.         i = str(i)
  88.         yield int(i + i[::-1])
  89.  
  90. if __name__ == '__main__':
  91.  
  92.     # n = 10
  93.     limit = 2
  94.     print product_lst(limit)
  95.     for i in range(1, 8):
  96.         print i, palindrome_product(i)
  97.  
  98.     for i in range(1, 8):
  99.         print i, product_lst(i)
Add Comment
Please, Sign In to add comment