-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -520,6 +520,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) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
if (n < 0) { | ||
throw new IllegalArgumentException("n cannot be negative!"); | ||
} | ||
if (k < 1) { | ||
throw new IllegalArgumentException("k must be positive!"); | ||
} | ||
if (n <= k) { | ||
This comment has been minimized.
Sorry, something went wrong.
Mirrious
|
||
return n == 0 ? BigInteger.ONE : BigInteger.valueOf(n); | ||
} | ||
BigInteger result = BigInteger.valueOf(n); | ||
This comment has been minimized.
Sorry, something went wrong.
Mirrious
|
||
for (int i = n - k; i > 1; i -= k) { | ||
result = result.multiply(BigInteger.valueOf(i)); | ||
} | ||
return result; | ||
} | ||
private BigIntegerMath() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -721,6 +721,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!"); | ||
This comment has been minimized.
Sorry, something went wrong.
Mirrious
|
||
} | ||
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() {} | ||
} |
This is a good practice, ensuring that invalid input is handled early.