• Arabic to Roman Number Conversion

    From porkchop@porkchop@invalid.foo (Mike Sanders) to comp.lang.awk on Sat Oct 28 00:22:11 2023
    From Newsgroup: comp.lang.awk

    # tags: roman, numbers, awk, code
    #
    # Arabic to Roman Number Conversion
    # Michael Sanders 2023
    # https://busybox.neocities.org/notes/arabic-to-roman.txt
    #
    # example usage: echo 6 | awk -f arabic-to-roman.txt
    #
    # note: using standard Roman numerals, the largest number
    # that can be represented is 3,999, written as MMMCMXCIX

    function toRoman(n) {
    if (n !~ /^[0-9]+$/ || n < 1 || n > 3999) return "invalid input"
    split("I IV V IX X XL L XC C CD D CM M", roman)
    split("1 4 5 9 10 40 50 90 100 400 500 900 1000", arabic)
    # loop through array in reverse order building romanNum
    for (x = 13; x >= 1; x--) {
    while (n >= arabic[x]) {
    romanNum = romanNum roman[x]
    n -= arabic[x]
    }
    }
    return romanNum
    }

    { print toRoman($1) }

    # eof
    --
    :wq
    Mike Sanders

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.lang.awk on Sat Oct 28 05:00:41 2023
    From Newsgroup: comp.lang.awk

    Some years ago I wrote an tool 'aroma' (and I think I also posted
    a version here) for arabic <-> roman (bidirectional) conversion.
    Here is the version I still have in my local bin directory...

    Have fun!

    Janis


    # aroma - yet another arabic number / roman number converter
    #
    # Historically, a couple variants of roman numbers have been
    # around, a couple alphabets were in use in different regions
    # and times and various rules to build and interpret roman
    # numbers were existing. In this code we restrict to the basic
    # alphabet I, V, X, L, C, D, M and allow simple subtractive
    # abbreviations IV, IX, XL, XC, CD, CM; this is often called
    # the normal form of the roman numbers. (Deviations from that
    # form, e.g. numbers like 'IM' or 'IIX', are not supported.)
    #
    # Janis Papanagnou, 2010

    awk -v n="${1?Please provide a number (arabic or roman) as argument!}" '
    BEGIN {
    split ("1000 900 500 400 100 90 50 40 10 9 5 4 1", ar)
    split ("M CM D CD C XC L XL X IX V IV I", ro)
    if (n ~ /^[[:digit:]]+$/) {
    print ar2ro(n) # output in unique normal form
    } else if (n ~ /^[MDCLXVI]*$/) {
    x = ro2ar(n) # a consistency test follows
    print (ar2ro(x) == n) ? x : "NaN"
    } else {
    print "NaN"
    }
    }

    function ro2ar (r, a, i)
    {
    a = 0
    for (i=1; r && ro[i]; i++)
    {
    l = length (ro[i])
    while (substr (r,1,l) == ro[i]) {
    a += ar[i]
    r = substr (r,l+1)
    }
    }
    return (r == "") ? a : "NaN"
    }

    function ar2ro (a, r, i)
    {
    for (i=1; ar[i]; i++) {
    while (a >= ar[i]) {
    a -= ar[i]
    r = r ro[i]
    }
    }
    return r
    }
    '

    # vim: ts=4 sw=4 syntax=awk



    On 28.10.2023 02:22, Mike Sanders wrote:
    # tags: roman, numbers, awk, code
    #
    # Arabic to Roman Number Conversion
    # Michael Sanders 2023
    # https://busybox.neocities.org/notes/arabic-to-roman.txt
    #
    # example usage: echo 6 | awk -f arabic-to-roman.txt
    #
    # note: using standard Roman numerals, the largest number
    # that can be represented is 3,999, written as MMMCMXCIX

    function toRoman(n) {
    if (n !~ /^[0-9]+$/ || n < 1 || n > 3999) return "invalid input"
    split("I IV V IX X XL L XC C CD D CM M", roman)
    split("1 4 5 9 10 40 50 90 100 400 500 900 1000", arabic)
    # loop through array in reverse order building romanNum
    for (x = 13; x >= 1; x--) {
    while (n >= arabic[x]) {
    romanNum = romanNum roman[x]
    n -= arabic[x]
    }
    }
    return romanNum
    }

    { print toRoman($1) }

    # eof


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From porkchop@porkchop@invalid.foo (Mike Sanders) to comp.lang.awk on Sat Oct 28 03:39:21 2023
    From Newsgroup: comp.lang.awk

    Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
    Some years ago I wrote an tool 'aroma' (and I think I also posted
    a version here) for arabic <-> roman (bidirectional) conversion.
    Here is the version I still have in my local bin directory...

    Have fun!

    Janis

    # aroma - yet another arabic number / roman number converter

    [...]

    This is really nice Janis & conversion both ways too, cool =)
    Thanks for sharing this, have added yours to my personal notes.

    Here, I lowercase output, good for footnotes: ('see also [iii]')
    --
    :wq
    Mike Sanders

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From yeti@yeti@tilde.institute to comp.lang.awk on Sat Oct 28 05:06:26 2023
    From Newsgroup: comp.lang.awk

    Related with a fun factor:

    Stand-up Maths
    How Roman numerals broke the official dog database. https://www.youtube.com/watch?v=jMxoGqsmk5Y
    --
    Fake signature.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From porkchop@porkchop@invalid.foo (Mike Sanders) to comp.lang.awk on Sat Oct 28 10:04:13 2023
    From Newsgroup: comp.lang.awk

    yeti <yeti@tilde.institute> wrote:

    Related with a fun factor:

    Stand-up Maths
    How Roman numerals broke the official dog database. https://www.youtube.com/watch?v=jMxoGqsmk5Y

    Ahh... nice. Sometimes youtube is really good =)
    --
    :wq
    Mike Sanders

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.lang.awk on Sat Oct 28 15:06:30 2023
    From Newsgroup: comp.lang.awk

    On 28.10.2023 07:06, yeti wrote:
    Related with a fun factor:

    Stand-up Maths
    How Roman numerals broke the official dog database. https://www.youtube.com/watch?v=jMxoGqsmk5Y

    Nice!

    This video mentions - as Mike's code did[*] - the number
    3999 as maximum roman number; *one* link I found mentions
    a "not more than three same digits _principle_". Actually
    I could not find any such principle. From early days I've
    learned that a sequence of MMMMMMMM is just too bulky in
    practice but not that it would be incorrect. Wikipedias
    mention that there were many ways to face that bulkiness
    with new notations. And also that sequences of four digits
    were and still are quite usual. The English Wikipedia has
    a table that stops at MMM, but I'm not sure whether just
    for convenience or because of some general "principle"[**].

    When using computers there's no reason to stop after 3999,
    I'd say.

    Janis

    [*] ... || n < 1 || n > 3999) return "invalid input"

    [**] Any (authoritative) links appreciated.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From yeti@yeti@tilde.institute to comp.lang.awk on Sun Oct 29 00:53:24 2023
    From Newsgroup: comp.lang.awk

    Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:

    When using computers there's no reason to stop after 3999,
    I'd say.

    They just would have had a y4k problem and would find a fix for that.

    But only having 3999 addresses would have been even worse than coding on
    an 8086 (64k pages) or even a SC/MP (4k pages).

    Maybe Cisterian numerals would have helped to push that to y10k?

    <https://en.wikipedia.org/wiki/Cistercian_numerals>

    But with increasing use of math(s) and science they would have needed an
    easier number system. Even far below the finite count of subatomic
    particles in the universe, combinations and similar concepts make counts explode.

    And calculations in Roman numerals must have been hell. Are there known algorithms?

    And the missing zero!

    That was a number system for the "you are what you have" era.

    A shepherd is someone with sheep.

    No sheep? => Not a shepherd!
    :-Þ
    --
    |rom The Future. +++ Breaking News From The Future. +++ Breaking News F|
    | The USoA are switching to the binary number system because |
    | having more than 1+1 distinct digits is far too woke. |
    |+ #MABA + #makeAmericaBinaryAgain + #USA + #USoA + #woke + #MABA + #ma|
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.lang.awk on Sun Oct 29 13:41:41 2023
    From Newsgroup: comp.lang.awk

    On 29.10.2023 02:53, yeti wrote:
    Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:

    When using computers there's no reason to stop after 3999,
    I'd say.

    They just would have had a y4k problem and would find a fix for that.
    [...]

    But only having 3999 addresses would have been even worse than coding on
    an 8086 (64k pages) or even a SC/MP (4k pages).
    [...]

    Sure. - My question was whether it makes sense to hard-code such a
    constant in a computer program (that is only run digitally) in the
    first place.[*] If someone wants to see a date, say 6543, in roman
    and he just gets "invalid input" is worse than "MMMMMMDXLIII", IMO.

    One [valid] reason for an error message might be if it's strictly
    "undefined" (by some relevant instance) when values grow larger;
    but I haven't found any. Even the web page I found that speaks of
    such a "not-four-consecutive-digits rule" starts with a photo that
    depicts a tower clock with 'IIII'; and this is obviously a common representation (from ancient usages till now) in roman numerals.

    Moreover, if you inspect the historic and scientific documents you
    observe that there had been also _so many_ of variants[**] that it
    appears strange in that light to think of such a rule.[***]

    That's why I was asking for a relevant reference for such a rule or
    principle.

    Janis

    [*] We nowadays also calculate Pi to ludicrous accuracies (and "no
    one" is attempting to print these digits on paper).

    [**] Even the humorously intended "subtracting two" that we see in
    the video that was posted upthread is one of the existing historic
    variants.

    [***] In practice folks use (inferior) [number-]systems as they fit
    and extend them when necessary. I would expect that they first try
    to stretch the existing system, then to enhance it by inventing new
    digits. (As has been done for large roman numerals.) Dropping that
    inferior (for reasons as you've described) system completely usually
    is a disruptive process that requires more wisdom and persistence to
    implement in society broadly.[****]

    [****] E.g., see climate crisis and traffic congestion in cities and
    (OTOH) abandonment of cars, for example. - But I am digressing.

    --- Synchronet 3.20a-Linux NewsLink 1.114