Post Reply 
Roman Numeral Function
01-17-2016, 09:29 PM (This post was last modified: 01-17-2016 10:51 PM by smp.)
Post: #1
Roman Numeral Function
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
Find all posts by this user
Quote this message in a reply
01-19-2016, 09:46 PM
Post: #2
RE: Roman Numeral Function
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.
Find all posts by this user
Quote this message in a reply
01-19-2016, 10:12 PM
Post: #3
RE: Roman Numeral Function
(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
Find all posts by this user
Quote this message in a reply
01-20-2016, 05:56 AM
Post: #4
RE: Roman Numeral Function
Here is an economical Roman to Arabic converter:

http://www.hpmuseum.org/forum/thread-164...ight=roman

Perhaps someone could translate the programme into Prime?
Find all posts by this user
Quote this message in a reply
01-20-2016, 01:08 PM
Post: #5
RE: Roman Numeral Function
(01-20-2016 05:56 AM)Gerald H Wrote:  Here is an economical Roman to Arabic converter:

http://www.hpmuseum.org/forum/thread-164...ight=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;
Find all posts by this user
Quote this message in a reply
01-20-2016, 04:09 PM
Post: #6
RE: Roman Numeral Function
Bravo, Didier! All numbers tested so far correct.

I didn't really expect anyone to do this...at least not so quickly!
Find all posts by this user
Quote this message in a reply
01-20-2016, 05:11 PM
Post: #7
RE: Roman Numeral Function
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;
Find all posts by this user
Quote this message in a reply
01-21-2016, 10:45 AM
Post: #8
RE: Roman Numeral Function
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.
Find all posts by this user
Quote this message in a reply
01-21-2016, 11:49 AM
Post: #9
RE: Roman Numeral Function
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-
Find all posts by this user
Quote this message in a reply
01-21-2016, 01:42 PM
Post: #10
RE: Roman Numeral Function
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;
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)