1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: kernel math ranges sequences project-euler.common ;
6 ! https://projecteuler.net/problem=28
11 ! Starting with the number 1 and moving to the right in a
12 ! clockwise direction a 5 by 5 spiral is formed as follows:
20 ! It can be verified that the sum of both diagonals is 101.
22 ! What is the sum of both diagonals in a 1001 by 1001 spiral
23 ! formed in the same way?
29 ! For a square sized n by n, the sum of corners is 4n² - 6n + 6
33 : sum-corners ( n -- sum )
34 dup 1 = [ [ sq 4 * ] [ 6 * ] bi - 6 + ] unless ;
36 : sum-diags ( n -- sum )
37 1 swap 2 <range> [ sum-corners ] map-sum ;
41 : euler028 ( -- answer )
44 ! [ euler028 ] 100 ave-time
45 ! 0 ms ave run time - 0.39 SD (100 trials)