Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.