object Euler28 extends Application {
def spiral (size :Int) :Int = {
if (size == 1) return 1;
val smaller = size-2;
spiral(smaller) + 4*(smaller*smaller) + (1+2+3+4)*(smaller+1);
}
println(spiral(1001));
}
This is a slightly brevified version of my original solution which is a bit clearer:
val smaller = size-2;
val start = smaller*smaller;
val step = smaller+1;
return spiral(smaller) + ((start + 1*step) + (start + 2*step) +
(start + 3*step) + (start + 4*step));
Looking at the grid:
21 22 23 24 <b>25</b>
20 7 8 <b>9</b> 10
19 6 <b>1</b> 2 11
18 5 4 3 12
17 16 15 14 13
The bold numbers are the values for start and the step is the distance from the start position to the first corner of the next ring and from that corner to the next corner.