Archive

Archive for July 9, 2019

Python: Largest Palindrome Product



Python: Largest Palindrome Product
Problem No.4 @ Projecteuler
Complete on: on Fri, 5 Jul 2019, 08:53

The task was to find the largest palindromic number that been generated from multiplying two of 3 digits number.

Definition: Palindromic numbers are numbers that remains the same when its digits are reversed. Like 16461, we may say they are “symmetrical”.wikipedia.org

To solve this I first wrote a function to check if we can read a number from both side or not, Then using while and for loop through numbers 100 to 999, and store largest palindromic, we select the range (100,999) because the task is about tow number each with 3 digits.



The Code:



# Problem 4
# Largest palindrome product
# SOLVED
# Completed on Fri, 5 Jul 2019, 08:53


palin =0
def palindromic(n) :

n_list=[]

for each in str(n) :

n_list.append(each)

n_last = len(n_list)-1

n_first =0

x=0

while (n_first+x != n_last-x) :

if n_list[n_first+x] != n_list[n_last-x] :

return False

else :

x +=1

if (n_first +x > n_last -x):

return True

return True

for set1 in range (1,999):

for set2 in range (set1,999):

if palindromic(set1 * set2) :

if (set1 * set2) > palin :

palin =(set1*set2)

print(‘\n We found it:’,palin, ‘coming from {} * {}’.format(set1,set2))






Follow me on Twitter..