Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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