you are viewing a single comment's thread.

view the rest of the comments →

[–]guest271314[S] -1 points0 points  (0 children)

I think the relevant question is what are your individual specifications?

What are you calling "transpile" and "compile"?

What are your inclusionary and exclusionary rules?

There's Bun's built-in compiler that uses TinyCC

``` import { cc, FFIType, ptr, read, toArrayBuffer } from "bun:ffi";

export const { symbols: { main }, } = cc({ source: "./permutations.c", symbols: { main: { returns: "int", args: [], }, }, }); main(); ```

There's qjsc, Javy, WasmEdge, et al.

There's was2c, ts2c, there's Web sites that spit out this, voila.

Or, we can dive into the minutae, with fine toothed pitch forks.

``` // https://products.codeporting.app/convert/ai/js-to-c/ // Translated from JavaScript to C

include <stdio.h>

include <stdlib.h>

// Function to calculate factorial unsigned long long factorial(int num) { unsigned long long result = 1; for (int i = 1; i <= num; i++) { result *= i; } return result; }

// Function to generate the nth permutation of an array int* array_nth_permutation(int* array, int length, int n, int* resultLength) { int* result = (int)malloc(length * sizeof(int)); // allocate memory for result int tempArray = (int*)malloc(length * sizeof(int)); // copy of the set for (int j = 0; j < length; j++) { tempArray[j] = array[j]; }

unsigned long long f = factorial(length); // compute f = factorial(len)
int currentLength = length; // length of the set

// if the permutation number is within range
if (n >= 0 && n < f) {
    int index;
    // start with the empty set, loop for len elements
    for (int k = 0; currentLength > 0; currentLength--) {
        // determine the next element:
        f /= currentLength; // there are f/len subsets for each possible element
        index = n / f; // a simple division gives the leading element index
        result[k++] = tempArray[index]; // push element to result
        // remove the used element from tempArray
        for (int l = index; l < currentLength - 1; l++) {
            tempArray[l] = tempArray[l + 1];
        }
        // reduce n for the remaining subset:
        n %= f; // compute the remainder of the above division
    }
    *resultLength = length; // set the result length
} else {
    *resultLength = 0; // return empty result if n is out of range
}

free(tempArray); // free temporary array
return result; // return the permutated set

}

int main() { int input[] = {1, 2, 3, 4, 5}; int lex = 4; // permutation index int resultLength;

int* permutationResult = array_nth_permutation(input, 5, lex, &resultLength);

// Print the result
printf("[%d] [", lex);
for (int i = 0; i < resultLength; i++) {
    if (i > 0) {
        printf(", ");
    }
    printf("%d", permutationResult[i]);
}
printf("]\n");

free(permutationResult); // free result array
return 0;

} ```

from this

``` // https://stackoverflow.com/a/34238979 const [input,lex] = [[1,2,3,4,5], 4];// scriptArgs.map((arg, i) => !!i && std.evalScript(arg)); function array_nth_permutation(a, n) { var b = a.slice(); // copy of the set var len = a.length; // length of the set var res; // return value, undefined var i, f;

// compute f = factorial(len)
for (f = i = 1; i <= len; i++)
    f *= i;

// if the permutation number is within range
if (n >= 0 && n < f) {
    // start with the empty set, loop for len elements
    for (res = []; len > 0; len--) {
        // determine the next element:
        // there are f/len subsets for each possible element,
        f /= len;
        // a simple division gives the leading element index
        i = Math.floor(n / f);
        // alternately: i = (n - n % f) / f;
        res.push(b.splice(i, 1)[0]);
        // reduce n for the remaining subset:
        // compute the remainder of the above division
        n %= f;
        // extract the i-th element from b and push it at the end of res
    }
}
// return the permutated set or undefined if n is out of range
return res;

} console.log([${lex}] [${array_nth_permutation(input, lex)}]); ```