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
 

Related to Converting decimal to Roman Literal using programming?

1. What is the basic approach to convert a decimal number to a Roman numeral in programming?

The basic approach involves creating a mapping of decimal values to their corresponding Roman numerals. The algorithm iterates through the decimal values in descending order, subtracting the value from the input number and appending the corresponding Roman numeral to the result string until the input number is reduced to zero.

2. What are the main Roman numeral symbols and their decimal values?

The main Roman numeral symbols and their decimal values are: I (1), V (5), X (10), L (50), C (100), D (500), and M (1000). Additionally, combinations like IV (4), IX (9), XL (40), XC (90), CD (400), and CM (900) are used to represent specific values.

3. How do you handle subtractive combinations in the conversion process?

Subtractive combinations are handled by including them in the mapping of decimal values to Roman numerals. During the conversion process, these combinations are checked and used before the individual symbols. For example, 4 is represented as IV instead of IIII, and 9 is represented as IX instead of VIIII.

4. Can you provide a sample code snippet in Python to convert decimal to Roman numeral?

Sure! Here is a sample Python code snippet:

def int_to_roman(num): val = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ] syb = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ] roman_num = '' i = 0 while num > 0: for _ in range(num // val[i]): roman_num += syb[i] num -= val[i] i += 1 return roman_num# Example usage:print(int_to_roman(1994)) # Output: MCMXCIV

5. What are some common pitfalls to avoid when converting decimal to Roman numeral?

Some common pitfalls include not correctly handling subtractive combinations, not iterating through the decimal values in descending order, and not properly reducing the input number during each iteration. Ensuring the mapping is accurate and the logic correctly implements the conversion rules will help avoid these issues.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • 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
61K
Back
Top