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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Mayan numerals step by step in the CLU 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 CLU programming language

Source code in the clu programming language

% This program must be linked with PCLU's "misc.lib" and "useful.lib"

base20 = proc (n: bigint) returns (sequence[int])
    own zero: bigint := bigint$i2bi(0)
    own twenty: bigint := bigint$i2bi(20)    
    if n=zero then return(sequence[int]$[0]) end
    
    digits: array[int] := array[int]$[]
    while n>zero do
        array[int]$addl(digits, bigint$bi2i(n//twenty))
        n := n/twenty
    end
    return(sequence[int]$a2s(digits))
end base20

mayan = proc (digits: sequence[int]) returns (string)
    own parts: array[string] := array[string]$[0:
        "    ", " .  ", " .. ", "... ", "....", "----"
    ]
    % generate edges
    edge: stream := stream$create_output()
    for i: int in int$from_to(1, sequence[int]$size(digits)) do
        stream$puts(edge, "+----")
    end
    stream$putl(edge, "+")

    % generate digits
    lines: stream := stream$create_output()
    for i: int in int$from_to_by(15, 0, -5) do
        for d: int in sequence[int]$elements(digits) do
            p: int := d-i
            if p<0 then p:=0 end
            if p>5 then p:=5 end
            if i=0 & p=0
                then stream$puts(lines, "| @  ")
                else stream$puts(lines, "|" || parts[p])
            end
        end
        stream$putl(lines, "|")
    end
    
    s_edge: string := stream$get_contents(edge)
    return(s_edge || stream$get_contents(lines) || s_edge)
end mayan

start_up = proc ()
    po: stream := stream$primary_output()
    n: bigint := bigint$parse(sequence[string]$bottom(get_argv()))
    stream$puts(po, mayan(base20(n)))
end start_up

  

You may also check:How to resolve the algorithm Natural sorting step by step in the Sidef programming language
You may also check:How to resolve the algorithm Self numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm Parallel brute force step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Check input device is a terminal step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the Sidef programming language