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

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Stirling numbers of the first kind step by step in the C++ 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 C++ programming language

The provided source code computes and prints the unsigned Stirling numbers of the first kind, denoted as S1(n,k) for integers n and k. These numbers are closely related to permutations and combinations and find applications in various areas of mathematics, including combinatorics, probability theory, and statistical mechanics.

Here's a detailed explanation of the code:

  1. Header Includes: The code includes necessary header files for input/output, data structures, and mathematical operations. Notably, it includes <gmpxx.h> to use the GNU Multiple Precision Arithmetic Library (GMP), which provides support for performing arithmetic operations on arbitrarily large integers.

  2. Unsigned Stirling Numbers Class: The unsigned_stirling1 class is defined to encapsulate the functionality for calculating unsigned Stirling numbers of the first kind. It has a public method get that takes two integer arguments, n and k, and returns the unsigned Stirling number S1(n,k).

  3. Recursive Algorithm: The get method uses a recursive algorithm to compute the Stirling number S1(n,k). It implements the following recursive formula:

    • If k is 0, it returns 1 if n is 0 and 0 otherwise.
    • If k is greater than n, it returns 0.
    • Otherwise, it calculates S1(n,k) as the sum of S1(n - 1, k - 1) and (n - 1) * S1(n - 1, k).
  4. Memoization: To avoid redundant calculations, the get method uses memoization. It stores the previously computed results in a private member variable cache_, which is a map from pairs (n, k) to the corresponding Stirling number S1(n,k). This optimization significantly improves the performance of the algorithm by avoiding the recomputation of previously calculated values.

  5. Printing Unsigned Stirling Numbers: The print_stirling_numbers function is used to print the unsigned Stirling numbers of the first kind up to a specified value of n. It prints a table showing the Stirling numbers in a tabular format, with columns representing different values of k and rows representing different values of n.

  6. Finding the Maximum Stirling Number: The main function demonstrates the use of the unsigned_stirling1 class. It creates an instance of the class (s1) and prints the unsigned Stirling numbers up to n = 12. Additionally, it finds and prints the maximum value of S1(n,k) for n = 100 by iterating through all possible values of k.

Overall, the provided code provides a robust and efficient implementation for computing and exploring unsigned Stirling numbers of the first kind. It employs recursion, memoization, and table printing for clarity and ease of understanding.

Source code in the cpp programming language

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <gmpxx.h>

using integer = mpz_class;

class unsigned_stirling1 {
public:
    integer get(int n, int k);
private:
    std::map<std::pair<int, int>, integer> cache_;
};

integer unsigned_stirling1::get(int n, int k) {
    if (k == 0)
        return n == 0 ? 1 : 0;
    if (k > n)
        return 0;
    auto p = std::make_pair(n, k);
    auto i = cache_.find(p);
    if (i != cache_.end())
        return i->second;
    integer s = get(n - 1, k - 1) + (n - 1) * get(n - 1, k);
    cache_.emplace(p, s);
    return s;
}

void print_stirling_numbers(unsigned_stirling1& s1, int n) {
    std::cout << "Unsigned Stirling numbers of the first kind:\nn/k";
    for (int j = 0; j <= n; ++j) {
        std::cout << std::setw(j == 0 ? 2 : 10) << j;
    }
    std::cout << '\n';
    for (int i = 0; i <= n; ++i) {
        std::cout << std::setw(2) << i << ' ';
        for (int j = 0; j <= i; ++j)
            std::cout << std::setw(j == 0 ? 2 : 10) << s1.get(i, j);
        std::cout << '\n';
    }
}

int main() {
    unsigned_stirling1 s1;
    print_stirling_numbers(s1, 12);
    std::cout << "Maximum value of S1(n,k) where n == 100:\n";
    integer max = 0;
    for (int k = 0; k <= 100; ++k)
        max = std::max(max, s1.get(100, k));
    std::cout << max << '\n';
    return 0;
}


  

You may also check:How to resolve the algorithm Additive primes step by step in the Java programming language
You may also check:How to resolve the algorithm String concatenation step by step in the REXX programming language
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the TXR programming language
You may also check:How to resolve the algorithm Repeat step by step in the C# programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Haskell programming language