[comp.sys.handhelds] HP48: GCF and LCD programs

eagle@catt.ncsu.edu (Daniel L'Hommedieu) (03/01/91)

I'm having trouble translating a program to RPN.  It's a very useful
program that I used in my Casio fx-4000P.  It calculates the greatest
common factor between two numbers.  Another program I had but cannot
currently find calculated the prime factors of a number (for example,
250=2*5*5*5).

For those of you who are ambitious and would like to help me translate
the GCF program into RPN, here it is in some pseudocode form:

input a;a=abs(a);
input b;b=abs(b);
if (a<b) then
  swap(a,b);
repeat
{
  c=-(int(a/b)*b-a);
  a=b;
  b=c;
} until c=0;
print b;

My program gets into an infinite loop, continuing regardless of the
value of c.  Any help with this program is greatly appreciated!

Daniel
--
Name: Daniel C. L'Hommedieu III   Snail: NCSU Box 21531/Raleigh/NC/27607
INet: eagle@catt.ncsu.edu         Prodigy ID: bccj33d   Tel:919 737 6143
-OBJ: "Welcome to the Literacy Self-test Hotline. Please leave your name, 
  number, and a brief message using today's word: accolade." 
Name: Daniel C. L'Hommedieu III   Snail: NCSU Box 21531/Raleigh/NC/27607
INet: eagle@catt.ncsu.edu         Prodigy ID: bccj33d   Tel:919 737 6143
-OBJ: "Welcome to the Literacy Self-test Hotline. Please leave your name, 
  number, and a brief message using today's word: accolade." 

edp@jareth.enet.dec.com (Eric Postpischil (Always mount a scratch monkey.)) (03/01/91)

In article <1991Mar1.044209.22242@ncsu.edu>, eagle@catt.ncsu.edu (Daniel
L'Hommedieu) writes:

[a program to find the greatest common factor of two numbers]

You didn't say what RPN language you are trying to translate it into.  Here it
is for the HP-48:

\<< ABS SWAP ABS WHILE DUP REPEAT OVER MOD SWAP END DROP \>>


				-- edp (Eric Postpischil)
				"Always mount a scratch monkey."
				edp@jareth.enet.dec.com

NORM%IONAACAD.BITNET@CUNYVM.CUNY.EDU (Norman Walsh) (03/02/91)

>I'm having trouble translating a program to RPN.  It's a very useful
>program that I used in my Casio fx-4000P.  It calculates the greatest
>common factor between two numbers.
> (text deleted)
>the GCF program into RPN, here it is in some pseudocode form:
>
>input a;a=abs(a);
>input b;b=abs(b);
>if (a<b) then
>  swap(a,b);
>repeat
>{
>  c=-(int(a/b)*b-a);
>  a=b;
>  b=c;
>} until c=0;
>print b;

Daniel,
Here is my translation of your psuedocode.  It is not particularly
efficient because I have endeavored to duplicate the psuedocode above
as closely as possible.  'a' and 'b' are taken from the stack, 'a' is
returned (not 'b' as the code above would have us believe):

<< 0 -> a b c
   <<
       IF a b <
       THEN a b 'a'
STO 'b' STO
       END
       DO a b / IP b *
a - NEG DUP 'c' STO
b 'a' STO 'b' STO
       UNTIL c 0 ==
       END a
   >>
>>

I have taken the slight liberty of optimizing the storage of b into
a and c into b by duping the result of calculating the new 'c' on the
stack and storing that into b rather than recalling it from 'c' and
storing it in 'b'.

If there is any part of the above code you would like explained,
just let me know.  'though I'll be away next week so be patient...

                                                    ndw

ares@alessia.dei.unipd.it (Nicola Catacchio 259126) (03/05/91)

In article <1991Mar1.044209.22242@ncsu.edu>, eagle@catt.ncsu.edu (Daniel L'Hommedieu) writes:

> For those of you who are ambitious and would like to help me translate
> the GCF program into RPN, here it is in some pseudocode form:

	Here you are:
<< WHILE DUP
   REPEAT SWAP OVER MOD 
   END DROP
>>
	Computes gcd given the two numbers in the stack, in level 1: and 2:.
	If someone can read me, please write to me or followup on this group,
I'm not sure that this news can be read out of Italy.
	My address is:
	ares@alessia.dei.unipd.it

	Nicola Catacchio (Padua University)

henrikj@kuling.UUCP (Henrik Johansson) (03/07/91)

Soeone out there asked for a program that computes
the greatest common divisor of two numbers.  He had an
example written in some strange language, and wanted the HP48-
version of it.  Here comes one:

<< WHILE DUP REPEAT
SWAP OVER MOD END
 DROP >>

It takes two numbers from the stack and leaves one
number on the stack as the result.

henrikj@kuling.docs.uu.se