Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7430 implemented multiFactorial #7438

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion guava/src/com/google/common/math/BigIntegerMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,30 @@ public static BigInteger binomial(int n, int k) {
static boolean fitsInLong(BigInteger x) {
return x.bitLength() <= Long.SIZE - 1;
}

/**
* Returns multifactorial of n with step size BigInteger k.
* This is used for large values of n.
*
* @param n the number to compute the multifactorial of. Must be non-negative.
* @param k the step size. Must be positive.
* @return the multifactorial of n with step size k. If none-zero n is less than k then return n, else return one.
* @throws IllegalArgumentException if n is negative or if k is less than 1.
*/
public static BigInteger multiFactorial(int n, int k) {
if (n < 0) {
throw new IllegalArgumentException("n cannot be negative!");
}
if (k < 1) {
throw new IllegalArgumentException("k must be positive!");
}
if (n <= k) {
return n == 0 ? BigInteger.ONE : BigInteger.valueOf(n);
}
BigInteger result = BigInteger.valueOf(n);
for (int i = n - k; i > 1; i -= k) {
result = result.multiply(BigInteger.valueOf(i));
}
return result;
}
private BigIntegerMath() {}
}
24 changes: 24 additions & 0 deletions guava/src/com/google/common/math/IntMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,30 @@ public static int mean(int x, int y) {
public static boolean isPrime(int n) {
return LongMath.isPrime(n);
}
/**
* Returns multifactorial of Int n with step size k.
*
* @param n the number to compute. Must be non-negative.
* @param k the step size for the multifactorial. Must be positive.
* @return the multifactorial of n with step size k. If none-zero n is less than k then return n, else return 1.
* @throws IllegalArgumentException if n is negative or if k is less than 1.
*/
public static int multiFactorial(int n, int k) {
if (n < 0) {
throw new IllegalArgumentException("n cannot be negative!");
}
if (k < 1) {
throw new IllegalArgumentException("k must be positive!");
}
if (n <= k) {
return n == 0 ? 1 : n;
}
int result = n;
for (int i = n - k; i > 1; i -= k) {
result *= i;
}
return result;
}

private IntMath() {}
}
25 changes: 24 additions & 1 deletion guava/src/com/google/common/math/LongMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,29 @@ public static double roundToDouble(long x, RoundingMode mode) {
}
throw new AssertionError("impossible");
}

/**
* Returns multifactorial of int n with step size k.
*
* @param n the number to compute. Must be non-negative.
* @param k the step size must be positive.
* @return the long type multifactorial of n with step size k. If none-zero n is less than k then return (long) n, else return 1L.
* @throws IllegalArgumentException if n is negative or if k is less than 1.
*/
public static long multiFactorial(int n, int k) {
if (n < 0) {
throw new IllegalArgumentException("n cannot be negative!");
}
if (k < 1) {
throw new IllegalArgumentException("k must be positive!");
}
if (n <= k) {
return n == 0 ? 1L : (long)n;
}
long result = n;
for (long i = n - k; i > 1; i -= k) {
result *= i;
}
return result;
}
private LongMath() {}
}