Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created May 28, 2011 19:25
Show Gist options
  • Save johana-star/997138 to your computer and use it in GitHub Desktop.
Save johana-star/997138 to your computer and use it in GitHub Desktop.
Euler Project Problem #028
# Solution to Project Euler's twenty-eighth problem
# http://projecteuler.net/index.php?section=problems&id=28
# Find the sum of the diagonals of a 1001 spiral grid.
def spiral_sum(number)
sum, n = 0, 0
(0..number).to_a.each do |x|
n = 2*x + n
sum = sum + n + 1
end
n = 0
(1..number).to_a.each do |x|
if x.odd? then y = x + 1 else y = x end
n = 2*y + n
sum = sum + n + 1
end
return sum
end
number = 1000
sum = spiral_sum(number)
p sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment