HP Forums
Roman Numeral Function - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Roman Numeral Function (/thread-5536.html)



Roman Numeral Function - smp - 01-17-2016 09:29 PM

Here's a simple function I created. Get a Roman Numeral as output for an integer input. Invoke by RN(integer).

Code:

EXPORT RN(N)
BEGIN
  LOCAL B:="";
  WHILE N>=1000 DO
    B:=B+"M";
    N:=N-1000;
  END;
  IF N>=900 THEN
    B:=B+"CM";
    N:=N-900;
  END;
  IF N>=500 THEN
    B:=B+"D";
    N:=N-500;
  END;
  IF N>=400 THEN
    B:=B+"CD";
    N:=N-400;
  END;
  WHILE N>=100 DO
    B:=B+"C";
    N:=N-100;
  END;
  IF N>=90 THEN
    B:=B+"XC";
    N:=N-90;
  END;
  IF N>=50 THEN
    B:=B+"L";
    N:=N-50;
  END;
  IF N>=40 THEN
    B:=B+"XL";
    N:=N-40;
  END;
  WHILE N>=10 DO
    B:=B+"X";
    N:=N-10;
  END;
  IF N=9 THEN
    B:=B+"IX";
    N:=N-9;
  END;
  IF N>=5 THEN
    B:=B+"V";
    N:=N-5;
  END;
  IF N=4 THEN
    B:=B+"IV";
    N:=N-4;
  END;
  WHILE N>=1 DO
    B:=B+"I";
    N:=N-1;
  END;
  RETURN B;
END;

This is pretty simple stuff, but I'm still learning PPL.

UPDATE: I messed up and posted code that did not handle 40 properly. I've updated with the correct code.

smp


RE: Roman Numeral Function - John Colvin - 01-19-2016 09:46 PM

Now all we need is the inverse function RN^(-1)(XLV) to convert from Roman numerals to integers. I think that would be more useful.


RE: Roman Numeral Function - smp - 01-19-2016 10:12 PM

(01-19-2016 09:46 PM)John Colvin Wrote:  Now all we need is the inverse function RN^(-1)(XLV) to convert from Roman numerals to integers. I think that would be more useful.

Cool idea! I'll give that some thought.

Thanks!
smp


RE: Roman Numeral Function - Gerald H - 01-20-2016 05:56 AM

Here is an economical Roman to Arabic converter:

http://www.hpmuseum.org/forum/thread-1640.html?highlight=roman

Perhaps someone could translate the programme into Prime?


RE: Roman Numeral Function - Didier Lachieze - 01-20-2016 01:08 PM

(01-20-2016 05:56 AM)Gerald H Wrote:  Here is an economical Roman to Arabic converter:

http://www.hpmuseum.org/forum/thread-1640.html?highlight=roman

Perhaps someone could translate the programme into Prime?

With some checks on the input:
Code:
EXPORT R→A(r)
BEGIN
  LOCAL l1:=ASC("IVXLCDM");
  LOCAL l2:={1,5,10,50,100,500,1000};
  LOCAL l3, ix, rn, dn, cur, prev, sz;
  
  //if not a string, stop
  IF TYPE(r)<>2 THEN RETURN r; END; 

  rn:=UPPER(r); sz:=SIZE(rn);
  l3:= MAKELIST(POS(l1,rn(I)),I,1,sz);

  //if string includes non-roman characters, stop
  IF NOT ΠLIST(l3) THEN RETURN r; END; 
  
  //else returns decimal number
  FOR ix FROM 1 to sz DO
    cur:= l2(l3(ix));
    dn:=dn + IFTE(prev<cur, -prev, prev);
    prev:=cur;
  END;
  RETURN dn+cur;
END;



RE: Roman Numeral Function - Gerald H - 01-20-2016 04:09 PM

Bravo, Didier! All numbers tested so far correct.

I didn't really expect anyone to do this...at least not so quickly!


RE: Roman Numeral Function - Didier Lachieze - 01-20-2016 05:11 PM

And here is a program to convert from Arabic to Roman representation:

Code:
EXPORT A→R(N)
BEGIN
  LOCAL r:={"M","D","C","L","X","V","I"};
  LOCAL d:={1000,500,100,50,10,5,1,0,0};
  LOCAL j,k,s:="";

  FOR j FROM 1 TO 7 DO
    WHILE N≥d(j) DO s:=s+r(j);N:=N-d(j);END;
    k:=j+j MOD 2+1;
    IF N≥d(j)-d(k) THEN s:=s+r(k)+r(j);N:=N-d(j)+d(k);END;
  END;
  RETURN s;
END;



RE: Roman Numeral Function - Gerald H - 01-21-2016 10:45 AM

A very nice programme, Didier, which I have translated to the 49G here:

http://www.hpmuseum.org/forum/thread-5567.html

Having again looked at the Prime I despair of using it - the pale blue & orange smudges on the keyboard are just too unclear.

On the 50G, 49G, 48G & 42S using reading glasses I can manage, so until HP improve visibility I'll leave Prime programming to you & your ilk.


RE: Roman Numeral Function - DrD - 01-21-2016 11:49 AM

I agree, those two are very nice complement of utility programs, Didier. You seem especially well talented with the Prime, so thanks for those, and all the help you have provided along the way!

-Dale-


RE: Roman Numeral Function - JMB - 01-21-2016 01:42 PM

Hi!

Here there is a slightly modified version of Didiers's A→R, that also give good results for N between 1 and 3999

Code:
EXPORT A→R(N)
BEGIN
  LOCAL r:={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  LOCAL d:={1000,900,500,400,100,90,50,40,10,9,5,4,1};
  LOCAL j,s:="";

  FOR j FROM 1 TO 13 DO
    WHILE N≥d(j) DO s:=s+r(j); N:=N-d(j); END;
  END;
  RETURN s;
END;