Skip to content

Commit b774454

Browse files
authored
Create reverse_factorial_recursive.py
Added reverse factorial recursive algorithm with doctests
1 parent 788d95b commit b774454

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def reverse_factorial(num: int, i: int = 1) -> int:
2+
"""
3+
Return n if n! equals the given num, else return -1.
4+
5+
This function finds the integer n such that n! == num using recursion.
6+
If no such n exists, returns -1.
7+
8+
>>> reverse_factorial(120)
9+
5
10+
>>> reverse_factorial(24)
11+
4
12+
>>> reverse_factorial(150)
13+
-1
14+
>>> reverse_factorial(1)
15+
1
16+
"""
17+
if num == 1:
18+
return i
19+
if num % i != 0:
20+
return -1
21+
return reverse_factorial(num // i, i + 1)

0 commit comments

Comments
 (0)