Monday, June 13, 2016

Day 1: largest palindrome

Question 1)

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.



Answer:

The largest palindrome can be written as: abccba Which then simpifies to: 100000a + 10000b + 1000c + 100c + 10b + a And then: 100001a + 10010b + 1100c Factoring out 11, you get: 11(9091a + 910b + 100c) So the palindrome must be divisible by 11. Seeing as 11 is prime, at least one of the numbers must be divisible by 11.


#Create a sequence from 100 to 999
three_dig = range(100,1000)
# and an empty list for the palindromes
list_palindromes = []
# loop twice over the number sequence
for n1 in three_dig:    
for n2 in three_dig:       
 # Get the product       
 number =  n1 * n2
 # Transform it in string      
 number = str(number)
 # Check the whole string to test      
  # if it's a palindrome       
 if number == number[::-1]:         
   # Add the palindrome to the list        
 list_palindromes.append(int(number))
print max(list_palindromes)

No comments:

Post a Comment