euler

git clone git://git.codymlewis.com/euler.git
Log | Files | Refs

commit b8ca72f3a4d3662a589559c73830d54fd9235504
parent 0a819113b5fb54aad2f29cfa1a7c25b08ebb3170
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Sun, 18 Nov 2018 14:43:02 +1100

Solution to problem 4

Diffstat:
AFour.py | 37+++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+), 0 deletions(-)

diff --git a/Four.py b/Four.py @@ -0,0 +1,37 @@ +''' +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. +''' + + +def find_palindrome(): + ''' + Find the palindromic number. + ''' + i = 999 + while i > 99: # O(n^2) + j = 999 + while j > 99: + if is_palindrome(i * j): + return i * j + j -= 1 + i -= 1 + return -1 + + +def is_palindrome(n): + ''' + Check if a given number is palindromic. + ''' + possible_pal = str(n) + for i in range(int(len(possible_pal) / 2)): + if possible_pal[i] != possible_pal[len(possible_pal) - (i + 1)]: + return False + return True + + +if __name__ == "__main__": + print(f"The biggest palindromic number that is a product of 2 three " + + f"digit numbers is {find_palindrome()}")