Author Topic: Roman Numeral Decode (Rosetta Code Task)  (Read 4030 times)

0 Members and 1 Guest are viewing this topic.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Roman Numeral Decode (Rosetta Code Task)
« on: April 16, 2021, 11:03:32 am »
This program I originally submitted to Rosetta in 2018 for the Roman Numeral Decode task under TechBASIC.

Here is the QB64 Version:

Code: QB64: [Select]
  1. '*** Roman Numerals Decoder
  2. '***
  3. '*** By George McGinn (03/30/2018)
  4. '***    Modified on 04/16/2021 From TechBASIC to QB64
  5. '***    NOTE: This program was originally submitted to the Rosetta Code
  6. '***          website as part of a solution to one of their challenges
  7. '***          (http://www.rosettacode.org/wiki/Roman_numerals/Decode#TechBASIC)
  8.  
  9. SCREEN _NEWIMAGE(400, 600, 32)
  10.  
  11.  
  12.  
  13. Main:
  14. '------------------------------------------------
  15. ' CALLS THE romToDec FUNCTION WITH THE ROMAN
  16. ' NUMERALS AND RETURNS ITS DECIMAL EQUIVELENT.
  17. '
  18.    PRINT "ROMAN NUMERAL TO DECIMAL CONVERSION"
  19.    PRINT: PRINT
  20.  
  21.    PRINT "MDCCIV  = "; romToDec("MDCCIV") '1704
  22.    PRINT "MCMXC   = "; romToDec("MCMXC") '1990
  23.    PRINT "MMVIII  = "; romToDec("MMVIII") '2008
  24.    PRINT "MDCLXVI = "; romToDec("MDCLXVI") '1666
  25.    PRINT: PRINT
  26.    PRINT "Here are other solutions not from the TASK:"
  27.    PRINT "MCMXCIX = "; romToDec("MCMXCIX") '1999
  28.    PRINT "XXV     = "; romToDec("XXV") '25
  29.    PRINT "CMLIV   = "; romToDec("CMLIV") '954
  30.    PRINT "MMXI    = "; romToDec("MMXI") '2011
  31.    PRINT "MMIIIX  = "; romToDec("MMIIIX") '2011
  32.    PRINT: PRINT
  33.    PRINT "2011 can be written either as MMXI or MMIIIX"
  34.    PRINT "With the IX = 9, MMIIIX is also 2011."
  35.    PRINT "2011 IS CORRECT (MM=2000 + II = 2 + IX = 9)"
  36.  
  37.    END
  38.  
  39.  
  40.  
  41. FUNCTION romToDec (roman AS STRING)
  42. '------------------------------------------------------
  43. ' FUNCTION THAT CONVERTS ANY ROMAN NUMERAL TO A DECIMAL
  44. '
  45.     prenum = 0: num = 0
  46.     LN = LEN(roman)
  47.     FOR i = LN TO 1 STEP -1
  48.         x$ = MID$(roman, i, 1)
  49.         n = 1000
  50.         SELECT CASE x$
  51.                CASE "M": n = n / 1
  52.                CASE "D": n = n / 2
  53.                CASE "C": n = n / 10
  54.                CASE "L": n = n / 20
  55.                CASE "X": n = n / 100
  56.                CASE "V": n = n / 200
  57.                CASE "I": n = n / n
  58.                CASE ELSE: n = 0
  59.         END SELECT
  60.         IF n < prenum THEN num = num - n ELSE num = num + n
  61.         prenum = n
  62.    NEXT i
  63.  
  64.    romToDec = num
  65.  
  66.  
« Last Edit: April 16, 2021, 11:35:35 am by George McGinn »
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Roman Numeral Decode (Rosetta Code Task)
« Reply #1 on: April 16, 2021, 08:23:54 pm »
Interesting. I think that your answer for 2011 (MMXI) may be the simplest correct answer. Using MMIIIX (200+II+IX) requires too much thought. Adding 2 to 9 does indeed result in 11 but so does 3 and 8 or 4 and 7 or 5 and 6. In all of my experience in reading Roman numerals I have never had to think about 'adding' numbers. I look for thousands, hundreds, fifties, tens and singles. Although technically MMIIIX may be correct, in my mind anyway, MMXI is the easiest.... Just my opinion...
Logic is the beginning of wisdom.

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Roman Numeral Decode (Rosetta Code Task)
« Reply #2 on: April 17, 2021, 08:50:13 am »
MMIIIX (200+II+IX) if NOT correct !!!

There can be only one character coming in reduction of the following one:
4 = IV
9 = IX
90 = XC
49=IL
99=IC
400 = CD
900 = CM aso

Apart from the number coming in reduction before the following one (here IX=9), the letters must follow the decreasing order of their value.

The value MMIIIX is a fancy, because then two "I" are smaller as X !!!
 2000 + 2 + 9   ---> oh ! 2 is less than 9 !!! Not possible in roman numeral !!!
2011= 2000+10+1 = MMXI
Always, yes, always, from biggest to smalest !!!












« Last Edit: April 17, 2021, 09:04:43 am by euklides »
Why not yes ?

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: Roman Numeral Decode (Rosetta Code Task)
« Reply #3 on: April 17, 2021, 09:21:04 am »
Actually, the program is correct as the code is interpreting MMIIIX as 2000 + 2 + 9 = 2011!

So the program is right. But I didn't want to write a book about the program. I guess I needed to explain how Roman Numerals work for those not versed in them.

To read for yourself, go to: https://en.wikipedia.org/wiki/Roman_numerals

The actual way or variants of how the Romans used their numbering system allowed for smaller numbers to appear anywhere.

Most people do not realize that when the Romans first used their numbering system, there was no "reduction" system in place. So the way you'd interpret these is to add all the representations from left to right. (M+M+I+I+I+X = 1000+1000+1+1+1+10 = 2013 is just as valid an interpretation as M+M+I+I+IX or 1000+1000+1+1+9
 
When the Roman's came up with "reduction," it was not exclusively used. Both systems were in use during the entire Roman Empire.

My program can be made to add the Roman Numerals the original way by adding a switch to bypass the "additive subtraction."


MMIIIX (200+II+IX) if NOT correct !!!

There can be only one character coming in reduction of the following one:
4 = IV
9 = IX
90 = XC
49=IL
99=IC
400 = CD
900 = CM aso

Apart from the number coming in reduction before the following one (here IX=9), the letters must follow the decreasing order of their value.

The value MMIIIX is a fancy, because then two "I" are smaller as X !!!
 2000 + 2 + 9   ---> oh ! 2 is less than 9 !!! Not possible in roman numeral !!!
2011= 2000+10+1 = MMXI
Always, yes, always, from biggest to smalest !!!
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Roman Numeral Decode (Rosetta Code Task)
« Reply #4 on: April 18, 2021, 05:50:22 am »
Which ever method that is used, I can tell you that my grandkids think it's fascinating, that I can read the the release dates on older movies....
Logic is the beginning of wisdom.

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Roman Numeral Decode (Rosetta Code Task)
« Reply #5 on: April 19, 2021, 05:58:38 am »
It is the role of the grandfather, who like an alchemist is knowing many secrets, to explain to his grandchildren the mysterious mechanism of Roman numerals !!!
Why not yes ?