John Lekberg


JUNTO Practice - "Project Euler Problem 4"

Discussed on 2018-10-01.

Project Euler Problem 4


Solution - John

import itertools

palindrome = lambda n: str(n) == str(n)[::-1]

three_digit_num = range(100, 1000)

squares = (n ** 2 for n in three_digit_num)

products = (
    n * m for n, m in itertools.combinations(three_digit_num, 2)
)

max_palindrome = max(
    p for p in itertools.chain(squares, products) if palindrome(p)
)

print(max_palindrome)

Solution - Oscar

def euler4():
    sMaxPalin = 0
    iFactor = 999

    def isPalin(iNumber):
        return "".join(reversed(str(iNumber))) == str(iNumber)

    while iFactor > 99:
        for i in range(999, 99, -1):
            iTempMult = iFactor * i
            if isPalin(iTempMult) and iTempMult > sMaxPalin:
                sMaxPalin = iTempMult

        iFactor -= 1

    return sMaxPalin


import cProfile

cProfile.run("euler4()")