-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from servoserv/Inverse-factorial-with-mod
[Added] Get Inverse Factorial upto 'n' (with modulus) (return an array) #70
- Loading branch information
Showing
4 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
Number_Theory_Functions/computeInverseFactorials/computeInverseFactorials.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<long long> inverseFactorials(int n, int MOD) { | ||
vector<long long> factorial(n + 1, 1); | ||
vector<long long> inverseFactorial(n + 1, 1); | ||
|
||
// Calculate factorial modulo MOD | ||
for (int i = 2; i <= n; i++) { | ||
factorial[i] = (factorial[i - 1] * i) % MOD; | ||
} | ||
|
||
// Copute he modular inverse of n! | ||
inverseFactorial[n] = 1; | ||
long long base = factorial[n], power = MOD - 2; | ||
while (power > 0) { | ||
if (power % 2 == 1) { | ||
inverseFactorial[n] = (inverseFactorial[n] * base) % MOD; | ||
} | ||
base = (base * base) % MOD; | ||
power /= 2; | ||
} | ||
|
||
// Calculating all inverse factorials using recursive property: | ||
// invFact[i-1] = invFact[i] * i % MOD | ||
for (int i = n - 1; i >= 1; i--) { | ||
inverseFactorial[i] = (inverseFactorial[i + 1] * (i + 1)) % MOD; | ||
} | ||
|
||
return inverseFactorial; | ||
} | ||
|
||
int main() { | ||
int n = 10; // Compute inverse factorials up to 10 | ||
int MOD = 1e9 + 7; // Example modulus | ||
vector<long long> invFact = inverseFactorials(n, MOD); | ||
|
||
// Print the results for the example case | ||
for (int i = 1; i <= n; i++) { | ||
cout << "Inverse Factorial of " << i << " mod " << MOD << " is " << invFact[i] << endl; | ||
} | ||
return 0; | ||
} |
46 changes: 46 additions & 0 deletions
46
Number_Theory_Functions/computeInverseFactorials/computeInverseFactorials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import java.util.Arrays; | ||
|
||
public class InverseFactorials { | ||
public static long[] computeInverseFactorials(int n, int MOD) { | ||
long[] factorial = new long[n + 1]; | ||
long[] inverseFactorial = new long[n + 1]; | ||
|
||
// Calculate factorial modulo MOD | ||
factorial[0] = 1; | ||
for (int i = 1; i <= n; i++) { | ||
factorial[i] = (factorial[i - 1] * i) % MOD; | ||
} | ||
|
||
// Compute modular inverse of n! | ||
inverseFactorial[n] = modPow(factorial[n], MOD - 2, MOD); | ||
|
||
// Calculating all inverse factorials using recursive property: | ||
for (int i = n - 1; i >= 0; i--) { | ||
inverseFactorial[i] = (inverseFactorial[i + 1] * (i + 1)) % MOD; | ||
} | ||
|
||
return inverseFactorial; | ||
} | ||
|
||
|
||
public static long modPow(long base, long exp, int MOD) { | ||
long result = 1; | ||
while (exp > 0) { | ||
if ((exp & 1) == 1) { | ||
result = (result * base) % MOD; | ||
} | ||
base = (base * base) % MOD; | ||
exp >>= 1; | ||
} | ||
return result; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int n = 10; | ||
int MOD = 1_000_000_007; // Example modulus | ||
long[] invFact = computeInverseFactorials(n, MOD); | ||
|
||
// Print the results for the example case | ||
System.out.println("Inverse Factorials (mod " + MOD + "): " + Arrays.toString(invFact)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Number_Theory_Functions/computeInverseFactorials/computeInverseFactorials.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
def inverse_factorials(n, MOD): | ||
factorial = [1] * (n + 1) | ||
inverse_factorial = [1] * (n + 1) | ||
|
||
# Calculating factorial modulo MOD | ||
for i in range(2, n + 1): | ||
factorial[i] = (factorial[i - 1] * i) % MOD | ||
|
||
# Compute the modular inverse of n! | ||
inverse_factorial[n] = pow(factorial[n], MOD - 2, MOD) | ||
|
||
# Calculating all inverse factorials using recursive property: | ||
for i in range(n - 1, 0, -1): | ||
inverse_factorial[i] = (inverse_factorial[i + 1] * (i + 1)) % MOD | ||
|
||
return inverse_factorial | ||
|
||
# Example usage | ||
n = 10 | ||
MOD = 10**9 + 7 # Example modulus | ||
inv_fact = inverse_factorials(n, MOD) | ||
|
||
# Print results | ||
for i in range(1, n + 1): | ||
print(f"Inverse Factorial of {i} mod {MOD} is {inv_fact[i]}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters