How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Zig programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Zig programming language

Table of Contents

Problem Statement

Print out the first   15   Catalan numbers by extracting them from Pascal's triangle.

Pascal's triangle

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Zig programming language

Source code in the zig programming language

const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();

    var n: u32 = 1;
    while (n <= 15) : (n += 1) {
        const row = binomial(n * 2).?;
        try stdout.print("{d:2}  {d:8}\n", .{ n, row[n] - row[n + 1] });
    }
}

pub fn binomial(n: u32) ?[]const u64 {
    if (n >= rmax)
        return null
    else {
        const k = n * (n + 1) / 2;
        return pascal[k .. k + n + 1];
    }
}

const rmax = 68;

// evaluated and created at compile-time
const pascal = build: {
    @setEvalBranchQuota(100_000);
    var coefficients: [(rmax * (rmax + 1)) / 2]u64 = undefined;
    coefficients[0] = 1;
    var j: u32 = 0;
    var k: u32 = 1;
    var n: u32 = 1;
    while (n < rmax) : (n += 1) {
        var prev = coefficients[j .. j + n];
        var next = coefficients[k .. k + n + 1];
        next[0] = 1;
        var i: u32 = 1;
        while (i < n) : (i += 1)
            next[i] = prev[i] + prev[i - 1];
        next[i] = 1;
        j = k;
        k += n + 1;
    }
    break :build coefficients;
};


  

You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Factor programming language
You may also check:How to resolve the algorithm Playing cards step by step in the Phix programming language
You may also check:How to resolve the algorithm Loops/For step by step in the J programming language
You may also check:How to resolve the algorithm Brace expansion step by step in the Prolog programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Elixir programming language