Advertisement
makispaiktis

Project Euler 4 - Largest palindrome product

May 22nd, 2020 (edited)
1,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. '''
  2. A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
  3. Find the largest palindrome made from the product of two 3-digit numbers.
  4. '''
  5.  
  6. def isPalindromic(n):
  7.     if n < 0 or n != int(n):
  8.         print("Negative numbers and numbers with comma cannot be palindromic numbers.")
  9.         return None
  10.     # Make the number string
  11.     stringNumber = str(n)
  12.     for i in range(int(len(stringNumber)/2)):
  13.         if stringNumber[i] != stringNumber[len(stringNumber)-i-1]:
  14.             return False
  15.     return True
  16.  
  17. # MAIN FUNCTION
  18. LIMITPOWER = 3
  19. maxNumber = -10000
  20. maxI = -100
  21. maxJ = -100
  22. for i in range(10**(LIMITPOWER-1), 10**LIMITPOWER):
  23.     for j in range(10 ** (LIMITPOWER - 1), 10 ** LIMITPOWER):
  24.         product = i * j
  25.         if product > maxNumber and isPalindromic(product):
  26.             # print(product)
  27.             maxNumber = product
  28.             maxI = i
  29.             maxJ = j
  30.  
  31. print("The largest palindromic number in [" + str((10**(LIMITPOWER-1))**2) + ", " + str((10**LIMITPOWER - 1)**2) + "] is: " + str(maxNumber) + " = " + str(maxI) + " * " + str(maxJ) + ".")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement