Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created June 15, 2015 05:43
Show Gist options
  • Save domgetter/afc9f1e07ffd9849897e to your computer and use it in GitHub Desktop.
Save domgetter/afc9f1e07ffd9849897e to your computer and use it in GitHub Desktop.
def sum_diagonals(m)
next_i = 1
r = [m[0][0]]
until next_i == m[0].size
i = next_i
j = 0
r[next_i] = 0
until i < 0
r[next_i] += m[i][j]
i -= 1
j += 1
end
next_i += 1
end
next_i -= 1
next_j = 1
until next_j == m[0].size-1
i = next_i
j = next_j
r[next_i+next_j] = 0
until i < 1 || j >= m[0].size
r[next_i+next_j] += m[i][j]
i -= 1
j += 1
end
next_j += 1
end
r << 1 << 1
end
a = [1.0,1.0]
a = sum_diagonals(a.product(a).map {|p| p.reduce(:*)}.each_slice(a.size).to_a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment