We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 788d95b commit b774454Copy full SHA for b774454
1 file changed
maths/reverse_factorial_recursive.py
@@ -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