I am extremely busy nowadays. I am writing those essays, perfecting them bit by bit. I still won’t let go of my love for Python :D. I came across this site called projecteuler.net (no pragmatic programming for about 2 months, got to stick to something else see). It is basically math problems aiming to increase logic etc. etc. Since I am in a hurry now, let me plainly put up two scripts I wrote to solve two problems. These were problems that really troubled me.
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.
Here is the solution I finally managed to hack up. The first one I wrote took me 3 minutes to get to the answer !. I won’t put that one here
#!/usr/bin/python
def checkPalin(string):
"""Returns true if a string is a palindrome"""
begin = 0
finish = len(string) - 1
while end > begin:
if string[start] != string[end]:
return False
begin += 1
finish -= 1
return True
def returnPalin():
"""Finds the largest palindrome that is the product of 2 3-digit numbers"""
num1 = 999
result_arr = []
while num1 > 100:
num2 = 990
if num2 > num1:
num2 = num1 - (num1 % 11)
while num2 > 109:
if checkPalin(str(num1 * num2)):
result_arr.append(num1 * num2)
num2 -= 11
num1 -= 1
result_arr.sort()
print result_arr[len(result_arr) - 1]
returnPalin()
a full week of hardwork has paid off !
Got to scoot, bye !



0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment