Skip to content

Instantly share code, notes, and snippets.

@you-ssk
Created November 19, 2012 13:10
Show Gist options
  • Save you-ssk/4110556 to your computer and use it in GitHub Desktop.
Save you-ssk/4110556 to your computer and use it in GitHub Desktop.
$tetra_num = Hash.new{|h,k| h[k] = k*(k+1)*(k+2)/6}
def tetra_array(n)
i,t = 1,[]
while n >= $tetra_num[i]
t << $tetra_num[i]
i += 1
end
t
end
def answer(n, t)
i = 1
while i <= n
t.repeated_combination(i).each do |c|
m = c.inject(:+)
if n == m
return c
end
end
i += 1
end
end
inputs = [40,14,5,165,120,103,106,139]
inputs.each do |i|
t = tetra_array(i)
a = answer(i,t)
b = answer(i,t.select{|x| x.odd?})
puts "#{a.size} #{b.size}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment