Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Partition-function-P/Java/partition-function-p.java
Normal file
36
Task/Partition-function-P/Java/partition-function-p.java
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import java.math.BigInteger;
|
||||
|
||||
public class PartitionFunction {
|
||||
public static void main(String[] args) {
|
||||
long start = System.currentTimeMillis();
|
||||
BigInteger result = partitions(6666);
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("P(6666) = " + result);
|
||||
System.out.printf("elapsed time: %d milliseconds\n", end - start);
|
||||
}
|
||||
|
||||
private static BigInteger partitions(int n) {
|
||||
BigInteger[] p = new BigInteger[n + 1];
|
||||
p[0] = BigInteger.ONE;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
p[i] = BigInteger.ZERO;
|
||||
for (int k = 1; ; ++k) {
|
||||
int j = (k * (3 * k - 1))/2;
|
||||
if (j > i)
|
||||
break;
|
||||
if ((k & 1) != 0)
|
||||
p[i] = p[i].add(p[i - j]);
|
||||
else
|
||||
p[i] = p[i].subtract(p[i - j]);
|
||||
j += k;
|
||||
if (j > i)
|
||||
break;
|
||||
if ((k & 1) != 0)
|
||||
p[i] = p[i].add(p[i - j]);
|
||||
else
|
||||
p[i] = p[i].subtract(p[i - j]);
|
||||
}
|
||||
}
|
||||
return p[n];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue