Created
December 10, 2014 04:42
-
-
Save vikhyat/1843481c42cb0260d0d5 to your computer and use it in GitHub Desktop.
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
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