## Project Euler Problem #28

### February 14, 2010

Read the details of the problem here

Summary

What is the sum of both diagonals in a 1001 by 1001 spiral?

Solution

Another straightforward problem. The sum of the corners of a ring is quite simple and it’s just a case of spotting the increment that the bottom right-hand corner undergoes as the spiral expands.

```def ( answer, brval ) = [ 1, 3 ]

(1..500).each { ring ->
answer += 4 * brval + 6 * (2 * ring)
brval += 8 * ring + 2
}
```

Note that the answer (sum of numbers) starts at 1 as ring 0 is a special case having only one element and isn’t handled in the loop.

This runs in 0.47 seconds. A Java version using longs runs in around 0.28 ms – more than 1500x faster. Jeepers.

Conclusion

I didn’t really use any specific Groovy feature here apart from multiple assignment in a few places and in the loop syntax. Performance was completely adequate for the scope of this problem but still dismally slow compared to Java for this type of work.