There's one drawback with HP28S and that's it's handle of fractional arithmetic,
witch it don't have.
With my program it will take about half(?) a second to get the minimized answer
with a denominator that has 4-6 digits. If more digits are needed (7-12) it will
take about 1.5-2.5 seconds depending on how many loops the program have to do.
I have not implemented routines for testing input error since it will increase
the operation time, thus the 'add' 'sub' div' 'mul' and 'num' programs are very
simple.
You can add, subtract, divide, multiply or just find the minimum of a fraction.
The answers are given in {a b c} or {a b} form. You choose.
First a few words about the program.
The main program is just a simple loop that find the maximum common divisor for
the counter and denominator. So, if you have a fraction 45/12 then the program
will compute 15/4 as a result.
Here's how:
Assume a fraction, 67/52. With MOD(67,52) the answer is 15, however, this
is not helpful unless the answer can be used in some sort of way.
Now, you take MOD(67,15) and MOD(52,15) the corresponding answers are 7 7.
Furthermore MOD(67,7) and MOD(52,7) gives 4 3, which indeed are not equal.
Because one of them, in later calculations, could be a zero we have to choose
the greatest of them. Continuing MOD(67,4) and MOD(52,4) we get 3 0 and again
we choose the greatest. Then, MOD(67,3) and MOD(57,3) gives 1 1 and in the
final run MOD(67,1) and MOD(52,1) gives 0 0.
Actually, all fractions can be divided with MOD in this way to give a 0 0
result, and used in a program it is an easy and fast way to find the minimized
fraction.
The interesting thing by doing it this way is that the greatest common divisor
for the to integers are the last maximum number found before we get 0 0. In this
example we have the number 1 which gives us 67/1 and 52/1 and the fraction can
not be reduced more than it was.
If you try with 135/81 for example you will get that the greatest number before
0 0 is 27 and that gives us 5/3.
THE PROGRAMS.
The input can be given in 2 ways with this simple programs.
With improper fractions (or combined with a proper fraction) you have to type it
in like this: {a b c} and/or {a b}.
If you use only proper fractions you can skip the {} if you want.
-------------------------------------------------------------------------------
Routines for calculating fractions:
'add' - add 2 fractions -
<< LET -> a b c d
<< a d * b c * + b
d * LEAST
>>
>>
'sub' - subtract 2 fractions -
<< LET -> a b c d
<< a d * b c * - b
d * LEAST
>>
>>
'div' - divide 2 fractions -
<< LET -> a b c d
<< a d * b c *
LEAST
>>
>>
'mul' - multiply to fractions -
<< LET -> a b c d
<< a c * b d *
LEAST
>>
>>
'->NUM' - gives the numerical value -
<< ->2 /
>>
LET - prepare input for 'add' 'sub'... -
<< -> a b
<< a ->2 b ->2
>>
>>
LET2 -subroutine for LET -
<< -> a
<<
IF a TYPE 5 <> <> means: shift =
THEN a 1
ELSE a
IF a SIZE 3 ==
THEN ->2
ELSE
END LIST-> DROP
END
>>
>>
'->2' - subroutine: from {a b c} to {a b} -
<<
IFERR LIST->
IF 3 ==
THEN -> a b c
<< a c * b + c
>>
END
THEN
END
>>
'->3' - subroutine for LEAST(can be skipped): from a b or {a b} to {a b c} -
<< -> a b
<< a b / IP a OVER
b * - b 3 PICK - 3
->LIST
>>
>>
-------------------------------------------------------------------------------
This is the program that minimize a fraction, the input must be on the form
a b on the stack.
LEAST - finds the minimum fraction -
<< -> a b
<< b a b MOD DUP
WHILE 0 <> <> means < shift = >
REPEAT SWAP DROP
a OVER MOD b 3 PICK
MOD MAX DUP
END DROP a OVER
/ b ROT / ->3 skipping '->3' gives a b answers
>> else {a b c}
>>
Because of the simplicity mentioned before you will get some "fun" answers now
and then.This is not because of the LEAST program but the simple procedures it
uses,(here '->3'). If you try to minimize 8 1 you'll get {8 0 -7}, the same
may occur if you use the subroutine '->3'. 4 5 ->3 gives {0 4 5}. This can be
fixed by two if-then's but will of course increase the operation time.On my own
HP28S I have a few routines for error checking giving better answers for further
calculation.
As far as I can see my program is ok, however, I will not take any
responsibility for any calculations that leads to wrong answers.
If you're not sure the programs are ok, check the answers with a calculator
that have the fraction option build in.
Sturle Hausvik, University of Bergen, norway.
My mailaddress: ip193@brems.ii.uio.no
Enjoy.