Skip to content

Instantly share code, notes, and snippets.

@dengsauve
Last active March 12, 2017 11:21
Show Gist options
  • Save dengsauve/504f7700d86fc04156162be93ae835d7 to your computer and use it in GitHub Desktop.
Save dengsauve/504f7700d86fc04156162be93ae835d7 to your computer and use it in GitHub Desktop.
Challenge #305 [Hard] Numbers for Sale created by dengsauve - https://repl.it/GQsh/6
def factorial(num) num==0 ? 1 : num * factorial(num-1) end
print 'You want the sum of all numbers in a number to be:'
n = gets.chomp.to_i
print 'You want the largest number to be 10 ^'
k_exp = gets.chomp.to_i
sigma, mu, n_k_l, k_l = 0, 10, n + k_exp - 1, k_exp - 1
(0..[k_exp, n/mu].min).each do |i|
sigma += ((-1)**i) *\
(factorial(k_exp)/(factorial(i) * factorial(k_exp - i))) *\
(factorial(n_k_l - (mu * i))/(factorial(k_l) * factorial(n_k_l - (mu * i) - k_l)))
end
puts sigma
def factorial(num) num==0 ? 1 : num * factorial(num-1) end
total = 0
(0..6).each { |i| total+=((-1)**i)*(factorial(15)/(factorial(i)*factorial(15-i)))*(factorial((83-(10*i)))/(factorial(14)*factorial((83-(10*i)-14)))) }
puts total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment