Skip to content

Commit b6cd5c7

Browse files
committed
#7430 implemented multiFactorial
1 parent b84a41d commit b6cd5c7

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,5 +521,33 @@ static boolean fitsInLong(BigInteger x) {
521521
return x.bitLength() <= Long.SIZE - 1;
522522
}
523523

524+
/**
525+
* Returns multifactorial of n with step size BigInteger k.
526+
* This is used for large values of n.
527+
*
528+
* @param n the number to compute the multifactorial of. Must be non-negative.
529+
* @param k the step size. Must be positive.
530+
* @return the multifactorial of n with step size k. If none-zero n is less than k then return n, else return one.
531+
* @throws IllegalArgumentException if n is negative or if k is less than 1.
532+
*/
533+
public static BigInteger multiFactorial(int n, int k) {
534+
if (n < 0) {
535+
throw new IllegalArgumentException("n cannot be negative!");
536+
}
537+
if (k < 1) {
538+
throw new IllegalArgumentException("k must be positive!");
539+
}
540+
541+
if (n <= k) {
542+
return n == 0 ? BigInteger.ONE : BigInteger.valueOf(n);
543+
}
544+
545+
BigInteger result = BigInteger.valueOf(n);
546+
for (int i = n - k; i > 1; i -= k) {
547+
result = result.multiply(BigInteger.valueOf(i));
548+
}
549+
return result;
550+
}
551+
524552
private BigIntegerMath() {}
525553
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,5 +722,32 @@ public static boolean isPrime(int n) {
722722
return LongMath.isPrime(n);
723723
}
724724

725+
/**
726+
* Returns multifactorial of Int n with step size k.
727+
*
728+
* @param n the number to compute. Must be non-negative.
729+
* @param k the step size for the multifactorial. Must be positive.
730+
* @return the multifactorial of n with step size k. If none-zero n is less than k then return n, else return 1.
731+
* @throws IllegalArgumentException if n is negative or if k is less than 1.
732+
*/
733+
public static int multiFactorial(int n, int k) {
734+
if (n < 0) {
735+
throw new IllegalArgumentException("n cannot be negative!");
736+
}
737+
if (k < 1) {
738+
throw new IllegalArgumentException("k must be positive!");
739+
}
740+
if (n <= k) {
741+
return n == 0 ? 1 : n;
742+
}
743+
744+
int result = n;
745+
for (int i = n - k; i > 1; i -= k) {
746+
result *= i;
747+
}
748+
return result;
749+
}
750+
725751
private IntMath() {}
752+
726753
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,5 +1346,31 @@ public static double roundToDouble(long x, RoundingMode mode) {
13461346
throw new AssertionError("impossible");
13471347
}
13481348

1349+
/**
1350+
* Returns multifactorial of int n with step size k.
1351+
*
1352+
* @param n the number to compute. Must be non-negative.
1353+
* @param k the step size must be positive.
1354+
* @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.
1355+
* @throws IllegalArgumentException if n is negative or if k is less than 1.
1356+
*/
1357+
public static long multiFactorial(int n, int k) {
1358+
if (n < 0) {
1359+
throw new IllegalArgumentException("n cannot be negative!");
1360+
}
1361+
if (k < 1) {
1362+
throw new IllegalArgumentException("k must be positive!");
1363+
}
1364+
if (n <= k) {
1365+
return n == 0 ? 1L : (long)n;
1366+
}
1367+
1368+
long result = n;
1369+
for (long i = n - k; i > 1; i -= k) {
1370+
result *= i;
1371+
}
1372+
return result;
1373+
}
1374+
13491375
private LongMath() {}
13501376
}

0 commit comments

Comments
 (0)