How to resolve the algorithm Mayan numerals step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Mayan numerals step by step in the Cowgol programming language

Table of Contents

Problem Statement

Present numbers using the Mayan numbering system   (displaying the Mayan numerals in a cartouche).

Normally, Mayan numbers are written vertically   (top─to─bottom)   with the most significant numeral at the top   (in the sense that decimal numbers are written left─to─right with the most significant digit at the left).   This task will be using a left─to─right (horizontal) format,   mostly for familiarity and readability,   and to conserve screen space (when showing the output) on this task page.

Mayan numerals   (a base─20 "digit" or glyph)   are written in two orientations,   this task will be using the "vertical" format   (as displayed below).   Using the vertical format makes it much easier to draw/construct the Mayan numerals (glyphs) with simple dots (.) and hyphen (-);     (however, round bullets (•) and long dashes (─) make a better presentation on Rosetta Code).

Furthermore, each Mayan numeral   (for this task)   is to be displayed as a cartouche   (enclosed in a box)   to make it easier to parse (read);   the box may be drawn with any suitable (ASCII or Unicode) characters that are presentable/visible in all web browsers.

Mayan numerals (glyphs) were added to the Unicode Standard in June of 2018   (this corresponds with version 11.0).   But since most web browsers don't support them at this time,   this Rosetta Code task will be constructing the glyphs with "simple" characters and/or ASCII art.

The Mayan numbering system has the concept of   zero,   and should be shown by a glyph that represents an upside─down (sea) shell,   or an egg.   The Greek letter theta   (Θ)   can be used   (which more─or─less, looks like an egg).   A   commercial at   symbol   (@)   could make a poor substitute.

The Mayan numbering system is a   [vigesimal (base 20)]   positional numeral system.

Note that the Mayan numeral   13   in   horizontal   format would be shown as:

Other forms of cartouches (boxes) can be used for this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Mayan numerals step by step in the Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";
include "argv.coh";

# convert number to base 20
sub base20(n: uint32, out: [uint8]): (n_digits: uint8) is
    n_digits := 0;
    loop
        [out] := (n % 20) as uint8;
        n := n / 20;
        out := @next out;
        n_digits := n_digits + 1;
        if n == 0 then break; end if;
    end loop;
end sub;

# get the N'th line (from the top) for a Mayan digit
sub digit_line(n: uint8, line: uint8): (s: [uint8]) is
    var parts: [uint8][] := {"    "," .  "," .. ","... ","....","----"};
    if n == 0 then
        if line == 3 then s := " @  ";
        else s := parts[0];
        end if;
    else
        var nn := n - 5*(3-line);
        if nn > 128 then s := parts[0];
        elseif nn > 5 then s := parts[5];
        else s := parts[nn];
        end if;
    end if;
end sub;

# print Mayan number
sub print_mayan(n: uint32) is
    sub edge(n: uint8) is
        while n>0 loop
            print("+----");
            n := n-1;
        end loop;
        print_char('+');
        print_nl();
    end sub;
    
    var digits: uint8[8]; # 8 digits is enough for 2**32
    var size := base20(n, &digits[0]);
    
    edge(size);
    var line: uint8 := 0;
    while line < 4 loop 
        var d: uint8 := size-1;
        loop
            print_char('|');
            print(digit_line(digits[d], line));
            if d==0 then break; end if;
            d := d - 1;
        end loop;
        print_char('|');
        print_nl();
        line := line + 1;
    end loop;
    edge(size);
end sub;

sub Error() is
    print("usage: mayan . number must be positive");
    print_nl();
    ExitWithError();
end sub;

# read number from command line
ArgvInit();
var arg := ArgvNext();
if arg == 0 as [uint8] then Error(); end if;
var n: int32;
(n, arg) := AToI(arg);
if n <= 0 then Error(); end if;
print_mayan(n as uint32);

  

You may also check:How to resolve the algorithm XML/XPath step by step in the Java programming language
You may also check:How to resolve the algorithm Magic squares of singly even order step by step in the Raku programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Perl programming language
You may also check:How to resolve the algorithm Zebra puzzle step by step in the Curry programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the PL/I programming language