There are 2 types of Permutation:
- Permutation with Repeat is Allowed
Permutation Lock is an example of Repeat Allowed.
There are 10 numbers to choose from (0,1,...9) and we choose 3 of them.
10 x 10 x 10 = 1000 permutationFormula: nr where n is the number of things to choose from, and r is number of times
- Permutation with No Repeat
A good example is lottery which number can not be repeat.
There are 3 numbers to choose from (1,2 and 3) and we choose 3 of them.
3 x 2 x 1 = 6 permutationFormula: Without repetition our choices get reduced each time.

object Permutations {
def main(args: Array[String]) {
print("How many lottery number? ")
val num1 = readInt()
println()
print("How many lottery number to pick? ")
val num2 = readInt()
println("Total Permutation: " + factorial(num1,num2).toString)
}
def factorial(x: BigInt, y: BigInt) : BigInt = {
if (y > 1)
x * factorial( x - 1, y - 1)
else
x
}
}
Tips: Remember to has exist call on recursive function to avoid unstopped loop
No comments:
Post a Comment