Skip to content

Instantly share code, notes, and snippets.

@MurageKibicho
Created June 1, 2025 09:02
Show Gist options
  • Save MurageKibicho/120007d69dc4c02ece17febd1737448d to your computer and use it in GitHub Desktop.
Save MurageKibicho/120007d69dc4c02ece17febd1737448d to your computer and use it in GitHub Desktop.
Generators for Certain Alternating Groups With Applications to Cryptography for LeetArxiv
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//Run : clear && gcc Symmetric.c -lm -o m.o && ./m.o
//Code walkthrough : https://leetarxiv.substack.com/p/generators-for-certain-alternating
int factorial(int n)
{
assert(n > 0 && n < 12);
int result = 1;for (int i = 2; i <= n; i++){result *= i;}
return result;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void PrintCycleNotationAndParity(int length, int *array)
{
int parity = 0;
//Initialize all visits to 0 (not visited)
int *visited = calloc(length, sizeof(int));
for(int i = 0; i < length; i++)
{
if(!visited[i])
{
printf("(");
int current = i;
printf("%d", current + 1);
visited[current] = 1;
current = array[current] - 1;
for(; current != i; current = array[current] - 1)
{
printf(" %d", current + 1);
visited[current] = 1;
}
printf(")");
parity += 1;
}
}
free(visited);
if(parity % 2 == 0){printf("\tOdd");}
else{printf("\tEven");}
}
void GeneratePermutations(int *arr, int start, int n)
{
if(start == n)
{
//Print the current permutation
for(int i = 0; i < n; i++){printf("%d ", arr[i]);}
//Print Cycle Notation and Parity
PrintCycleNotationAndParity(n, arr);
printf("\n");
return;
}
for(int i = start; i < n; i++)
{
swap(&arr[start], &arr[i]);
GeneratePermutations(arr, start + 1, n);
swap(&arr[start], &arr[i]);
}
}
int main()
{
int n = 3;
int arr[n];
//Initialize the array with 1 to n
for(int i = 0; i < n; i++){arr[i] = i + 1;}
printf("All permutations in S_%d:\n", n);
GeneratePermutations(arr, 0, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment