Skip to content

Instantly share code, notes, and snippets.

@vikhyat
Created December 10, 2014 04:42
Show Gist options
  • Save vikhyat/1843481c42cb0260d0d5 to your computer and use it in GitHub Desktop.
Save vikhyat/1843481c42cb0260d0d5 to your computer and use it in GitHub Desktop.
def ncr(n, r)
a, b = r, n-r
a, b = b, a if a < b # a is the larger
numer = (a+1..n).inject(1) { |t,v| t*v } # n!/r!
denom = (2..b).inject(1) { |t,v| t*v } # (n-r)!
numer / denom
end
def ncrdigs_naive(n, r, base)
ncr(n, r).to_s(base).length
end
gets.to_i.times do
n, r = gets.split.map {|x| x.to_i }
puts "#{ncrdigs_naive(n,r,10)} #{ncrdigs_naive(n,r,2)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment