Solution to problem 28 in Julia

This commit is contained in:
David Doblas Jiménez 2021-08-23 19:37:48 +02:00
parent 6b2abda602
commit 9669a4cf17

49
src/Julia/Problem028.jl Normal file
View File

@ -0,0 +1,49 @@
#=
Created on 23 Aug 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for Problem 28 of Project Euler
https://projecteuler.net/problem=28
=#
using BenchmarkTools
function Problem28()
#=
Starting with the number 1 and moving to the right in a clockwise
direction a 5 by 5 spiral is formed as follows:
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral
formed in the same way?
=#
size = 1001 # Must be odd
ans = 1 # Special case for size 1
step = 0
i, cur = 1, 1
while step < size-1
step = i * 2
for j in 1:4
cur += step
ans += cur
end
i += 1
end
return ans
end
println("Time to evaluate Problem 28:")
@btime Problem28()
println("")
println("Result for Problem 28: ", Problem28())