Skip to content

Commit 2fd39d6

Browse files
committed
#7430 implemented multiFactorial
1 parent 99f97f6 commit 2fd39d6

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

guava/src/com/google/common/math/BigIntegerMath.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,30 @@ public static BigInteger binomial(int n, int k) {
520520
static boolean fitsInLong(BigInteger x) {
521521
return x.bitLength() <= Long.SIZE - 1;
522522
}
523-
523+
/**
524+
* Returns multifactorial of n with step size BigInteger k.
525+
* This is used for large values of n.
526+
*
527+
* @param n the number to compute the multifactorial of. Must be non-negative.
528+
* @param k the step size. Must be positive.
529+
* @return the multifactorial of n with step size k. If none-zero n is less than k then return n, else return one.
530+
* @throws IllegalArgumentException if n is negative or if k is less than 1.
531+
*/
532+
public static BigInteger multiFactorial(int n, int k) {
533+
if (n < 0) {
534+
throw new IllegalArgumentException("n cannot be negative!");
535+
}
536+
if (k < 1) {
537+
throw new IllegalArgumentException("k must be positive!");
538+
}
539+
if (n <= k) {
540+
return n == 0 ? BigInteger.ONE : BigInteger.valueOf(n);
541+
}
542+
BigInteger result = BigInteger.valueOf(n);
543+
for (int i = n - k; i > 1; i -= k) {
544+
result = result.multiply(BigInteger.valueOf(i));
545+
}
546+
return result;
547+
}
524548
private BigIntegerMath() {}
525549
}

guava/src/com/google/common/math/IntMath.java

+24
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,30 @@ public static int mean(int x, int y) {
721721
public static boolean isPrime(int n) {
722722
return LongMath.isPrime(n);
723723
}
724+
/**
725+
* Returns multifactorial of Int n with step size k.
726+
*
727+
* @param n the number to compute. Must be non-negative.
728+
* @param k the step size for the multifactorial. Must be positive.
729+
* @return the multifactorial of n with step size k. If none-zero n is less than k then return n, else return 1.
730+
* @throws IllegalArgumentException if n is negative or if k is less than 1.
731+
*/
732+
public static int multiFactorial(int n, int k) {
733+
if (n < 0) {
734+
throw new IllegalArgumentException("n cannot be negative!");
735+
}
736+
if (k < 1) {
737+
throw new IllegalArgumentException("k must be positive!");
738+
}
739+
if (n <= k) {
740+
return n == 0 ? 1 : n;
741+
}
742+
int result = n;
743+
for (int i = n - k; i > 1; i -= k) {
744+
result *= i;
745+
}
746+
return result;
747+
}
724748

725749
private IntMath() {}
726750
}

guava/src/com/google/common/math/LongMath.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,29 @@ public static double roundToDouble(long x, RoundingMode mode) {
13451345
}
13461346
throw new AssertionError("impossible");
13471347
}
1348-
1348+
/**
1349+
* Returns multifactorial of int n with step size k.
1350+
*
1351+
* @param n the number to compute. Must be non-negative.
1352+
* @param k the step size must be positive.
1353+
* @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.
1354+
* @throws IllegalArgumentException if n is negative or if k is less than 1.
1355+
*/
1356+
public static long multiFactorial(int n, int k) {
1357+
if (n < 0) {
1358+
throw new IllegalArgumentException("n cannot be negative!");
1359+
}
1360+
if (k < 1) {
1361+
throw new IllegalArgumentException("k must be positive!");
1362+
}
1363+
if (n <= k) {
1364+
return n == 0 ? 1L : (long)n;
1365+
}
1366+
long result = n;
1367+
for (long i = n - k; i > 1; i -= k) {
1368+
result *= i;
1369+
}
1370+
return result;
1371+
}
13491372
private LongMath() {}
13501373
}

0 commit comments

Comments
 (0)