Calculate nPr

Given separate integers n and r, return the number of ways to arrange r distinct elements chosen from n distinct elements. Order matters. If r > n, return 0.

Given separate integers n and r, return the number of ways to arrange r distinct elements chosen from n distinct elements. Order matters. If r > n, return 0.

0 <= n <= 12 and 0 <= r <= 12. The maximum result is 12P12 = 479,001,600, and every partial product is at most this value.

Example 1: input n = 5, r = 2; output 20. There are five choices for the first position and four for the second.

Example 2: input n = 3, r = 5; output 0. There are fewer distinct elements than positions.

Example 3: input n = 0, r = 0; output 1. There is exactly one empty arrangement.

Category: Math & Numbers. Topic: math. Difficulty: Easy. Patterns: math.