PE 76, took a while to notice you need to subtract 1 from the partition function
This commit is contained in:
44
projecteuler/076/main.cpp
Normal file
44
projecteuler/076/main.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int MAX_N = 100;
|
||||
|
||||
vector<unsigned long long> partition_function(MAX_N + 1, -1);
|
||||
|
||||
int partitioning(int n){
|
||||
unsigned long long result = 0;
|
||||
|
||||
for(int k = 1; k <= n; ++k){
|
||||
int lower = n - k*(3 * k - 1) / 2;
|
||||
int upper = n - k*(3 * k + 1) / 2;
|
||||
result += (k % 2 == 0 ? -1 : 1) * ((upper >= 0 ? partition_function[upper] : 0) + (lower >=0 ? partition_function[lower] : 0));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
cout << "Hello this is Patrick" << endl;
|
||||
auto start = chrono::high_resolution_clock::now();
|
||||
|
||||
// Use partition theory: https://en.wikipedia.org/wiki/Partition_(number_theory)
|
||||
// https://kylekizirian.github.io/euler-partition-function.html
|
||||
|
||||
partition_function[0] = 1;
|
||||
|
||||
for(int n = 1; n <= MAX_N; ++n){
|
||||
partition_function[n] = partitioning(n);
|
||||
}
|
||||
|
||||
// for(unsigned long long i : partition_function){
|
||||
// cout << i << " ";
|
||||
// } cout << endl;
|
||||
|
||||
cout << partition_function[MAX_N] - 1 << endl;
|
||||
|
||||
auto duration = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
|
||||
cout << (float)duration.count()/1000 << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user