From b77445479bf262eb74b83484c9b2776c74d7b4bb Mon Sep 17 00:00:00 2001 From: Parth Mittal Date: Sat, 11 Oct 2025 17:46:19 +0530 Subject: [PATCH 1/6] 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/6] 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/6] [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/6] 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) From 3990e2c40bf855885a747577e5f2fec7399f4863 Mon Sep 17 00:00:00 2001 From: cclauss Date: Mon, 14 Sep 2026 04:08:26 +0000 Subject: [PATCH 5/6] updating DIRECTORY.md --- DIRECTORY.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index cafd1eee9e30..bdae8a291a75 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -522,6 +522,7 @@ ## [Fractals](fractals) * [Barnsley Fern](fractals/barnsley_fern.py) + * [Hilbert Curve](fractals/hilbert_curve.py) * [Julia Sets](fractals/julia_sets.py) * [Koch Snowflake](fractals/koch_snowflake.py) * [Mandelbrot](fractals/mandelbrot.py) @@ -699,7 +700,7 @@ * [Gradient Descent](machine_learning/gradient_descent.py) * [K Means Clust](machine_learning/k_means_clust.py) * [K Medoids](machine_learning/k_medoids.py) - * [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py) + * [K Nearest Neighbors](machine_learning/k_nearest_neighbors.py) * [Linear Discriminant Analysis](machine_learning/linear_discriminant_analysis.py) * [Linear Regression](machine_learning/linear_regression.py) * [Linear Regression Vectorized](machine_learning/linear_regression_vectorized.py) @@ -710,9 +711,11 @@ * Lstm * [Lstm Prediction](machine_learning/lstm/lstm_prediction.py) * [Mfcc](machine_learning/mfcc.py) + * [Mini Batch Gradient Descent](machine_learning/mini_batch_gradient_descent.py) * [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py) * [Polynomial Regression](machine_learning/polynomial_regression.py) * [Principle Component Analysis](machine_learning/principle_component_analysis.py) + * [Q Learning](machine_learning/q_learning.py) * [Random Forest Classifier](machine_learning/random_forest_classifier.py) * [Random Forest Regressor](machine_learning/random_forest_regressor.py) * [Scoring Functions](machine_learning/scoring_functions.py) @@ -742,6 +745,7 @@ * [Binary Multiplication](maths/binary_multiplication.py) * [Binomial Coefficient](maths/binomial_coefficient.py) * [Binomial Distribution](maths/binomial_distribution.py) + * [Binomial Expansion](maths/binomial_expansion.py) * [Ceil](maths/ceil.py) * [Chebyshev Distance](maths/chebyshev_distance.py) * [Check Polygon](maths/check_polygon.py) @@ -784,6 +788,7 @@ * [Interquartile Range](maths/interquartile_range.py) * [Is Int Palindrome](maths/is_int_palindrome.py) * [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py) + * [Is Ipv6 Address Valid](maths/is_ipv6_address_valid.py) * [Is Square Free](maths/is_square_free.py) * [Jaccard Similarity](maths/jaccard_similarity.py) * [Joint Probability Distribution](maths/joint_probability_distribution.py) @@ -856,6 +861,7 @@ * [Radix2 Fft](maths/radix2_fft.py) * [Recursive Digit Sum](maths/recursive_digit_sum.py) * [Remove Digit](maths/remove_digit.py) + * [Reverse Factorial Recursive](maths/reverse_factorial_recursive.py) * [Segmented Sieve](maths/segmented_sieve.py) * Series * [Arithmetic](maths/series/arithmetic.py) From b55c78b978c5c8071e90b3fddc17eb6d939b931e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 14 Sep 2026 06:20:13 +0200 Subject: [PATCH 6/6] Refactor docstring and include doctest Updated docstring formatting and added doctest. --- maths/reverse_factorial_recursive.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/maths/reverse_factorial_recursive.py b/maths/reverse_factorial_recursive.py index 919674dc9b8a..b87e0eb5bb53 100644 --- a/maths/reverse_factorial_recursive.py +++ b/maths/reverse_factorial_recursive.py @@ -9,15 +9,12 @@ def reverse_factorial_recursive(value: int, current_divisor: int = 1) -> int: Parameters ---------- - value : int - The positive integer to test (candidate factorial value). - current_divisor : int, optional - The current divisor used while reducing `value` (default is 1). + value: The positive integer to test (candidate factorial value). + current_divisor: The current divisor used while reducing `value` (default is 1). Returns ------- - int - The factorial root (x) if x! == value, otherwise -1. + The factorial root (x) if x! == value, otherwise -1. Examples -------- @@ -53,3 +50,9 @@ def reverse_factorial_recursive(value: int, current_divisor: int = 1) -> int: return -1 return reverse_factorial_recursive(value // current_divisor, current_divisor + 1) + + +if __name__ == "__main__": + from doctest import testmod + + testmod()