Skip to content

Commit

Permalink
Merge pull request #72 from servoserv/Inverse-factorial-with-mod
Browse files Browse the repository at this point in the history
[Added] Get Inverse Factorial upto 'n' (with modulus) (return an array) #70
  • Loading branch information
imsuraj675 authored Jan 14, 2025
2 parents b308bee + a5fd274 commit 28a0c71
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
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;
}
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));
}
}
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]}")
2 changes: 1 addition & 1 deletion RoadMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ These are fundamental algorithms and functions often used in competitive program
- [x] [LCM of 'a' and 'b'](Number_Theory_Functions/LCM_of_a_and_b)
- [x] [Binary Exponentiation (with modulus)](Number_Theory_Functions/Binary_exponentiation_(with_modulus))
- [ ] Get Factorial upto 'n' (with modulus) (return an array)
- [ ] Get Inverse Factorial upto 'n' (with modulus) (return an array)
- [x] [Get Inverse Factorial upto 'n' (with modulus) (return an array)](Number_Theory_Functions/computeInverseFactorials)
- [ ] nPr (Permutation with modulus)
- [ ] nCr (Combination with modulus)

Expand Down

0 comments on commit 28a0c71

Please sign in to comment.