Write a program to calculate _{n}P^{r}. _{n}P^{r} represents n permutation r and value of _{n}P^{r }is (n!) / (n-r)!.
Example 1:
Input: n = 2, r = 1
Output: 2
Explaination: 2!/(2-1)! = 2!/1! = (2*1)/1 = 2.
Example 2:
Input: n = 3, r = 3
Output: 6
Explaination: 3!/(3-3)! = 3!/0! = 6/1 = 6.
Your Task:
You do not need to read input or print anything. Your task is to complete the function nPr() which takes n and r as input parameters and returns _{n}P^{r} .
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ n, r ≤ 20

Source: geeksforgeeks | Difficulty: EASY
Tags: Combinatorics

Mathematics

Starter code: #User function Template for python3

class Solution:
    def nPr(self, n, r):
        # code here

Solutions:
class Solution:

	def nPr(self, n, r):
		(f1, f2) = (1, 1)
		for i in range(1, n + 1):
			f1 = f1 * i
		for j in range(1, n - r + 1):
			f2 = f2 * j
		return f1 // f2


class Solution:

	def nPr(self, n, r):
		summ = 1
		for i in range(1, n + 1):
			summ = summ * i
		x = n - r
		res = 1
		for j in range(1, x + 1):
			res = res * j
		return summ // res


import math

class Solution:

	def nPr(self, n, r):
		return math.floor(math.factorial(n) // math.factorial(n - r))


import math

class Solution:

	def nPr(self, n, r):
		a = math.factorial(n)
		b = n - r
		c = math.factorial(b)
		return a // c


class Solution:

	def nPr(self, n, r):
		e = 1
		t = 1
		b = n - r
		for i in range(1, n + 1):
			t *= i
		for j in range(1, b + 1):
			e *= j
		return t // e


def fact(n):
	f = 1
	for i in range(1, n + 1):
		f = f * i
	return f

class Solution:

	def nPr(self, n, r):
		return int(fact(n) / fact(n - r))


class Solution:

	def nPr(self, n, r):
		fact_n = 1
		fact_r = 1
		for i in range(1, n + 1):
			fact_n = i * fact_n
		for i in range(1, n - r + 1):
			fact_r = i * fact_r
		return fact_n // fact_r


class Solution:

	def factorial(self, n):
		if n == 0 or n == 1:
			return 1
		else:
			return n * self.factorial(n - 1)

	def nPr(self, n, r):
		nfact = self.factorial(n)
		dfact = self.factorial(n - r)
		npr = nfact // dfact
		return npr


class Solution:

	def nPr(self, n, r):
		fact = {}
		fact[0] = 1
		i = 1
		while i <= n:
			fact[i] = i * fact[i - 1]
			i = i + 1
		return int(fact[n] / fact[n - r])


class Solution:

	def nPr(self, n, r):
		n_r = n - r
		fact_n = fact(n)
		fact_n_r = fact(n_r)
		return fact_n // fact_n_r

def fact(n):
	fact = 1
	for i in range(1, n + 1):
		fact = fact * i
	return fact


class Solution:

	def nPr(self, n, r):
		prod = 1
		for i in range(n, n - r, -1):
			prod *= i
		return prod


class Solution:

	def fact(self, no):
		fac = 1
		for i in range(1, no + 1):
			fac = fac * i
		return fac

	def nPr(self, n, r):
		npr = self.fact(n) / self.fact(n - r)
		return int(npr)


import math

class Solution:

	def nPr(self, n, r):

		def fact(a):
			fac = 1
			for i in range(1, a + 1):
				fac = fac * i
			return fac
		c = fact(n) // fact(n - r)
		return c


def ftrl(x):
	if x == 0:
		return 1
	elif x == 1:
		return 1
	else:
		return x * ftrl(x - 1)

class Solution:

	def nPr(self, n, r):
		nu = ftrl(n)
		de = ftrl(n - r)
		return nu // de


import math

class Solution:

	def nPr(self, n, r):

		def fact(n):
			l = 1
			if n == 0 or n == 1:
				return 1
			else:
				return n * fact(n - 1)
		return int(fact(n) / fact(n - r))


import math

class Solution:

	def nPr(self, n, r):
		return int(math.factorial(n) / math.factorial(n - r))


class Solution:

	def nPr(self, n, r):

		def factorial(n):
			x = 1
			if n == 0:
				return 1
			else:
				for i in range(1, n + 1):
					x = x * i
				return x
		nPr = factorial(n) / factorial(n - r)
		return int(nPr)


class Solution:

	def nPr(self, n, r):
		p = 1
		q = 1
		for i in range(1, n + 1):
			p = p * i
		for j in range(1, n - r + 1):
			q = q * j
		a = int(p / q)
		return a


class Solution:

	def fect(self, n):
		temp = 1
		while n > 1:
			temp *= n
			n -= 1
		return temp

	def nPr(self, n, r):
		return self.fect(n) // self.fect(n - r)


class Solution:

	def nPr(self, n, r):
		import math
		(x, y) = (math.factorial(n), math.factorial(n - r))
		return x // y


import math

class Solution:

	def nPr(self, n, r):
		ans = math.factorial(n)
		ans1 = math.factorial(n - r)
		return ans // ans1


class Solution:

	def nPr(self, n, r):

		def factorial(n):
			if n == 1 or n == 0:
				return 1
			else:
				return n * factorial(n - 1)
		return factorial(n) // factorial(n - r)


class Solution:

	def nPr(self, n, r):
		lowerLimit = n - r + 1
		prd = 1
		for i in range(lowerLimit, n + 1):
			prd = prd * i
		return prd


def fact(x):
	if x <= 1:
		return 1
	result = 1
	for i in range(2, x + 1):
		result *= i
	return result

class Solution:

	def nPr(self, n, r):
		if r > n:
			return 12312323
		return fact(n) // fact(n - r)


class Solution:

	def nPr(self, n, r):
		(fact, fact1) = (1, 1)
		for i in range(1, n + 1):
			fact = fact * i
		for j in range(1, n - r + 1):
			fact1 = fact1 * j
		return fact // fact1


class Solution:

	def fact(self, n):
		if n < 2:
			return 1
		return n * self.fact(n - 1)

	def nPr(self, n, r):
		return self.fact(n) // self.fact(n - r)


class Solution:

	def nPr(self, n, r):

		def fact(num):
			if num == 0 or num == 1:
				return 1
			else:
				return num * fact(num - 1)
		return fact(n) // fact(n - r)


class Solution:

	def nPr(self, n, r):
		m = 1
		for i in range(1, n + 1):
			m *= i
		p = n - r
		s = 1
		for j in range(1, p + 1):
			s *= j
		output = int(m / s)
		return output


class Solution:

	def nPr(self, n, r):
		return int(self.factorial(n) / self.factorial(n - r))

	def factorial(self, num):
		if num == 0:
			return 1
		else:
			return num * self.factorial(num - 1)


class Solution:

	def nPr(self, n, r):

		def fact(k):
			if k == 0:
				return 1
			return k * fact(k - 1)
		ans = fact(n) / fact(n - r)
		return int(ans)


class Solution:

	def nPr(self, n, r):
		nminusr = n - r
		nf = 1
		nrf = 1
		for i in range(n):
			nf *= i + 1
		for i in range(nminusr):
			nrf *= i + 1
		return int(nf / nrf)


class Solution:

	def nPr(self, n, r):
		s = 1
		for i in range(1, n + 1):
			s = s * i
			p = 1
			for j in range(1, n - r + 1):
				if n - r == 0:
					return p * 1
				else:
					p = p * j
		return s // p


class Solution:

	def nPr(self, n, r):
		upper = 1
		lower = 1
		for i in range(1, n + 1):
			upper = upper * i
		for i in range(1, n - r + 1):
			lower = lower * i
		return int(upper / lower)


class Solution:

	def factorial(self, n):
		if n == 1:
			return 1
		return n * self.factorial(n - 1)

	def nPr(self, n, r):
		if r == 1:
			return n
		if r == n:
			return self.factorial(n)
		return int(self.factorial(n) / self.factorial(n - r))


import math

class Solution:

	def nPr(self, n, r):
		nf = math.factorial(n)
		nrf = math.factorial(n - r)
		return nf // nrf


class Solution:

	def nPr(self, n, r):
		m = n
		sum = 1
		sum1 = 1
		while m > 0:
			sum = sum * m
			m = m - 1
		demo = n - r
		de = demo
		while de > 0:
			sum1 = sum1 * de
			de = de - 1
		return sum // sum1


class Solution:

	def factorial(self, N):
		if N == 0 or N == 1:
			return 1
		if N > 1:
			fact = 1
			for i in range(1, N + 1):
				fact *= i
			return fact

	def nPr(self, n, r):
		nu = self.factorial(n)
		de = self.factorial(n - r)
		return nu // de


class Solution:

	def nPr(self, n, r):
		u = 1
		for i in range(1, n + 1):
			u = u * i
		v = 1
		for j in range(1, n - r + 1):
			v = v * j
		return u // v


from math import factorial

class Solution:

	def nPr(self, n, r):
		return factorial(n) // factorial(n - r)


class Solution:

	def nPr(self, n, r):
		if r > n:
			return 0
		else:
			(factn, factr, factc) = (1, 1, 1)
			if n == 0:
				factn = 1
			else:
				for i in range(2, n + 1):
					factn = factn * i
			c = n - r
			if n == 0:
				factc = 1
			else:
				for i in range(2, c + 1):
					factc = factc * i
			C = factn // factc
			return C


class Solution:

	def nPr(self, n, r):
		out = 1
		if n != r:
			for i in range(n, n - r, -1):
				out = out * i
		else:
			for i in range(1, n + 1):
				out = out * i
		return out


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

class Solution:

	def nPr(self, n, r):
		return fact(n) // fact(n - r)


class Solution:

	def nPr(self, n, r):
		res = 1
		res1 = 1
		for i in range(1, n + 1):
			res *= i
		for j in range(1, n - r + 1):
			res1 *= j
		return res // res1


class Solution:

	def nPr(self, n, r):
		factt = 1
		facttt = 1
		sub = n - r
		for i in range(1, n + 1):
			factt *= i
		for i in range(1, sub + 1):
			facttt *= i
		total = factt // facttt
		return total


class Solution:

	def nPr(self, n, r):
		upper = 1
		lower = 1
		result = 0
		for i in range(1, n + 1):
			upper = upper * i
		for j in range(1, n - r + 1):
			lower = lower * j
		result = int(upper / lower)
		return result


class Solution:

	def nPr(self, n, r):
		s = 1
		for i in range(1, n + 1):
			s = i * s
		c = n - r
		b = 1
		for i in range(1, c + 1):
			b = i * b
		return int(s / b)


class Solution:

	def nPr(self, n, r):
		mul = 1
		mul1 = 1
		for i in range(n, 1, -1):
			mul = mul * i
		x = n - r
		if x == 0:
			x = 1
		for j in range(x, 1, -1):
			mul1 = mul1 * j
		return mul // mul1


class Solution:

	def nPr(self, n, r):
		temp = 1
		for i in range(1, n + 1):
			temp = temp * i
		temp1 = 1
		for i in range(1, n - r + 1):
			temp1 = temp1 * i
		return int(temp / temp1)


import math

class Solution:

	def nPr(self, n, r):
		return round(math.factorial(n) / math.factorial(n - r))


class Solution:

	def nPr(self, n, r):
		a = 1
		a1 = 1
		for i in range(2, n + 1):
			a *= i
		for i in range(2, n - r + 1):
			a1 *= i
		return int(a / a1)


class Solution:

	def factorial(self, n):
		if n == 0:
			return 1
		return n * self.factorial(n - 1)

	def nPr(self, n, r):
		f1 = self.factorial(n)
		f2 = self.factorial(n - r)
		out = f1 // f2
		return out


class Solution:

	def nPr(self, n, r):
		a = 1
		b = 1
		for i in range(1, n + 1):
			a = a * i
		for i in range(1, n - r + 1):
			b = b * i
		return int(a / b)


class Solution:

	def nPr(self, n, r):
		num = Solution.factorial(n)
		deno = Solution.factorial(n - r)
		result = num // deno
		return result

	def factorial(n):
		multi = 1
		for i in range(2, n + 1):
			multi *= i
		return multi


class Solution:

	def fact(self, n):
		if n == 0 or n == 1:
			return 1
		else:
			return n * self.fact(n - 1)

	def nPr(self, n, r):
		out = self.fact(n) / self.fact(n - r)
		return int(out)


class Solution:

	def nPr(self, n, r):
		k = 1
		x = 1
		for i in range(1, n + 1):
			k = k * i
		for j in range(1, n - r + 1):
			x = x * j
		return int(k / x)


class Solution:

	def nPr(self, n, r):
		self.n = n
		self.r = r
		a = 1
		b = 1
		c = n - r
		for i in range(1, n + 1):
			a = a * i
		for j in range(1, c + 1):
			b = b * j
		return a // b


import math

class Solution:

	def nPr(self, n, r):
		k = math.factorial(n)
		e = math.factorial(n - r)
		d = k // e
		return d


class Solution:

	def nPr(self, n, r):

		def fact(n, limit):
			num = 1
			for i in range(n, limit, -1):
				num *= i
			return num
		g = fact(n, n - r)
		return g


class Solution:

	def nPr(self, n, r):

		def fact(x):
			if x == 0 or x == 1:
				return 1
			fac = 1
			return x * fact(x - 1)
		return int(fact(n) / fact(n - r))


class Solution:

	def nPr(self, n, r):
		f = 1
		g = 1
		y = n - r
		for i in range(1, n + 1):
			f = f * i
		for j in range(1, y + 1):
			g = g * j
		return int(f / g)


class Solution:

	def nPr(self, n, r):
		if n == 0:
			return 1
		ans = 1
		for i in range(n - r + 1, n + 1):
			ans *= i
		return ans


class Solution:

	def factorial(self, n):
		if n == 0:
			return 1
		ans = 1
		for i in range(1, n + 1):
			ans *= i
		return ans

	def nPr(self, n, r):
		num = self.factorial(n)
		den = self.factorial(n - r)
		return num // den


class Solution:

	def nPr(self, n, r):
		return int(self.fact(n) / self.fact(n - r))

	def fact(self, a):
		ans = 1
		while a != 0:
			ans *= a
			a -= 1
		return ans