How to resolve the algorithm Motzkin numbers step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Motzkin numbers step by step in the C programming language

Table of Contents

Problem Statement

The nth Motzkin number (denoted by M[n]) is the number of different ways of drawing non-intersecting chords between n points on a circle (not necessarily touching every point by a chord). By convention M[0] = 1.

Compute and show on this page the first 42 Motzkin numbers or, if your language does not support 64 bit integers, as many such numbers as you can. Indicate which of these numbers are prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Motzkin numbers step by step in the C programming language

This C code explores the properties of the OEIS sequence A030452, defined as M(n) = (4^n + 4 - 2^(n+3)) / 6 for n ≥ 0, with an initial seed value of M(0) = M(1) = 1. The program checks if each value in the sequence is a prime number and prints the corresponding results.

Here's a step-by-step explanation of the code:

  1. Header Inclusions:

    • The code includes several standard C library headers:
      • <locale.h> for locale settings related to number formatting.
      • <stdbool.h> for the bool data type.
      • <stdint.h> for the uint64_t data type (unsigned 64-bit integer).
      • <stdio.h> for input and output operations.
  2. is_prime Function:

    • This function checks whether a given unsigned 64-bit integer n is a prime number.
    • It uses optimizations to quickly check for small prime factors (2 and 3) and then checks for other factors up to the square root of n.
    • If n is a prime number, the function returns true; otherwise, it returns false.
  3. main Function:

    • Sets the locale to use the system's default settings for number formatting with setlocale(LC_ALL, "").
    • Prints a header line with column labels: "n", "M(n)", and "Prime?".
    • Initializes variables m0 and m1 with the initial seed values for the OEIS sequence.
  4. Loop for Calculating M(n):

    • The loop iterates from i = 0 to i < 42.
    • For each iteration, it calculates the next value in the OEIS sequence as:
      • m = (m1 * (2 * i + 1) + m0 * (3 * i - 3)) / (i + 2) for i > 1.
      • m = 1 for i = 0 and i = 1 (the initial seed values).
    • The calculated value m is stored for printing.
  5. Printing Results:

    • For each value of m, the program prints:
      • i: The current index in the sequence (starting from 0).
      • m: The calculated value of M(n) for the current index.
      • "true" or "false" depending on whether m is a prime number.
  6. Updating Seed Values:

    • After printing the results for the current iteration, the program updates m0 to m1 and m1 to the newly calculated m for the next iteration of the loop.

Source code in the c programming language

#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

bool is_prime(uint64_t n) {
    if (n < 2)
        return false;
    if (n % 2 == 0)
        return n == 2;
    if (n % 3 == 0)
        return n == 3;
    for (uint64_t p = 5; p * p <= n; p += 4) {
        if (n % p == 0)
            return false;
        p += 2;
        if (n % p == 0)
            return false;
    }
    return true;
}

int main() {
    setlocale(LC_ALL, "");
    printf(" n          M(n)             Prime?\n");
    printf("-----------------------------------\n");
    uint64_t m0 = 1, m1 = 1;
    for (uint64_t i = 0; i < 42; ++i) {
        uint64_t m =
            i > 1 ? (m1 * (2 * i + 1) + m0 * (3 * i - 3)) / (i + 2) : 1;
        printf("%2llu%'25llu  %s\n", i, m, is_prime(m) ? "true" : "false");
        m0 = m1;
        m1 = m;
    }
}


  

You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm String length step by step in the Frink programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the Julia programming language
You may also check:How to resolve the algorithm Chowla numbers step by step in the Raku programming language