{ "cells": [ { "cell_type": "markdown", "id": "9c7a281d", "metadata": {}, "source": [ "# Largest Palindrome Product\n", "> A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is $9009 = 91 \\times 99$.\n", ">\n", "> Find the largest palindrome made from the product of two 3-digit numbers.\n", "\n", "Our search space is only $\\binom{900}{2} = 404550$ 3-digit pairs - a lot to check by hand but peanuts to a modern computer. " ] }, { "cell_type": "code", "execution_count": 1, "id": "e5fd66f6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "906609" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from itertools import combinations\n", "\n", "is_palindrome = lambda x: str(x) == str(x)[::-1]\n", "three_digit_pairs = combinations(range(100, 1000), 2)\n", "max(x*y for (x,y) in three_digit_pairs if is_palindrome(x*y))" ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 9.5", "language": "sage", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 5 }