From b77445479bf262eb74b83484c9b2776c74d7b4bb Mon Sep 17 00:00:00 2001 From: Parth Mittal Date: Sat, 11 Oct 2025 17:46:19 +0530 Subject: [PATCH 1/4] Create reverse_factorial_recursive.py Added reverse factorial recursive algorithm with doctests --- maths/reverse_factorial_recursive.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 maths/reverse_factorial_recursive.py diff --git a/maths/reverse_factorial_recursive.py b/maths/reverse_factorial_recursive.py new file mode 100644 index 000000000000..40dd93f127fc --- /dev/null +++ b/maths/reverse_factorial_recursive.py @@ -0,0 +1,21 @@ +def reverse_factorial(num: int, i: int = 1) -> int: + """ + Return n if n! equals the given num, else return -1. + + This function finds the integer n such that n! == num using recursion. + If no such n exists, returns -1. + + >>> reverse_factorial(120) + 5 + >>> reverse_factorial(24) + 4 + >>> reverse_factorial(150) + -1 + >>> reverse_factorial(1) + 1 + """ + if num == 1: + return i + if num % i != 0: + return -1 + return reverse_factorial(num // i, i + 1) From 7d7f50b5ebcbf98deb23e023b3d11d528afd5672 Mon Sep 17 00:00:00 2001 From: Parth Mittal Date: Sat, 11 Oct 2025 17:57:12 +0530 Subject: [PATCH 2/4] Update reverse_factorial_recursive.py --- maths/reverse_factorial_recursive.py | 35 +++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/maths/reverse_factorial_recursive.py b/maths/reverse_factorial_recursive.py index 40dd93f127fc..a9221e6cec25 100644 --- a/maths/reverse_factorial_recursive.py +++ b/maths/reverse_factorial_recursive.py @@ -1,10 +1,25 @@ -def reverse_factorial(num: int, i: int = 1) -> int: +def reverse_factorial(n: int, divisor: int = 1) -> int: """ - Return n if n! equals the given num, else return -1. + Return x such that x! equals the given n, or -1 if no such integer exists. - This function finds the integer n such that n! == num using recursion. - If no such n exists, returns -1. + This function recursively divides the number `n` by successive integers + starting from 1 until it reaches 1. If the division completes exactly at 1, + the last divisor - 1 is the factorial root. Otherwise, return -1. + Parameters + ---------- + n : int + The number whose factorial root is to be found. + divisor : int, optional + The current divisor used in the recursion (default is 1). + + Returns + ------- + int + The factorial root if it exists, otherwise -1. + + Examples + -------- >>> reverse_factorial(120) 5 >>> reverse_factorial(24) @@ -13,9 +28,13 @@ def reverse_factorial(num: int, i: int = 1) -> int: -1 >>> reverse_factorial(1) 1 + >>> reverse_factorial(720) + 6 """ - if num == 1: - return i - if num % i != 0: + if n < 1: + raise ValueError("Input must be a positive integer.") + if n == 1: + return divisor + if n % divisor != 0: return -1 - return reverse_factorial(num // i, i + 1) + return reverse_factorial(n // divisor, divisor + 1) From 0bc36915a67ef60a6cd3022a8397162b363c50f4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Oct 2025 12:27:31 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/reverse_factorial_recursive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/reverse_factorial_recursive.py b/maths/reverse_factorial_recursive.py index a9221e6cec25..e8967bcefe01 100644 --- a/maths/reverse_factorial_recursive.py +++ b/maths/reverse_factorial_recursive.py @@ -2,7 +2,7 @@ def reverse_factorial(n: int, divisor: int = 1) -> int: """ Return x such that x! equals the given n, or -1 if no such integer exists. - This function recursively divides the number `n` by successive integers + This function recursively divides the number `n` by successive integers starting from 1 until it reaches 1. If the division completes exactly at 1, the last divisor - 1 is the factorial root. Otherwise, return -1. From d5ba32f687f94b3e3cfb26fab66e70bd094839eb Mon Sep 17 00:00:00 2001 From: Parth Mittal Date: Sat, 11 Oct 2025 18:02:32 +0530 Subject: [PATCH 4/4] Update reverse_factorial_recursive.py --- maths/reverse_factorial_recursive.py | 59 +++++++++++++++++----------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/maths/reverse_factorial_recursive.py b/maths/reverse_factorial_recursive.py index e8967bcefe01..919674dc9b8a 100644 --- a/maths/reverse_factorial_recursive.py +++ b/maths/reverse_factorial_recursive.py @@ -1,40 +1,55 @@ -def reverse_factorial(n: int, divisor: int = 1) -> int: +def reverse_factorial_recursive(value: int, current_divisor: int = 1) -> int: """ - Return x such that x! equals the given n, or -1 if no such integer exists. + Return x such that x! == value, otherwise return -1. - This function recursively divides the number `n` by successive integers - starting from 1 until it reaches 1. If the division completes exactly at 1, - the last divisor - 1 is the factorial root. Otherwise, return -1. + The function divides `value` by 1, 2, 3, ... recursively. If the repeated + division reduces `value` exactly to 1, the factorial root x is + (current_divisor - 1). If the division ever has a remainder, no integer x + exists and the function returns -1. Parameters ---------- - n : int - The number whose factorial root is to be found. - divisor : int, optional - The current divisor used in the recursion (default is 1). + value : int + The positive integer to test (candidate factorial value). + current_divisor : int, optional + The current divisor used while reducing `value` (default is 1). Returns ------- int - The factorial root if it exists, otherwise -1. + The factorial root (x) if x! == value, otherwise -1. Examples -------- - >>> reverse_factorial(120) + >>> reverse_factorial_recursive(120) 5 - >>> reverse_factorial(24) + >>> reverse_factorial_recursive(24) 4 - >>> reverse_factorial(150) + >>> reverse_factorial_recursive(150) -1 - >>> reverse_factorial(1) + >>> reverse_factorial_recursive(1) 1 - >>> reverse_factorial(720) - 6 + >>> reverse_factorial_recursive(2) + 2 """ - if n < 1: - raise ValueError("Input must be a positive integer.") - if n == 1: - return divisor - if n % divisor != 0: + if not isinstance(value, int): + raise TypeError("value must be an integer") + if not isinstance(current_divisor, int): + raise TypeError("current_divisor must be an integer") + + if value < 1: + raise ValueError("value must be a positive integer") + + # Special-case: initial call with value == 1 should return 1 (since 1! = 1). + if value == 1 and current_divisor == 1: + return 1 + + # If value reduced to 1 during recursion, the factorial root is divisor - 1. + if value == 1: + return current_divisor - 1 + + # If not divisible by the current divisor, it's not a factorial number. + if value % current_divisor != 0: return -1 - return reverse_factorial(n // divisor, divisor + 1) + + return reverse_factorial_recursive(value // current_divisor, current_divisor + 1)