1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.ranges sequences ;
6 ! http://projecteuler.net/index.php?section=problems&id=28
11 ! Starting with the number 1 and moving to the right in a clockwise direction a
12 ! 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 formed in the same way?
28 ! For a square sized n by n, the sum of corners is 4n² - 6n + 6
32 : sum-corners ( n -- sum )
33 dup 1 = [ [ sq 4 * ] [ 6 * ] bi - 6 + ] unless ;
35 : sum-diags ( n -- sum )
36 1 swap 2 <range> [ sum-corners ] sigma ;
40 : euler028 ( -- answer )
43 ! [ euler028 ] 100 ave-time
44 ! 0 ms ave run time - 0.39 SD (100 trials)