Skip to content

Instantly share code, notes, and snippets.

@MurageKibicho
Created February 20, 2024 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MurageKibicho/c833807f02c17e34f92692848062170b to your computer and use it in GitHub Desktop.
Save MurageKibicho/c833807f02c17e34f92692848062170b to your computer and use it in GitHub Desktop.
Generating pentatopes as the frequencies of sum of digits of numbers in base n, excluding numbers with the digit 0
#include <stdio.h>
#include <stdlib.h>
#define BASE 5
#define LENGTH 5
#define MAX_SUM (BASE * LENGTH - 1)
// Define a structure to hold index, number, and sum of digits
struct NumberInfo {
int index;
int number[LENGTH];
int sum;
};
// Comparison function for qsort to sort NumberInfo array by sum
int compare(const void *a, const void *b) {
const struct NumberInfo *num1 = (const struct NumberInfo *)a;
const struct NumberInfo *num2 = (const struct NumberInfo *)b;
return num1->sum - num2->sum;
}
void generateNumbers(struct NumberInfo numbers[], int n, int length, int frequency[]) {
int number[length];
int i, j;
int count = 0;
// Initialize all digits to 1
for (i = 0; i < length; i++) {
number[i] = 1;
}
// Generate numbers and store those without the digit 0
while (1) {
// Check if the number contains 0
int hasZero = 0;
int sum = 0;
for (i = 0; i < length; i++) {
if (number[i] == 0) {
hasZero = 1;
break;
}
sum += number[i];
}
if (!hasZero) {
// Store the number, its index, and sum of digits in the struct array
numbers[count].index = count + 1;
numbers[count].sum = sum;
for (i = 0; i < length; i++) {
numbers[count].number[i] = number[i];
}
frequency[sum]++; // Update frequency array
count++;
}
// Find the next number
for (i = length - 1; i >= 0; i--) {
if (number[i] < n - 1) {
number[i]++;
break;
} else {
number[i] = 1;
}
}
if (i < 0) {
break; // All numbers generated
}
}
}
int main() {
int numbersCount = 1; // Since 00000 will not be included
for (int i = 1; i <= LENGTH; i++) {
numbersCount *= BASE - 1; // There are BASE - 1 possibilities for each digit
}
// Define an array of structures to hold number info
struct NumberInfo numbers[numbersCount];
int frequency[MAX_SUM + 1] = {0}; // Initialize frequency array with zeros
generateNumbers(numbers, BASE, LENGTH, frequency);
// Sort the numbers array by sum of digits using qsort
qsort(numbers, numbersCount, sizeof(struct NumberInfo), compare);
// Printing the sorted numbers with index and sum of digits
printf("Index\tNumber\tSum of Digits\n");
for (int i = 0; i < numbersCount; i++) {
printf("%d\t", numbers[i].index);
for (int j = 0; j < LENGTH; j++) {
printf("%d", numbers[i].number[j]);
}
printf("\t%d\n", numbers[i].sum);
}
// Printing the frequencies of each possible sum
printf("\nFrequencies of each possible sum:\n");
for (int i = 1; i <= MAX_SUM; i++) {
printf("Sum %d: %d\n", i, frequency[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment