Created
October 3, 2011 00:51
-
-
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?
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
#!/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