Created
May 28, 2011 19:25
-
-
Save johana-star/997138 to your computer and use it in GitHub Desktop.
Euler Project Problem #028
This file contains hidden or 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
# 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