• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arabic numeral to roman numeral  RSS feed

 
kennith stomps
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to come up with an algorithm that will convert an arabic numeral to a roman numeral. I have been watching video after video, on how the process is to be done, but I cannot seem to entirely grasp the concept, on how this works.  Here is my code thus far, I know it should be easy, but I am totally stuck.

 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to go back to the paper and pencil. Roman numerals work as Arabic numbers do, in units, tens, hundreds and thousands. At which point Roman numerals stop, even though Arabic numerals (so called because they were introduced to Europe by the Arabs, but they were probably invented in India) can go on for millions of digits if you have a large enough sheet of paper. Roman numerals work on having number forms for 1,2,3,4 and 5,6,7,8,9. The same applies with 10,20,30,40 an 50,60,70,80,90. And the hundreds, but 4x and 9x usually have different forms from their predecessors.
I suggest you shou‍ld start by writing rules, e.g.
  • If the last digit is 6, the number will end VI.
  • If the last digit is 0, we ignore it.


  • An alternative way to look at Roman numbers is to group 4,5,6,7,8 as containing a V character, as long as you use 4=IV not 4=IIII.
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay, how would you go about specifying whether the last digit is 6?
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If (number > 10)
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this is the only route I have been able to think of thus far.... something like,
    if (number > 10 && number < 50)
    //I output one X for every 10 digits,
    then maybe use the modulus operator and if number is greater than 5 output a V, and then use modulus again, and output I for every remaining value.

    Something like this perhaps? and keep going with if statements until I get to 3999?  Their has to be a simpler way
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sorry for the 3 replys prior, keep pressing some key that makes it post before I am finished typing
     
    Piet Souris
    Rancher
    Posts: 2015
    71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    for a nice short introduction read here: Project Euler: roman numerals.
    See assignment 89 for some nice training.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7927
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For every power of ten, there is a Roman letter representing the digits 1 and 5. You get the digits 1-3 by concatenating the letter for 1 as many times. You get the digits 4-8 by adding or subtracting the digit for 5 with the digit for 1-3. You get the digits for 9 by subtracting the digit for 1 from the digit for 1 of the next power of ten. Subtraction works by putting the smaller digits before the larger digits. Here is an example:

    1948. First split in powers of ten:

    Get the digits for 1 and 5 for each power of ten:
    power of tendigit for 1digit for 5
    10^0IV
    10^1XL
    10^2DC
    10^3M

    Replace each Arabic digit with the Roman letters appropriate for that power of ten:
    power of tenArabic digitRoman digitexplanation
    10^08VIII5 + 3
    10^14XL50 - 10
    10^29CM1000 - 100
    10^31M1000

    Now concatenate all the Roman digits from largest power to least power: MCMXLVIII

    I think you should start by initializing a list containing the Roman digits for 1 and 5, given a certain power of ten.
    Then, write a method that when given a power of ten, can convert an Arabic digit to a Roman digit.

    Finally, write a method that splits an Arabic numeral into powers of ten, converts each digit to the Roman digit appropriate for that power of ten, and concatenates all the digits.
     
    Piet Souris
    Rancher
    Posts: 2015
    71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Beware that 49 is not IL.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7927
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You would only get IL if you forgot to split the number in powers of ten first.

    First convert 4*10^1 and 9*10^0 to XL and IX, then concatenate them: XLIX.
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    is this anywhere on the rite track
    [code]
    import java.util.Scanner;
    public class Main {

    public static void main(String[] args) {

    string romanNumeral [] = { "I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
    int  arabicNumeral [] = { 1,4,5,9,10,40,50,90,100,400,500,900,1000};
    }

    System.out.println("Please enter a valid number you wish to convert");
    Scanner input = new Scanner(System.in);
    int num = input.nextInt();
    if ( num > 3999 || num == 0 || num < -1 );
    System.out.println("invalid input");
    int beg, end;
    while (num!=-1 && number > 0);{
    for ( beg = 6; beg > 0; beg--){
    if(num >= arabicNumeral[beg]){
    break;
    }
    }
    for ( end = 0; end < 7; end++){
    if(n<=decimal[end]){
    break;
    }
    }
    d = num;
    while(d!=0){
    d/=10;
    if(d<10){
    break;
    }

    }
    if(n<10 && n==4){
    System.out.println()
    }

    }

    }

    [code]
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7927
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not really. The main problem is that you're trying to do too much at once. Start by writing a method that converts the values 1-9 to Roman numerals.
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    just roman numerals converted to 10 would just be something like this

     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The problem comes after this, where you need some kind of a for loop I am guessing to automate which strings are assigned to which int values.

    for( I = romanNumeral[0]; I < 4; romanNumeral[]++;)
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is the closest i can come up with, not any idea how to create a loop to add an I for every value added, or an IV, or whichever letter is neeeded where
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    or something like this, once we get to 10, the loop adds an x value, then repeats, until we get to 20, then add another x value and repeat, until 30, etc... Anyone any ideas?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7927
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're going too fast again. You solved the challenge for the first nine digits by hardcoding them. I want you to convert the numbers one to nine to Roman numerals, and the only String literals you're allowed to use are "I" and "V". You have to generate the other numerals by using rules.
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have no idea how to do that
     
    Paul Clapham
    Sheriff
    Posts: 22690
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    kennith stomps wrote:I have no idea how to do that


    Start by telling us what the rules are to convert a one-digit Arabic number into Roman numerals. (If you can't state the rules then it's quite natural that you can't write a program to implement those rules.)
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not sure of the rules, would you explain. I am not sure how to change the Arabic number into Roman numerals.
     
    Paul Clapham
    Sheriff
    Posts: 22690
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have a look at the Wikipedia article then: Roman numerals.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7927
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The rules are the following:

  • 0 is the empty string.
  • 1 is the string "I".
  • 5 is the string "V".
  • 10 is the string "X".
  • Addition works by concatenating lesser or equal numerals to greater or equal numerals.
  • Subtraction works by concatenating greater numerals to lesser numerals.
  • You get 2 and 3 by adding 1 to 0 that number of times.
  • You get 4 by subtracting 1 from 5.
  • You get 6-8 by respectively adding 1-3 to 5.
  • You get 9 by subtracting 1 from 10.
  •  
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh yes, I knew those rules, I was thinking you were talking of something different. I just did not know the rules of how that is to be put into code.
     
    Paul Clapham
    Sheriff
    Posts: 22690
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    kennith stomps wrote:I just did not know the rules of how that is to be put into code.


    I don't understand this. Your task here is to take those rules and write code which implements them. There aren't any rules about how you should do that -- except of course that you should produce compilable code which produces correct output for each input you give it.
     
    kennith stomps
    Ranch Hand
    Posts: 148
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How would you write that code, I have no idea
     
    Liutauras Vilda
    Marshal
    Posts: 4789
    330
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    kennith stomps wrote:How would you write that code, I have no idea

    Buddy, you should not be expecting solution to come naturally and in an easy way, unless you maybe well experienced. You got lots of information, you got even algorithm explained, what you need now, is to sit down, concentrate and be prepared to spend good 10 hours of coding, testing, documenting and polishing your solution.

    Asking to show how others would write the code so you could copy, would give you just an illusion you learned something.

    I most likely too wouldn't solve this instantly, even by looking probably to suggestions, but as my Dad says: "read 10, 20 if needed even 100 times until you understand". So this is what I'd do. By reading posts once, I got an impression that there is all information you need - now is your call.
     
    Campbell Ritchie
    Marshal
    Posts: 56197
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Get lots of little bits of paper, and write down how you would explain to a three‑year old how to convert 1,2,3,4,5,6,7,8,9 into I,II,III,IIII/IV,V,VI,VII,VIII,VIIII/IX. You will obviously not use both IV and IIII; you will probably use IV.  Work out how to get from 3 to III.

    If you get it wrong, you can destroy the evidence by throwing the bit of paper away
    Don't write any code until you have got the rules worked out; start by confining yourself to number < 10 and add bigger numbers later.
     
    Paweł Baczyński
    Bartender
    Posts: 2058
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is something seriously wrong with this code. One sneaky extra character can cause serious bugs.
     
    Campbell Ritchie
    Marshal
    Posts: 56197
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is also something wrong with conjoining the predicates greater than zero and not equal to minus one. You don't need both parts.
     
    Paweł Baczyński
    Bartender
    Posts: 2058
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is also something wrong with having two variables named num and number in the same scope. Which one does what?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!