028 Number spiral diagonals.sf 615 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # https://github.com/trizen
  5. # https://projecteuler.net/problem=28
  6. # Runtime: 0.153s
  7. /*
  8. Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
  9. 21 22 23 24 25
  10. 20 7 8 9 10
  11. 19 6 1 2 11
  12. 18 5 4 3 12
  13. 17 16 15 14 13
  14. It can be verified that the sum of the numbers on the diagonals is 101.
  15. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
  16. */
  17. var N = 1001
  18. var sum = 1
  19. for n in (2 .. ceil(N/2)) {
  20. sum += (n * (16*n - 28) + 16)
  21. }
  22. say sum