Essentially, you can think of this the same as a normal factorial, accept that you decrement n
and r
each recursion and stop when r == 1
instead of n == 1
.
public static int permut(int n, int r){
if(r == 1) return n;
return n*permut(n-1, r-1);
}
So if n = 10
and r = 3
, you need 10!/(10-3)!
or 10*9*8
, and the program will give:
n = 10, r = 3, val = 10
n = 9, r = 2, val = 10*9
n = 8, r = 1, val = 10*9*8