Reduce allocations and FfOrmatting

This commit is contained in:
David Doblas Jiménez 2022-10-05 20:15:51 +02:00
parent 972c716993
commit 854a99118f

View File

@ -10,9 +10,13 @@ https://projecteuler.net/problem=4 =#
using BenchmarkTools
function ispalindrome(n)
s = string(n)
s == reverse(s)
ispalindrome(x::Integer, numdigits = ndigits(x)) = _ispalindrome(x, 10^(numdigits - 2))
function _ispalindrome(x::Integer, divider)
x < 10 && return iszero(x) || iszero(divider)
(x, digit1) = divrem(x, 10)
(digit2, x) = divrem(x, divider)
return digit1 == digit2 && _ispalindrome(x, div(divider, 100))
end
@ -25,8 +29,9 @@ function Problem4()
ans = 10_001
for i = 100:999, j = i:999
if ((i * j > ans) && ispalindrome(i * j))
ans = i * j
num = i * j
if ((num > ans) && ispalindrome(num, ndigits(num)))
ans = num
end
end