P4
projecteuler.net

Largest Palindrome Product

ℹ️Published on Friday, 16th November 2001, 06:00 pm; Solved by 519644;
Difficulty rating: 5%

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$.

Find the largest palindrome made from the product of two $3$-digit numbers.



Soluzione

Qui basta convertire la richiesta in sintassi di Julia: ci interessa il più alto numero palindromo nn ottenuto come il prodotto di due numeri ii e jj da tre cifre? facciamo un loop con ii e jj sul range dei numeri da 3 cifre, e controlliamo se il loro prodotto è palindromo e se è maggiore del candidato trovato finora (perché ci interessa trovare il massimo numero che ha questa caratteristica). Il controllo sulla palindromia lo si fa confrontando la stringa del numero con sè stessa invertita (tipo 121 è uguale a 121, mentre 123 è diverso da 321).

max_n = 1 # n=i*j
max_i = 1; max_j = 1

for i in 100:998
	for j in (i+1):999
		n = i*j
		if string(n) == reverse(string(n)) && n>max_n
			max_n = n
			max_i = i; max_j = j
		end
	end
end
@show max_n, max_i, max_j
Last modified: May 01, 2025. Website built with Franklin.jl and the lovely Julia programming language.