Skip to content

Instantly share code, notes, and snippets.

@ballpointcarrot
Created April 19, 2011 03:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ballpointcarrot/926786 to your computer and use it in GitHub Desktop.
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.
#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