Skip to content
Go back

Capicúas and Products: Find the Largest with Python

Published:  at  05:55 PM

Can you imagine searching for a hidden mathematical treasure among the multitude of numbers? 💎 Let’s unravel the mystery of the largest capicua product!

🔮 Problem Statement

The challenge is to find, within a given range of numbers, two numbers whose product results in the largest possible capicua (or numerical palindrome). A capicua, like a textual palindrome, reads the same from left to right as from right to left (e.g., 9009, 1221).

Parameters:

Return:

Examples:

>>> sorted(largest_capicua_product(10, 99)) == [91, 99]
True
>>> sorted(largest_capicua_product(100, 999)) == [913, 993]
True

Additional Notes:

🧩 Step-by-Step Solution

Our strategy focuses on identifying all possible capicua products within the range, and then determining which is the largest and returning the factors that generate it.

First, we need a function that determines if a number is a capicua. This is simple: we convert the number to a string and compare it to its reversed version.

def is_capicua(num):
	return str(num) == str(num)[::-1]

Here, str(num) converts the number to a string and [::-1] is a concise notation for reversing the string.

The core of the solution is the largest_capicua_product function. We use a dictionary comprehension to generate a dictionary where the keys are the capicua products and the values are the pairs of numbers that generate them.

def largest_capicua_product(start, end):
	products = {(x * y): [x, y] for x in range(start, end + 1) for y in range(start, end + 1) if is_capicua(x * y)}
	return products[max(products.keys())] if len(products) > 0 else []

This block of code iterates over all possible pairs of numbers within the given range. For each pair, it calculates their product and checks if it is a capicua using the is_capicua function. If it is, it adds the product as a key and the pair of numbers as a value to the products dictionary.

Finally, if we find any capicua products (i.e., the products dictionary is not empty), we find the maximum key (the largest capicua product) and return the value associated with that key (the pair of numbers that produce it). If we do not find any capicua products, we return an empty list.

Complete Solution:

def is_capicua(num):
	return str(num) == str(num)[::-1]

def largest_capicua_product(start, end):
	products = {(x * y): [x, y] for x in range(start, end + 1) for y in range(start, end + 1) if is_capicua(x * y)}
	return products[max(products.keys())] if len(products) > 0 else []

🧠 Key Concepts

Dictionary comprehension is fundamental here. It allows us to create the dictionary of capicua products concisely and readably, avoiding the need for more extensive for loops and if statements. It is a powerful tool for data manipulation in Python.

The concept of slicing in strings, specifically [::-1], is crucial for reversing the string and determining if a number is a capicua. This elegant syntax is an efficient way to reverse sequences in Python.

The correct use of nested loops is essential to iterate over all possible combinations of numbers within the given range. This ensures that no potential capicua product is omitted.

The use of ternary conditionals allows us to express the logic of returning the pair of numbers or an empty list concisely, depending on whether capicua products were found or not. It is an elegant way to express simple conditional logic in a single line.

💫 Final Thoughts

This solution, while functional, can be optimized for very large ranges. We could reduce the search space by considering only products of numbers greater than or equal to the square root of the possible largest capicua. In addition, generating all products in memory can be costly for extremely large ranges; a solution based on generators could be more efficient in those cases.

Did you know…? 🤔 The search for the largest palindrome formed by the product of two n-digit numbers is a recurring problem in programming competitions and is a good way to illustrate the importance of algorithmic optimization.

I hope this journey through the world of capicúas has been as interesting to you as it has been to me. If you liked this article, feel free to explore more programming challenges on our blog! The next problem awaits you! 🚀



Previous Post
Fast Prime Factorization: Find the Smallest Prime Factor
Next Post
Anagrams: Detect if two words are equal when reordered