Reduce allocations
This commit is contained in:
parent
34fd8531d9
commit
2df31ca1fb
@ -1,28 +1,37 @@
|
|||||||
#=
|
#=
|
||||||
Created on 17 Jun 2021
|
Created on 17 Jun 2021
|
||||||
|
|
||||||
@author: David Doblas Jiménez
|
@author: David Doblas Jiménez
|
||||||
@email: daviddoji@pm.me
|
@email: daviddoji@pm.me
|
||||||
|
|
||||||
Solution for Problem 4 of Project Euler
|
Solution for Problem 4 of Project Euler
|
||||||
https://projecteuler.net/problem=4
|
https://projecteuler.net/problem=4 =#
|
||||||
=#
|
|
||||||
|
using BenchmarkTools
|
||||||
|
using Profile
|
||||||
|
|
||||||
|
function ispalindrome(n::Int64)
|
||||||
|
# return digits(n) == reverse!(digits(n))
|
||||||
|
for i in zip(digits(n), reverse!(digits(n)))
|
||||||
|
if i[1] ≠ i[end]
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
function Problem4()
|
function Problem4()
|
||||||
#=
|
#=
|
||||||
A palindromic number reads the same both ways. The largest palindrome made
|
A palindromic number reads the same both ways. The largest palindrome made
|
||||||
from the product of two 2-digit numbers is 9009 = 91 x 99.
|
from the product of two 2-digit numbers is 9009 = 91 x 99.
|
||||||
|
|
||||||
Find the largest palindrome made from the product of two 3-digit numbers.
|
Find the largest palindrome made from the product of two 3-digit numbers. =#
|
||||||
=#
|
|
||||||
ans = 0
|
ans = 10_001
|
||||||
for i in 100:1000
|
for i in 100:999, j in i:999
|
||||||
for j in 100:1000
|
num = i * j
|
||||||
palindrome = i * j
|
if (num > ans) && ispalindrome(num)
|
||||||
s = string(palindrome)
|
ans = num
|
||||||
if (s==reverse(s)) & (palindrome > ans)
|
|
||||||
ans = palindrome
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -31,6 +40,6 @@ end
|
|||||||
|
|
||||||
|
|
||||||
println("Time to evaluate Problem 4:")
|
println("Time to evaluate Problem 4:")
|
||||||
@time Problem4()
|
@btime Problem4()
|
||||||
println("")
|
println("")
|
||||||
println("Result for Problem 4: ", Problem4())
|
println("Result for Problem 4: ", Problem4())
|
Loading…
x
Reference in New Issue
Block a user