How to resolve the algorithm Stirling numbers of the first kind step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Stirling numbers of the first kind step by step in the D programming language

Table of Contents

Problem Statement

Stirling numbers of the first kind, or Stirling cycle numbers, count permutations according to their number of cycles (counting fixed points as cycles of length one). They may be defined directly to be the number of permutations of n elements with k disjoint cycles. Stirling numbers of the first kind express coefficients of polynomial expansions of falling or rising factorials. Depending on the application, Stirling numbers of the first kind may be "signed" or "unsigned". Signed Stirling numbers of the first kind arise when the polynomial expansion is expressed in terms of falling factorials; unsigned when expressed in terms of rising factorials. The only substantial difference is that, for signed Stirling numbers of the first kind, values of S1(n, k) are negative when n + k is odd. Stirling numbers of the first kind follow the simple identities:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Stirling numbers of the first kind step by step in the D programming language

Source code in the d programming language

import std.bigint;
import std.functional;
import std.stdio;

alias sterling1 = memoize!sterling1Impl;
BigInt sterling1Impl(int n, int k) {
    if (n == 0 && k == 0) {
        return BigInt(1);
    }
    if (n > 0 && k == 0) {
        return BigInt(0);
    }
    if (k > n) {
        return BigInt(0);
    }
    return sterling1(n - 1, k - 1) + (n - 1) * sterling1(n - 1, k);
}

void main() {
    writeln("Unsigned Stirling numbers of the first kind:");
    int max = 12;
    write("n/k");
    foreach (n; 0 .. max + 1) {
        writef("%10d", n);
    }
    writeln;
    foreach (n; 0 .. max + 1) {
        writef("%-3d", n);
        foreach (k; 0 .. n + 1) {
            writef("%10s", sterling1(n, k));
        }
        writeln;
    }
    writeln("The maximum value of S1(100, k) = ");
    auto previous = BigInt(0);
    foreach (k; 1 .. 101) {
        auto current = sterling1(100, k);
        if (previous < current) {
            previous = current;
        } else {
            import std.conv;

            writeln(previous);
            writefln("(%d digits, k = %d)", previous.to!string.length, k - 1);
            break;
        }
    }
}


  

You may also check:How to resolve the algorithm Speech synthesis step by step in the Python programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Wren programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the C++ programming language
You may also check:How to resolve the algorithm String matching step by step in the EchoLisp programming language