Project Euler Problems for Learn Programming - Solution 4 (Python & Javascript)

Programming

Hey SteemIt community. Did you heard about Project Euler? If you don't know please review this website: Project Euler

Problem 4

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.

Solution (Python)

multiplicands = []
multipliers = []
palindromes = []

for multiplicand in range(999, 99, -1):
    for multiplier in range(999, 99, -1):
        possible_palindrome = multiplicand * multiplier
        possible_palindrome_str = str(possible_palindrome)
        if possible_palindrome_str == possible_palindrome_str[::-1]:
                multiplicands.append(multiplicand)
                multipliers.append(multiplier)
                palindromes.append(possible_palindrome)

largest_palindrome = max(palindromes)
index = palindromes.index(largest_palindrome)

print("Multiplicand is: " + str(multiplicands[index]))
print("Multiplier is: " + str(multipliers[index]))
print("Palindrome is: " + str(max(palindromes)))

Solution (Javascript)

var palindromes = [];

function reverseString(str) {
    for (var i = str.length - 1, o = ''; i >= 0; o += str[i--]) { }
    return o;
}

for(i=999; i>99; i--) {
    for(j=999; j>99; j--) {
        possiblePalindrome = i * j;
        possiblePalindromeStr = possiblePalindrome.toString();
        if (possiblePalindromeStr == reverseString(possiblePalindromeStr)) {
            palindromes.push(possiblePalindrome)
        }
    }
}

largestPalindrome = Math.max.apply(Math, palindromes);
console.log(largestPalindrome);

Result: 906609

Project Euler - Solution 3

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center