math - How to find nPr (permutations) efficiently? -
is there better way using basic formula n!/(n-r)! have ncr(combinations) ncr = (n-l)cr + (n-1)c(r-1) ?
how this: npr = (n−1)pr + (n−1)p(r−1) ⋅ r
rationale: npr denotes number of ways choose r elements n while noting order , not putting them back. in above recursion distinguish 2 cases. either don't choose nth element, in case you'll choosing r elements set of (n−1). or you'll choosing nth element well, in case you'll choosing other (r−1) elements set of (n−1), , there r possibilities @ point in order chose nth element.
apart this, note can avoid 2 factorials taking product on difference:
n ─┬──┬─ n! │ │ = ──── = (n−r+1)⋅(n−r+2)⋅…⋅(n−1)⋅n = npr │ │ r! i=n−r+1
this leads yet recursive formulation: npr = (n−1)p(r−1) ⋅ n
Comments
Post a Comment