Mayan numerals: Difference between revisions

Added FreeBASIC
m (syntax highlighting fixup automation)
(Added FreeBASIC)
 
(6 intermediate revisions by 6 users not shown)
Line 430:
|....|----|----|----|----|----|....|----|----|----|----|----|....|
+----+----+----+----+----+----+----+----+----+----+----+----+----+</pre>
 
=={{header|Amazing Hopper}}==
{{Trans|BASIC}}
<syntaxhighlight lang="c">
#include <basico.h>
 
algoritmo
muestra=""
obtener total argumentos
cuando ' sea distinto a( 2 ) '{ terminar }
obtener parámetro alfanumérico (2), guardar en 'muestra'
 
lsimb={}, '" "," . "," .. ","... ","...."'#(utf8("────")), enlistar en 'lsimb'
 
largo=0, #( largo = len(muestra))
sdígitos={}, separar( muestra, sdígitos, "")
ndígitos=0, #( ndígitos = number(sdígitos) )
 
m=0, t=0
#( m = largo-1)
 
// convertir dígitos a base 20,la base de los mayas:
iterar para( j=m, #(j>=1), --j )
iterar para( i=1, #(i<=j), ++i )
bit.and( #(ndígitos[i]), 1 ), por '10',
guardar en 't'
#( ndígitos[i+1] += t )
#( ndígitos[i] = int(ndígitos[i]/2) )
siguiente
siguiente
 
s=1
iterar mientras ( #( ndígitos[s]==0 && s<largo) )
++s
reiterar
// armar los cartuchos:
imprimir ( #(utf8("╔═════")),#(replicate( utf8("═════"), largo-s) ), #(utf8("═╗")), NL,\
#(utf8("║┌────")),#(replicate( utf8("┬────"), largo-s) ), #(utf8("┐║")), NL )
n=0
iterar para ( l=3, #(l>=0), --l )
 
imprimir (#(utf8("║")))
iterar para ( i=s, #(i<=largo), ++i )
si ( bit.or ( #(ndígitos[i]), l ), es igual a '0' )
imprimir ( #(utf8("│ Θ ")) )
sino
guardar ' #(ndígitos[i]-5*l) ' en 'n'
si ( #(n>5) )
n=5
sino si ( n, es negativo? )
n=0
fin si
imprimir( #(utf8("│")), #(lsimb[n+1]) )
fin si
siguiente
imprimir (#(utf8("│║")),NL)
siguiente
 
imprimir ( #(utf8("║└────")),#(replicate( utf8("┴────"), largo-s) ), #(utf8("┘║")), NL,\
#(utf8("╚═════")),#(replicate( utf8("═════"), largo-s) ), #(utf8("═╝")), NL )
saltar
 
terminar
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/nummaya.bas 4005
╔════════════════╗
║┌────┬────┬────┐║
║│ │ │ │║
║│ │ │ │║
║│────│ │ │║
║│────│ Θ │────│║
║└────┴────┴────┘║
╚════════════════╝
 
$ hopper3 basica/nummaya.bas 8017
╔═════════════════════╗
║┌────┬────┬────┬────┐║
║│ │ │ │ .. │║
║│ │ │ │────│║
║│ │ │ │────│║
║│ . │ Θ │ Θ │────│║
║└────┴────┴────┴────┘║
╚═════════════════════╝
 
$ hopper3 basica/nummaya.bas 326205
╔══════════════════════════╗
║┌────┬────┬────┬────┬────┐║
║│ │ │ │ │ │║
║│ │ │────│ │ │║
║│ │ │────│────│ │║
║│ .. │ Θ │────│────│────│║
║└────┴────┴────┴────┴────┘║
╚══════════════════════════╝
 
$ hopper3 basica/nummaya.bas 886205
╔══════════════════════════╗
║┌────┬────┬────┬────┬────┐║
║│ │ │ │ │ │║
║│ │ │────│ │ │║
║│ │────│────│────│ │║
║│────│────│────│────│────│║
║└────┴────┴────┴────┴────┘║
╚══════════════════════════╝
 
$ hopper3 basica/nummaya.bas 98380658201233422
╔═══════════════════════════════════════════════════════════════════════╗
║┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐║
║│ │ │ │ │ │ │ │ │ │ .. │ │ │ │ │║
║│ │ │ │ │ │....│ │ │ │────│....│ │ . │ │║
║│ │ │ │ .. │....│────│....│ │ .. │────│────│ │────│ │║
║│ . │....│ Θ │────│────│────│────│....│────│────│────│... │────│ .. │║
║└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘║
╚═══════════════════════════════════════════════════════════════════════╝
 
</pre>
 
=={{header|APL}}==
Line 796 ⟶ 920:
|style="width:3em;vertical-align:bottom;"|Θ
|}
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">UL: "╔"
UC: "╦"
UR: "╗"
LL: "╚"
LC: "╩"
LR: "╝"
HB: "═"
VB: "║"
 
Mayan: [" ", " ∙ ", " ∙∙ ", "∙∙∙ ", "∙∙∙∙"]
 
M0: " Θ "
M5: "────"
 
toMayan: function [digit][
result: array.of:4 Mayan\0
if digit = 0 [
result\3: M0
return result
]
 
d: digit
loop 3..0 'i [
if? d >= 5 [
result\[i]: M5
d: d - 5
]
else [
result\[i]: Mayan\[d]
break
]
]
return result
]
 
draw: function [mayans][
idx: dec size mayans
 
prints UL
 
loop 0..idx 'i [
loop 0..3 'j [
prints HB
]
prints (i < idx)? -> UC -> UR ++ `\n`
]
 
loop 1..4 'i [
prints VB
loop 0..idx 'j ->
prints mayans\[j]\[dec i] ++ VB
print ""
]
 
prints LL
 
loop 0..idx 'i [
loop 0..3 'j ->
prints HB
prints (i < idx)? -> LC -> LR ++ `\n`
]
]
 
loop [4005, 8017, 326205, 886205, 1081439556] 'n [
print ["Converting" n "to Mayan:"]
digs: digits.base:20 n
mayans: map digs => toMayan
draw mayans
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre>Converting 4005 to Mayan:
╔════╦════╦════╗
║ ║ ║ ║
║ ║ ║ ║
║────║ ║ ║
║────║ Θ ║────║
╚════╩════╩════╝
 
Converting 8017 to Mayan:
╔════╦════╦════╦════╗
║ ║ ║ ║ ∙∙ ║
║ ║ ║ ║────║
║ ║ ║ ║────║
║ ∙ ║ Θ ║ Θ ║────║
╚════╩════╩════╩════╝
 
Converting 326205 to Mayan:
╔════╦════╦════╦════╦════╗
║ ║ ║ ║ ║ ║
║ ║ ║────║ ║ ║
║ ║ ║────║────║ ║
║ ∙∙ ║ Θ ║────║────║────║
╚════╩════╩════╩════╩════╝
 
Converting 886205 to Mayan:
╔════╦════╦════╦════╦════╗
║ ║ ║ ║ ║ ║
║ ║ ║────║ ║ ║
║ ║────║────║────║ ║
║────║────║────║────║────║
╚════╩════╩════╩════╩════╝
 
Converting 1081439556 to Mayan:
╔════╦════╦════╦════╦════╦════╦════╗
║ ∙ ║ ∙∙ ║∙∙∙ ║∙∙∙∙║∙∙∙ ║ ∙∙ ║ ∙ ║
║────║────║────║────║────║────║────║
║────║────║────║────║────║────║────║
║────║────║────║────║────║────║────║
╚════╩════╩════╩════╩════╩════╩════╝</pre>
 
=={{header|AutoHotkey}}==
Line 1,184 ⟶ 1,422:
|....|----|----|----|----|----|....|
+----+----+----+----+----+----+----+</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
 
const std::vector<std::string> MAYAN_DIGITS = { " Θ ", " ∙ ", " ∙∙ ", "∙∙∙ ", "∙∙∙∙", "────" };
const std::string BLANK = " ";
 
std::unordered_map<std::string, std::string> BORDER { { "horizontal_beam", "═" }, { "vertical_beam", "║" },
{ "lower_left", "╚" }, { "lower_central", "╩" }, { "lower_right", "╝" },
{ "upper_left", "╔" }, { "upper_central", "╦" }, { "upper_right", "╗" } };
 
std::vector<int32_t> to_base20(const int32_t& number) {
std::vector<int32_t> result;
result.emplace_back(number % 20);
int32_t n = number / 20;
while ( n != 0 ) {
result.emplace_back(n % 20);
n /= 20;
}
std::reverse(result.begin(), result.end());
return result;
}
 
std::vector<std::string> to_mayan_numeral(int32_t digit) {
std::vector<std::string> result(4, BLANK);
if ( digit == 0 ) {
result[3] = MAYAN_DIGITS[0];
return result;
}
 
for ( int32_t i = 3; i >= 0; --i ) {
if ( digit >= 5 ) {
result[i] = MAYAN_DIGITS[5];
digit -= 5;
} else {
result[i] = ( digit == 0 ) ? BLANK : MAYAN_DIGITS[digit];
break;
}
}
return result;
}
 
void display(const std::vector<std::vector<std::string>>& numerals) {
const int32_t index = numerals.size() - 1;
 
std::cout << BORDER["upper_left"];
for ( int32_t i = 0; i <= index; ++i ) {
for ( int32_t j = 0; j <= 3; ++j ) {
std::cout << BORDER["horizontal_beam"];
}
if ( i < index ) {
std::cout << BORDER["upper_central"];
} else {
std::cout << BORDER["upper_right"] << std::endl;
}
}
 
for ( int32_t i = 1; i <= 4; ++i ) {
std::cout << BORDER["vertical_beam"];
for ( int32_t j = 0; j <= index; ++j ) {
std::cout << numerals[j][i - 1] + BORDER["vertical_beam"];
}
std::cout << std::endl;
}
 
std::cout << BORDER["lower_left"];
for ( int32_t i = 0; i <= index; ++i ) {
for ( int32_t j = 0; j <= 3; ++j ) {
std::cout << BORDER["horizontal_beam"];
}
if ( i < index ) {
std::cout << BORDER["lower_central"];
} else {
std::cout << BORDER["lower_right"] << std::endl;
}
}
}
 
int main() {
for ( const int32_t& base10 : { 4'005, 8'017, 326'205, 886'205, 1'081'439'556 } ) {
std::cout << "Base 10 number, " << base10 << " to Mayan:" << std::endl;
std::vector<int32_t> digits = to_base20(base10);
std::vector<std::vector<std::string>> mayans;
for ( const int32_t& digit : digits ) {
std::vector<std::string> mayan = to_mayan_numeral(digit);
mayans.push_back(mayan);
}
display(mayans);
std::cout << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
Base 10 number, 4005 to Mayan:
╔════╦════╦════╗
║ ║ ║ ║
║ ║ ║ ║
║────║ ║ ║
║────║ Θ ║────║
╚════╩════╩════╝
 
Base 10 number, 8017 to Mayan:
╔════╦════╦════╦════╗
║ ║ ║ ║ ∙∙ ║
║ ║ ║ ║────║
║ ║ ║ ║────║
║ ∙ ║ Θ ║ Θ ║────║
╚════╩════╩════╩════╝
 
Base 10 number, 326205 to Mayan:
╔════╦════╦════╦════╦════╗
║ ║ ║ ║ ║ ║
║ ║ ║────║ ║ ║
║ ║ ║────║────║ ║
║ ∙∙ ║ Θ ║────║────║────║
╚════╩════╩════╩════╩════╝
 
Base 10 number, 886205 to Mayan:
╔════╦════╦════╦════╦════╗
║ ║ ║ ║ ║ ║
║ ║ ║────║ ║ ║
║ ║────║────║────║ ║
║────║────║────║────║────║
╚════╩════╩════╩════╩════╝
 
Base 10 number, 1081439556 to Mayan:
╔════╦════╦════╦════╦════╦════╦════╗
║ ∙ ║ ∙∙ ║∙∙∙ ║∙∙∙∙║∙∙∙ ║ ∙∙ ║ ∙ ║
║────║────║────║────║────║────║────║
║────║────║────║────║────║────║────║
║────║────║────║────║────║────║────║
╚════╩════╩════╩════╩════╩════╩════╝
</pre>
 
=={{header|CLU}}==
Line 1,511 ⟶ 1,889:
|}
 
=={{header|FōrmulæFreeBASIC}}==
<syntaxhighlight lang="vbnet">Dim As Integer i, j, m, n, s, v
Dim As String maya(5), num
For i = 0 To 5
Read maya(i)
Next i
Const ul = Chr(201), uc = Chr(203), ur = Chr(187), ll = Chr(200)
Const lc = Chr(202), lr = Chr(188), hb = Chr(205), vb = Chr(186)
 
Dim As Longint numbers(0 To ...) = {4005, 8017, 326205, 886205, 1081439556, 18380658207197784}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
For v = 0 To Ubound(numbers)
num = Str(numbers(v))
Print "Converting"; numbers(v); " to Mayan:"
m = Len(num)
Redim As Integer D(m)
For i = 1 To m
D(i) = Val(Mid(num,i,1))
Next i
For j = m-1 To 1 Step -1
For i = 1 To j
D(i+1) += 10*(D(i) And 1)
D(i) \= 2
Next i
Next j
s = 1
Do While D(s) = 0 And s < m
s += 1
Loop
For i = s To m
Print Iif(i < m-1, ul+hb+hb+hb+hb, uc+hb+hb+hb+hb);
Next i
Print ur
For j = 3 To 0 Step -1
For i = s To m
If (D(i) Or j) = 0 Then
Print vb + " @ ";
Else
n = D(i)-5*j
If n > 5 Then
n = 5
Elseif n < 0 Then
n = 0
End If
Print vb; maya(n);
End If
Next i
Print vb
Next j
For i = s To m
Print Iif(i < m-1, ll+hb+hb+hb+hb, lc+hb+hb+hb+hb);
Next i
Print lr + Chr(10) '"+"
Next v
Sleep
Data " "," . "," .. ","... ","....","----"</syntaxhighlight>
{{out}}
<pre>Same as BASIC entry.</pre>
 
=={{header|Fōrmulæ}}==
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
{{FormulaeEntry|page=https://formulae.org/?script=examples/Mayan_numerals}}
 
In '''[https://formulae.org/?example=Mayan_numerals this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
Line 2,723 ⟶ 3,158:
# decimal number to base-20 array, most significant digit first
def dec2vig:
[recurse(if . >= 020 then ./20|floor else empty end) | . % 20]
| if length > 1 then .[:-1] else . end
| reverse;
Line 4,436 ⟶ 4,870:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv
 
var ul = "╔"
2,169

edits