Skip to content

Instantly share code, notes, and snippets.

@bsa7
Last active February 21, 2016 18:17
Show Gist options
  • Save bsa7/c58506c9d0368e2cc7a8 to your computer and use it in GitHub Desktop.
Save bsa7/c58506c9d0368e2cc7a8 to your computer and use it in GitHub Desktop.
number of combination and permutation from n symbols in string of k length
def f(n) # factorial
(1..n).to_a.inject{|x,y|x = x * y}
end
n = (('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a).length
k = 8
puts "n = #{n}, k = #{k}"
# 1. Combination with repetition
number_with_repetitions = f(n+k-1) / (f(k) * f(n-1))
puts "number of combinations with repetitions #{number_with_repetitions} only #{k} length"
number_variable_length_with_repetitions = (1..k).to_a.inject{|x,y|x = x + f(n+y-1) / (f(y) * f(n-1))}
puts "number of combinations with repetitions #{number_variable_length_with_repetitions} length from 1 to #{k}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment