//============================================================================
// Name        : NewPartitions2.cpp
// Author      : Us
// Version     : 1.0
// Copyright   : GNU
// Description : DIMENSION 4: Finds partitions of the integral value (9) into 3 (number of edges) positive integers that make the determinant of A[2] equal to zero
//============================================================================

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <numeric>
#include <cstdlib>
#include <vector>
#include <list>


using namespace std;

static ofstream myfile;


int CalculDet(unsigned short x1,unsigned short x2,unsigned short x3)
{ 
    return -3*x3 + x2*(-3 + 2*x3) + x1*(-3 - x2*(-2 + x3) + 2*x3);
};

bool validSet(unsigned short * firstNum, unsigned short l)
{
	if(	0 == CalculDet(l,l*firstNum[0],l))
	{       myfile<< l<<" ";
		for( int i = 0; i<1; i++) myfile<< l*firstNum[i]<<" ";
		myfile<< l<<" ";
		myfile<<endl;
	}
	return true;
};

void part(unsigned short k, unsigned short t, unsigned short l)
{

  // Attention: Partitions with no zeros where, since there is an edge from P_0 to P_1 and from P_(n-1) to P_n, the 1st and last magnitudes are equal to l:

	unsigned short r[k];
	int i,j,s,m;
        m=t/l-2;
	r[0]=m-k+1;
        for(i=1;i<k;i++){
		r[i]=1;	}
        validSet(r,l);
	while(r[k-1]!=m-k+1){
		i=k;
		while(r[--i]==1);
		if(i!=k-1){
			r[i]--;
			r[i+1]=2;
			validSet(r,l);
		}
		else{
			while(r[--i]==1);
			r[i]--;
			s=r[k-1];
			r[k-1]=1;
			r[i+1]=s+1;
			validSet(r,l);
		}
	}
};


int main()
{

 // Assigns the values of the necessary constants: m= dimension of the manifold, z= value of the integral, nc= number of cycles

    int m=4;
   
    int z=9;
   
    int nc=0;
   
       
    // Computes the number of edges

    int cardI = (m/2+1)*m/4; 
 
 // writes the partitions in the file OutputPart2.txt where the first element is equal to the last and is a divisor of the integral value smaller or equal than m/2+1
    

     cout << "Writing partitions to the file: OutputPart2.txt"<<endl;
     myfile.open ("OutputPart2.txt");
     unsigned short r[2]; r[0]=1;r[1]=3;
     for(int i=0; i<2; i++){         
         part(cardI-2,z,r[i]);}
     myfile.close();
     cout << "Finished. \n";
     cout << "\n";

     return 0;
}
