Skip to content

Instantly share code, notes, and snippets.

@mweppler
Created October 3, 2011 00:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mweppler/1258204 to your computer and use it in GitHub Desktop.
Save mweppler/1258204 to your computer and use it in GitHub Desktop.
A programming language has 10 different instructions. How many five-instruction programs can be written in this language if no instruction is repeated? How many seven-instruction programs?
#!/usr/bin/ruby
def factorial n
f = n
for i in (n - 1).downto(1)
f *= i
i -= 1
end
return f
end
def combinations_with_repetition n, r
return factorial(n + r - 1) / (factorial(r) * factorial(n - 1))
end
def combinations_without_repetition n, r
return factorial(n) / (factorial(r) * factorial(n - r))
end
def permutations_with_repetition n, r
return n ** r
end
def permutations_without_repetition n, r
return factorial(n) / factorial(n - r)
end
puts "#{combinations_with_repetition 10, 5} 5 instruction programs (combinations_with_repetition)"
puts "#{combinations_without_repetition 10, 5} 5 instruction programs (combinations_without_repetition)"
puts "#{permutations_with_repetition 10, 5} 5 instruction programs (permutations_with_repetition)"
puts "#{permutations_without_repetition 10, 5} 5 instruction programs (permutations_without_repetition)"
puts "#{combinations_with_repetition 10, 7} 7 instruction programs (combinations_with_repetition)"
puts "#{combinations_without_repetition 10, 7} 7 instruction programs (combinations_without_repetition)"
puts "#{permutations_with_repetition 10, 7} 7 instruction programs (permutations_with_repetition)"
puts "#{permutations_without_repetition 10, 7} 7 instruction programs (permutations_without_repetition)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment