From 9669a4cf179765564a580c5337862cc7b9399fe2 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Mon, 23 Aug 2021 19:37:48 +0200 Subject: [PATCH] Solution to problem 28 in Julia --- src/Julia/Problem028.jl | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/Julia/Problem028.jl diff --git a/src/Julia/Problem028.jl b/src/Julia/Problem028.jl new file mode 100644 index 0000000..17c23a7 --- /dev/null +++ b/src/Julia/Problem028.jl @@ -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())