Task: Special factorials


C++: #include <cmath>
#include <cstdint>
#include <iostream>
#include <functional>


uint64_t factorial(int n) {
    uint64_t result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}


int inverse_factorial(uint64_t f) {
    int p = 1;
    int i = 1;

    if (f == 1) {
        return 0;
    }

    while (p < f) {
        p *= i;
        i++;
    }

    if (p == f) {
        return i - 1;
    }
    return -1;
}


uint64_t super_factorial(int n) {
    uint64_t result = 1;
    for (int i = 1; i <= n; i++) {
        result *= factorial(i);
    }
    return result;
}


uint64_t hyper_factorial(int n) {
    uint64_t result = 1;
    for (int i = 1; i <= n; i++) {
        result *= (uint64_t)powl(i, i);
    }
    return result;
}


uint64_t alternating_factorial(int n) {
    uint64_t result = 0;
    for (int i = 1; i <= n; i++) {
        if ((n - i) % 2 == 0) {
            result += factorial(i);
        } else {
            result -= factorial(i);
        }
    }
    return result;
}


uint64_t exponential_factorial(int n) {
    uint64_t result = 0;
    for (int i = 1; i <= n; i++) {
        result = (uint64_t)powl(i, (long double)result);
    }
    return result;
}

void test_factorial(int count, std::function<uint64_t(int)> func, const std::string &name) {
    std::cout << "First " << count << ' ' << name << '\n';
    for (int i = 0; i < count; i++) {
        std::cout << func(i) << ' ';
    }
    std::cout << '\n';
}

void test_inverse(uint64_t f) {
    int n = inverse_factorial(f);
    if (n < 0) {
        std::cout << "rf(" << f << ") = No Solution\n";
    } else {
        std::cout << "rf(" << f << ") = " << n << '\n';
    }
}

int main() {
    
    test_factorial(9, super_factorial, "super factorials");
    std::cout << '\n';

    
    test_factorial(8, hyper_factorial, "hyper factorials");
    std::cout << '\n';

    test_factorial(10, alternating_factorial, "alternating factorials");
    std::cout << '\n';

    test_factorial(5, exponential_factorial, "exponential factorials");
    std::cout << '\n';

    test_inverse(1);
    test_inverse(2);
    test_inverse(6);
    test_inverse(24);
    test_inverse(120);
    test_inverse(720);
    test_inverse(5040);
    test_inverse(40320);
    test_inverse(362880);
    test_inverse(3628800);
    test_inverse(119);

    return 0;
}






Python: 

from math import prod

def superFactorial(n):
    return prod([prod(range(1,i+1)) for i in range(1,n+1)])

def hyperFactorial(n):
    return prod([i**i for i in range(1,n+1)])

def alternatingFactorial(n):
    return sum([(-1)**(n-i)*prod(range(1,i+1)) for i in range(1,n+1)])

def exponentialFactorial(n):
    if n in [0,1]:
        return 1
    else:
        return n**exponentialFactorial(n-1)
        
def inverseFactorial(n):
    i = 1
    while True:
        if n == prod(range(1,i)):
            return i-1
        elif n < prod(range(1,i)):
            return "undefined"
        i+=1

print("Superfactorials for [0,9] :")
print({"sf(" + str(i) + ") " : superFactorial(i) for i in range(0,10)})

print("\nHyperfactorials for [0,9] :")
print({"H(" + str(i) + ") "  : hyperFactorial(i) for i in range(0,10)})

print("\nAlternating factorials for [0,9] :")
print({"af(" + str(i) + ") " : alternatingFactorial(i) for i in range(0,10)})

print("\nExponential factorials for [0,4] :")
print({str(i) + "$ " : exponentialFactorial(i) for i in range(0,5)})

print("\nDigits in 5$ : " , len(str(exponentialFactorial(5))))

factorialSet = [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]

print("\nInverse factorials for " , factorialSet)
print({"rf(" + str(i) + ") ":inverseFactorial(i) for i in factorialSet})

print("\nrf(119) : " + inverseFactorial(119))