Last active
February 20, 2024 06:32
-
-
Save MurageKibicho/c1d16a7b7f2425c720ad90fb20fcbff6 to your computer and use it in GitHub Desktop.
GeneratePartitions excluding zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include <math.h> | |
int partitionMaxSum = 0; | |
typedef struct partition_struct Partition;struct partition_struct{int index; int number; int partitionLength; int *partition;}; | |
int PartitionCompareNumber(const void *a, const void *b) {return ((*(Partition *)a).number - (*(Partition *)b).number);} | |
Partition *CreatePartition(int partitionArrayLength,int partitionLength, int numberBase) | |
{ | |
Partition *array = malloc(partitionArrayLength * sizeof(Partition)); | |
for(int i = 0; i < partitionArrayLength; i++){array[i].index = 0;array[i].number = 0;array[i].partitionLength = partitionLength; array[i].partition= calloc(partitionLength,sizeof(int));} | |
return array; | |
} | |
void DestroyPartition(int partitionArrayLength, Partition *array){for(int i = 0; i < partitionArrayLength; i++){free(array[i].partition);}free(array);} | |
int FindPartitionArrayLength(int partitionLength, int numberBase){int result = 1;double logValue = 1;for(int i = 1; i <= partitionLength; i++){logValue += log2((double)numberBase -1); assert(logValue < 32);result *= numberBase -1;}return result;} | |
void GeneratePartitions(int partitionArrayLength, Partition *partition, int numberBase, int partitionLength, int frequencyLength, int *frequencies) | |
{ | |
int number[partitionLength]; | |
int i = 0;int count = 0;for(int i = 0; i < partitionLength; i++){number[i] = 1;} | |
while(1) | |
{ | |
/*Check if number has zero*/ | |
int hasZero = 0; int sum = 0;for(i = 0; i < partitionLength; i++){if(number[i] == 0){hasZero = 1; break;}sum += number[i];} | |
if(!hasZero) | |
{ | |
/*Store number in Partition struct*/ | |
assert(count < partitionArrayLength); | |
partition[count].index = count; | |
partition[count].number = sum; | |
for(i = 0; i < partitionLength; i++) | |
{ | |
partition[count].partition[i] = number[i]; | |
} | |
assert(sum < frequencyLength); | |
frequencies[sum] += 1; | |
count += 1; | |
/*Find next number*/ | |
for(i = partitionLength -1; i >= 0; i--) | |
{ | |
if(number[i] < numberBase - 1) | |
{ | |
number[i] += 1; | |
break; | |
} | |
else | |
{ | |
number[i] = 1; | |
} | |
} | |
if(i < 0){break;} | |
} | |
} | |
} | |
void PrintPartition(int partitionArrayLength, Partition *partition) | |
{ | |
for(int i = 0; i < partitionArrayLength; i++) | |
{ | |
printf("%3d: ", partition[i].index); | |
for(int j = 0; j < partition[i].partitionLength; j++) | |
{ | |
printf("%3d, ", partition[i].partition[j]); | |
} | |
printf("%3d \n", partition[i].number); | |
} | |
printf("\n"); | |
} | |
void PrintFrequencies(int frequencyLength, int *frequencies) | |
{ | |
for(int i = 0; i < frequencyLength; i++) | |
{ | |
printf("(%3d %3d)\n", i, frequencies[i]); | |
} | |
printf("\n"); | |
} | |
/* | |
int main() | |
{ | |
int partitionLength = 2; | |
int numberBase = 25; | |
int frequencyLength = numberBase * partitionLength - 1; | |
int partitionArrayLength = FindPartitionArrayLength(partitionLength, numberBase); | |
assert(partitionLength > -1);assert(partitionArrayLength > -1);assert(numberBase > -1); | |
Partition *partition = CreatePartition(partitionArrayLength, partitionLength, numberBase);assert(partition != NULL); | |
int *frequencies = calloc(frequencyLength, sizeof(int));assert(frequencies != NULL); | |
GeneratePartitions(partitionArrayLength, partition, numberBase, partitionLength, frequencyLength, frequencies); | |
printf("%d\n", partitionArrayLength); | |
PrintPartition(partitionArrayLength, partition); | |
PrintFrequencies(frequencyLength, frequencies); | |
DestroyPartition(partitionArrayLength, partition); | |
free(frequencies); | |
return 0; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment