Converting decimal to Roman Literal using programming?

  • Comp Sci
  • Thread starter shivajikobardan
  • Start date
  • Tags
    Programming
In summary: X XXC CD100 100 1000001000 1000 100000010000 10000 10000000 var dict = { 1: "I", 4: "IV", 5: "V", 9: "IX", 10: "X", 40: "XL", 50: "L", 90: "XC", 100: "C", 500: "D", 900: "CM", 1000: "M",};ordinary_literal = Number(prompt("Enter the value of year in foramt 2010
  • #1
shivajikobardan
674
54
Homework Statement
Converting decimal to Roman Literal using programming?
Relevant Equations
None
JavaScript:
var dict = {
  1: "I",
  5: "V",
  10: "X",
  50: "L",
  100: "C",
  500: "D",
  1000: "M",
};

ordinary_literal = Number(prompt("Enter the value of year in foramt 2010"));
//2010 as input
let roman_value = "";
let minDifference = Infinity;

while (ordinary_literal > 0) {
  for (var key in dict) {
    var difference = Math.abs(ordinary_literal - key);
    if (difference < minDifference) {
      minDifference = difference;
      key_to_use = key;
    }
  }

  roman_value += dict[key_to_use];
  ordinary_literal -= key_to_use;
}

console.log(roman_value);

The code fails when pressed 9.
It's because of dictionary. But how do I realize what's missing in dictionary? how do I realize what else to add in this dictionary? Is there a technique to do so?
 
Physics news on Phys.org
  • #2
So you want it to print out IX for 9 but it printed out VIIII instead?
 
  • #3
jedishrfu said:
So you want it to print out IX for 9 but it printed out VIIII instead?
it printed X for 9.
 
  • #4
I don't have a specific algorithm in mind, but it appears that you have a lot less logic in your code than is needed. Roman numerals are a bit tricky, as certain numbers need to be handled as special cases, particularly 4's and 9's that can appear in various places in the decimal representation.
4 --> IV
9 --> IX
and similar for 14, 19, 24 29, etc.

40 --> XL
90 --> XC
and similar for 140, 190, 240, 290, etc.

400 --> CD
900 --> CM
and similar for 1400, 1900, 2400, 2900, etc.

Your difficulty with 9 showing up as X is a completely different situation. A good way to find the problem is to hand-simulate your code; that is, sit down with paper and pencil and analyze what your program is doing when you enter 9 as the input. An alternative is to use the Javascript debugger to single-step through your code to inspect the variable values at each step.
 
  • #5
shivajikobardan said:
The code fails when pressed 9.
I'm guessing that's the first number you tried. I suspect that if you enter 2010 you'll get incorrect results as well.

Your algorithm needs to be refined, and by that I mean, completely rewritten. It's reasonable to assume that the largest input number is 3999, as there isn't a Roman numeral for 4000, as far as I'm aware -- they didn't usually put four Roman numerals of the same kind together, so you won't see IIII where they would use IV, or MMMM for 4000. Also there's not a symbol I know of for 5000.

The way I would do the problem is to figure out how many thousands there are ( < 4), how many hundreds, how many tens, how many ones, with special cases for 4 or 9 digits.
 
  • Like
Likes jedishrfu
  • #6
Look at your algorithm where you do abs.

10 was closer to 9 than 5 was right? But 10 is greater than 9 so what should you have done.

As @Mark44 has said in an earlier post Roman numerals are a bit more complex than your algorithm allows and you'll need to rethink it.
 
  • #7
JavaScript:
var dict = {
  1: "I",
  4: "IV",
  5: "V",
  9: "IX",
  10: "X",
  40: "XL",
  50: "L",
  90: "XC",
  100: "C",
  500: "D",
  900: "CM",
  1000: "M",
};
ordinary_literal = Number(prompt("Enter the value of year in foramt 2010"));
//2010 as input
let roman_value = "";
let minDifference = Infinity;
while (ordinary_literal > 0) {
  for (var key in dict) {
    var difference = Math.abs(ordinary_literal - key);
    if (difference < minDifference) {
      minDifference = difference;
      key_to_use = key;
    }
  }
  roman_value += dict[key_to_use];
  ordinary_literal -= key_to_use;
}
console.log(roman_value);
Does this work? I've added couple of dictionary values. no, it doesn't work.
 
Last edited:
  • #8
shivajikobardan said:
Does this work? I've added couple of dictionary values. no, it doesn't work
All you did was to add a few key/value pairs in your dictionary, but you didn't change the rest of your code, as far as I can see with a quick scan. As I said before, you need to completely revise your code if you want this to work. See post #5 for my suggestion.
 
  • #9
Mark44 said:
The way I would do the problem is to figure out how many thousands there are ( < 4), how many hundreds, how many tens, how many ones, with special cases for 4 or 9 digits.
Or look up each decimal digit in a 2-dimensional table / array similar to this:

Code:
     1   10   100  1000
1    I    X     C     M
2   II   XX    CC    MM
3  III  XXX   CCC   MMM
4   IV   XL    CD
5    V    L     D
6   VI   LX    DC
7  VII  LXX   DCC
8 VIII LXXX  DCCC
9   IX   XC    CM
 

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
5K
  • Programming and Computer Science
Replies
7
Views
15K
  • Introductory Physics Homework Help
Replies
3
Views
3K
  • Sticky
  • Aerospace Engineering
2
Replies
48
Views
60K
Back
Top