
makePartitions function, as long as actually how dealing with them is simply a matter of "do-the-dirty-job" for me at the moment.partition.h
#include <vector> #ifndef H_PARTITION #define H_PARTITION class Partition { public: Partition(int num); int getNumber(void); std::vector<std::vector<int> > getPartitions(void); std::vector<int> getPartition(int index); void makePartitions(void); void showPartitions(void); int operator()(void); private: int Number; std::vector<std::vector<int> > Partitions; }; #endif // H_PARTITION
partition.cpp
#include <iostream> #include "partition.h" int Partition::operator()(void) { return Partitions.size(); } void Partition::showPartitions(void) { std::cout << std::endl; for(unsigned int i = 0; i < Partitions.size(); i++) { for(unsigned int j = 0; j < Partitions[i].size() - 1; j++) { std::cout << Partitions[i][j] << " + "; } std::cout << Partitions[i][Partitions[i].size()-1] << std::endl; } } void Partition::makePartitions(void) { std::vector<int> temp; temp.push_back(Number); // the first partition for number N is equal to { N } while(temp[0]>0) { Partitions.push_back(temp); // stores the last partition computed if(temp[0]!=1) { // if the first element of the partition is 1 // then the partition is { 1, ... , 1 } and we're done int k = 0; for(unsigned int i=1; i < temp.size(); i++) { // finds the rightmost element != 1 if(temp[i]>1) { k++; } } temp[k]--; // decreases the rightmost element != 1 temp.insert(temp.begin()+k+1, 1); // inserts 1 at its right bool loop_again = true; while(loop_again) { int grouped = 0; for(unsigned int i=k+1; i < temp.size(); i++) { // groups trailing 1s preserving // monotonicity of the sequence if(grouped+temp[i]<=temp[k]) { grouped += temp[i]; temp.erase(temp.begin()+i); // deletes 1s as they're added i--; } } if(grouped!=0) { // inserts the grouped 1s (if any) temp.insert(temp.begin()+k+1, grouped); } int sum = 0; for(unsigned int j = 0; j < temp.size(); j++) { if(temp[j]==1) { sum++; // calculates the amount of remaining 1s } } if(sum>1&&grouped!=0) { // if there are more trailing 1s and we have just // grouped a few it sets the flag for another loop k++; loop_again = true; } else { loop_again = false; // this partition is done, // so we exit the inner while loop } } } else { temp[0]--; // decreases the first element // of the partition { 1, ... , 1 } to exit the loop } } } std::vector<int> Partition::getPartition(int index) { return Partitions[index]; } std::vector<std::vector<int> > Partition::getPartitions(void) { return Partitions; } int Partition::getNumber(void) { return Number; } Partition::Partition(int num) { if(num>=0) { Number = num; } else { Number = 1; } }
test.cpp
#include <iostream> #include "partition.h" #define N 6 int main(void) { Partition number(N); number.makePartitions(); std::cout << std::endl << "The number " << N << " has the following " << number() << " partitions:" << std::endl << std::endl; number.showPartitions(); return EXIT_SUCCESS; }
[or at least doesn't read like one]. 
1 {1}
2 {1,1}, {2}
3 {1,1,1}, {1,2}, {3}
4 {1,1,1,1}, {1,1,2}, {1,3}, {2,2}, {4}
5 {1,1,1,1,1},{1,1,1,2},{1,1,3},{1,2,2},{1,4},{2,3}, {5}
* p(1) = 1 * p(2) = 2 * p(3) = 3 * p(4) = 5 * p(5) = 7 * p(6) = 11 * p(7) = 15 * p(8) = 22 * p(9) = 30 * p(10) = 42 * p(100) = 190,569,292 * p(1000) = 24,061,467,864,032,622,473,692,149,727,991 ≈ 2.4 × 10^31
class node { int value; node* next; // constructor node (int val, node* nxt); } class partition { int partitionSum; // number being partitioned node* head; // constructor partition (int partsum, node* hd); }
partition sixpart; // some partition of 6 // fill sixpart with {1,1,1,1,1,1} partition node* newnode = new node (1, sixpart->head); partition sevenpart = new partition (7, newnode);

partition.h
#include <vector> #ifndef H_PARTITION #define H_PARTITION void printPartition(std::vector<int>& part); class Partition { private: std::vector<int> currPart; int Number; public: class Iterator { friend class Partition; Partition* partition; public: Iterator(void) { partition = NULL; } Iterator(const Iterator& i) { partition = i.partition; } Iterator(Partition& p) { partition = &p; } std::vector<int>& operator*(void) { return (std::vector<int>&) partition->currPart; } Iterator operator++(void); bool operator==(Iterator op2) { if(partition) { return (partition->currPart==op2.partition->currPart) ? true : false; } else { return (op2.partition) ? false : true; } } bool operator!=(Iterator op2) { return !(*this==op2); } }; friend class Iterator; Partition(int num) { Number = num; currPart.push_back(Number); } Iterator begin(void) { return Iterator(*this); } Iterator end(void) { return Iterator(); } }; #endif // H_PARTITION
partition.cpp
#include "partition.h" #include <iostream> Partition::Iterator Partition::Iterator::operator++(void) { if(partition->currPart[0]!=1) { // if the first element of the partition is 1 // then the partition is { 1, ... , 1 } and we're done int k = 0; for(unsigned int i=1; i < partition->currPart.size(); i++) { // finds the rightmost element != 1 if(partition->currPart[i]>1) { k++; } } partition->currPart[k]--; // decreases the rightmost element != 1 partition->currPart.insert(partition->currPart.begin()+k+1, 1); // inserts 1 at its right bool loop_again = true; while(loop_again) { int grouped = 0; for(unsigned int i=k+1; i < partition->currPart.size(); i++) { // groups trailing 1s preserving // monotonicity of the sequence if(grouped+partition->currPart[i] <= partition->currPart[k]) { grouped += partition->currPart[i]; partition->currPart.erase(partition->currPart.begin()+i); // deletes 1s as they're added i--; } } if(grouped!=0) { // inserts the grouped 1s (if any) partition->currPart.insert(partition->currPart.begin()+k+1, grouped); } int sum = 0; for(unsigned int j = 0; j < partition->currPart.size(); j++) { if(partition->currPart[j]==1) { sum++; // calculates the amount of remaining 1s } } if(sum>1&&grouped!=0) { // if there are more trailing 1s and we have just // grouped a few it sets the flag for another loop k++; loop_again = true; } else { loop_again = false; // this partition is done, // so we exit the inner while loop } } } else { *this = Iterator(partition->end()); } return *this; } void printPartition(std::vector<int>& part) { for(unsigned int i = 0; i < part.size() - 1; i++) { std::cout << part[i] << " + "; } std::cout << part[part.size()-1] << std::endl; }
test1.cpp
#include "partition.h" #include <iostream> int main(void) { Partition number(4); Partition::Iterator i = number.begin(); printPartition(*i); ++i; printPartition(*i); ++i; printPartition(*i); ++i; printPartition(*i); ++i; printPartition(*i); return 0; }
test2.cpp
#include "partition.h" #include <iostream> int main(void) { Partition number(4); for(Partition::Iterator i = number.begin(); i != number.end(); ++i) { printPartition(*i); } return 0; }
).partition.h
#include <vector> #ifndef H_PARTITION #define H_PARTITION void printPartition(std::vector<int>& part); class Partition { private: std::vector<int> currPart; int Number; public: class Iterator { friend class Partition; Partition* partition; public: Iterator(void) { partition = NULL; } Iterator(const Iterator& i) { partition = i.partition; } Iterator(Partition& p) { partition = &p; } std::vector<int>& operator*(void) { return (std::vector<int>&) partition->currPart; } Iterator operator++(void); bool operator==(Iterator op2) { if(partition&&op2.partition) { return (partition->currPart==op2.partition->currPart) ? true : false; } else { return (partition==op2.partition) ? true : false; } } bool operator!=(Iterator op2) { return !(*this==op2); } }; friend class Iterator; Partition(int num) { Number = num; currPart.push_back(Number); } Iterator begin(void) { return Iterator(*this); } Iterator end(void) { return Iterator(); } }; #endif // H_PARTITION

| DaniWeb Message | |
| Cancel Changes | |