Created
April 19, 2011 03:45
-
-
Save ballpointcarrot/926786 to your computer and use it in GitHub Desktop.
Calculating the diagonal sum of a number spiral, with '1' in the center.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#adds the diagonals of a number spiral (where 1 is center) | |
def spiral(n) | |
sum = 1 | |
max = n**2 | |
corner = 1 | |
step = 2 | |
while corner < max | |
4.times do |i| | |
corner += step | |
sum += corner | |
end | |
step += 2 | |
end | |
sum | |
end | |
puts spiral(1001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment