Project Euler 4th Question Answer

Project Euler Question 4 Answer

Project Euler Question 4 Answer

def isPalindrome(s):
    return s == s[::-1]

result = 0
for i in range(100, 999):
    for x in range(100, 999):
        num = i * x
        if isPalindrome(str(num)) and num > result:
            result = num
            
print(result)

Here, we first create a function and in this function, we check whether the number or the word is palindromic.

The working principle of the function is very simple. If the number is the same as its reverse, it returns "True"; if not, it returns "False".

We have a variable called "result", which is the variable that holds our largest palindromic number. In the algorithm below, it keeps changing its value with the largest one.

We have two for loops. I and X go from 100 to 999; since the question asks for the largest palindromic number that can be formed with 3-digit numbers, our numbers are tested between 100 and 999.

We have a variable called "num", and we write the product of i and x to this "num" variable.

Inside the innermost for loop, we check whether it is palindromic and whether it is greater than the number stored in the "result" variable. If it is greater, we add the new result to the "result" variable. When the process is completed, we print the result to the screen.

To see the correct answer, click here.