Guest User

Python Euler

a guest
May 23rd, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. import timeit
  2. from primefac import primes, primefac, 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. " Solution 1"
  17.  
  18. def is_palindrome(n):
  19.     """ReturnTrueifnisapalindrome;Falseotherwise."""
  20.     s = str(n)
  21.     return s == s[::-1]
  22.  
  23.  
  24. def product_lst(n):
  25.     palindrome = max_palindrome = 0
  26.     pair = (0,0)
  27.     for i in xrange(n - 1, 0, -1):
  28.         "Since all palindromes are divisible by 11, either j or i has to be as well"
  29.         if i % 11 == 0:
  30.             j_max = i
  31.             j_range = xrange(i+1, 0, -1)
  32.         else:
  33.             j_max = 11*int(i/11)
  34.             j_range = xrange(j_max, 0, -11)
  35.  
  36.         if i*j_max < palindrome:
  37.             break
  38.  
  39.         for j in j_range:
  40.             product = i*j
  41.             if product < palindrome:
  42.                 break
  43.             if is_palindrome(product):
  44.                 if product > palindrome:
  45.                         palindrome = product
  46.                         pair = (i, j)
  47.     return (pair, palindrome)
  48.  
  49.  
  50. " Solution 2"
  51.  
  52.  
  53. def palindrome_product(num):
  54.     " Makes a generator that generates palindromes with length num"
  55.     palindrome_generator = generate_palindromes(num)
  56.     for palindrome in palindrome_generator:
  57.         num1, num2 = is_product_of_two(palindrome, num)
  58.         if num1 != 0:
  59.             return ((num1, num2), palindrome)
  60.  
  61.  
  62. def generate_palindromes(n):
  63.     " Generates palindromes of decreasing order"
  64.     first_half = 10**n - 1
  65.     while first_half > 10**(n-1) - 1:
  66.         second_half = str(first_half)[::-1]
  67.         palindrome = int(str(first_half) + str(second_half))
  68.         yield palindrome
  69.         first_half -= 1
  70.  
  71.  
  72. def is_product_of_two(palindrome, n):
  73.     " Checks wheter the palindrome can be written as a product"
  74.     " of two numbers of length num"
  75.     palindrome //= 11
  76.     "All palindromes are divisible by 11"
  77.     num_1 = num_2 = 0
  78.     if not isprime(palindrome):
  79.         " Generate numbers such that 11*i has length n"
  80.         for i in xrange(10**(n)/11, 10**(n-1)/11, -1):
  81.             " If num2 > 10**n then num2 is no longer a n-digit number"
  82.             if palindrome > i*10**n:
  83.                 break
  84.             if palindrome % i == 0:
  85.                 num_1 = i*11
  86.                 num_2 = palindrome/i
  87.                 break
  88.     return num_1, num_2
  89.  
  90.  
  91. if __name__ == '__main__':
  92.    
  93.     # n = 10
  94.     limit = 5
  95.     print product_lst(10**limit)
  96.     print palindrome_product(9)
  97.     # print timeit.timeit(lambda: palindrome_product(8), number = 1)/float(1)
  98.     # print timeit.timeit(lambda: palindrome_product(9), number = 1)/float(1)
Advertisement
Add Comment
Please, Sign In to add comment