Reduce allocations and FfOrmatting
This commit is contained in:
parent
972c716993
commit
854a99118f
@ -10,9 +10,13 @@ https://projecteuler.net/problem=4 =#
|
|||||||
using BenchmarkTools
|
using BenchmarkTools
|
||||||
|
|
||||||
|
|
||||||
function ispalindrome(n)
|
ispalindrome(x::Integer, numdigits = ndigits(x)) = _ispalindrome(x, 10^(numdigits - 2))
|
||||||
s = string(n)
|
function _ispalindrome(x::Integer, divider)
|
||||||
s == reverse(s)
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -25,8 +29,9 @@ function Problem4()
|
|||||||
|
|
||||||
ans = 10_001
|
ans = 10_001
|
||||||
for i = 100:999, j = i:999
|
for i = 100:999, j = i:999
|
||||||
if ((i * j > ans) && ispalindrome(i * j))
|
num = i * j
|
||||||
ans = i * j
|
if ((num > ans) && ispalindrome(num, ndigits(num)))
|
||||||
|
ans = num
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user