HP Forums
(12C) Add or Subtract Fractions - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (12C) Add or Subtract Fractions (/thread-11410.html)



(12C) Add or Subtract Fractions - Gamo - 09-17-2018 05:49 AM

Program to Add or Subtract Fractions
Result is simplify to lowest term and
convert to Mixed Fraction.

In the form of

a/b [+ or -] c/d = e/f

If f < e
Then get the result for Mixed Fraction of g+ h/f

Procedure:

a [ENTER] b [ENTER] c [ENTER] d [R/S]
Answer e [X<>Y] f

If Answer f < e press [R/S] --> answer display of g.000h [X<>Y] f of g+ h/f

If Answer f > e is the "Final Answer" press [R/S] and ignore any decimal display.

-----------------------------------------------------------------------------------------

Example: 8/71 + 7/8

8 [ENTER] 71 [ENTER] 7 [ENTER] 8 [R/S]

Display 561 [X<>Y] 568

Answer is 561/568 // Denominator is larger than numerator is a "Final Answer"
Press [R/S] and ignore any decimal display.

------------------------------------------------------------------

Example: 130/19 + 7/3

130 [ENTER] 19 [ENTER] 7 [ENTER] 3 [R/S]

Display 523 [X<>Y] 57

Answer is 523/57 // Denominator is less than numerator then get Mixed Fraction.

Press [R/S] Display 9.0010 [X<>Y] 57

Final Answer is 9+ 10/57

----------------------------------------------------------
Program: Add or Subtract Fractions
Code:

01 STO 4
02 Rv
03 STO 3
04 Rv
05 STO 2
06 Rv
07 STO 1
08 RCL 2
09 RCL 4
10 X≤Y
------------------------------------
11 X<>Y
12 X<>Y
13 STO 0
14 X<>Y
15 ENTER
16 ENTER
17 ENTER
18 RCL 0
19  ÷
20 FRAC
----------------------------------
21 X=0
22 GTO 26
23 Rv
24  +
25 GTO 16
26  +
27 STO 5
28 RCL 2
29  ÷
30 RCL 1
-------------------------------
31  x
32 RCL 5
33 RCL 4
34  ÷
35 RCL 3
36  x
37  +
38 STO 6
39 RCL 5
40 X≤Y
--------------------------
41 X<>Y
42 X<>Y
43  -
44 LSTx
45 X<>Y
46 X=0
47 GTO 49
48 GTO 40
49 X<>Y
50 STO 7
-------------------------------
51  1
52 X<>Y
53 X≤Y
54 GTO 62
55 RCL 5
56 RCL 7
57  ÷
58 RCL 6
59 RCL 7
60  ÷
-------------------------------
61 GTO 64
62 RCL 5
63 RCL 6
64 R/S
65 STO 0
66 X<>Y
67 X<>Y
68  ÷
69 LSTx
70 X<>Y
----------------------------
71 ENTER
72 FRAC
73 X<>Y
74 INTG
75 Rv
76  x
77 EEX
78  4
79  ÷
80 Rv
--------------------------------
81 Rv
82 Rv
83  +
84 FIX 4
85 RCL 0
86 X<>Y
87 GTO 00

Remark: Program will run fast on modern HP-12C like 12C+ and HP-12C Emulators
Gamo


RE: (12C) Add or Subtract Fractions - Gamo - 09-19-2018 07:55 AM

Continue form post #1

Here is the "Multiply and Divide Fractions" program.

Most of the procedure is the same except

Only when calculate "Fractions Division" the last input must follow with [CHS]

Example: 153/15 ÷ 13/17

153 [ENTER] 15 [ENTER] 13 [ENTER] 17 [CHS] [R/S]

Display 867 [Y<>Y] 65

Answer is 867/65 // Denominator < Numerator find Mixed Fraction

[R/S] 13.0022 [X<>Y] 65

Answer is 13+ 22/65

------------------------------------------------------
Program: Multiply or Divide Fractions
Code:

01 STO 4
02 Rv
03 STO 3
04 Rv
05 STO 2
06 Rv
07 STO 1
08 RCL 4
09  1
10 X<>Y
------------------------------------------
11 X≤Y
12 GTO 22
13 RCL 1
14 RCL 3
15  x
16 STO 5
17 RCL 2
18 RCL 4
19  x
20 STO 6
----------------------------------------
21 GTO 33
22 RCL 4
23 CHS
24 STO 4
25 RCL 1
26 RCL 4
27  x
28 STO 5
29 RCL 2
30 RCL 3
--------------------------------------------------
31  x
32 STO 6
33 RCL 6
34 RCL 5
35 X≤Y
36 X<>Y
37 X<>Y
38  -
39 LSTx
40 X<>Y
-----------------------------------------------
41 X=0
42 GTO 44
43 GTO 35
44 X<>Y
45 STO 7
46  1
47 X<>Y
48 X≤Y
49 GTO 57
50 RCL 6
----------------------------------------------
51 RCL 7
52  ÷
53 RCL 5
54 RCL 7
55  ÷
56 GTO 59
57 RCL 6
58 RCL 5
59 R/S
60 STO 0
---------------------------------------------
61 X<>Y
62 X<>Y
63  ÷
64 LSTx
65 X<>Y
66 ENTER
67 FRAC
68 X<>Y
69 INTG
70 Rv
-------------------------------------------
71  x
72 EEX
73  4
74  ÷
75 Rv
76 Rv
77 Rv
78  +
79 FIX 4
80 RCL 0
81 X<>Y
82 GTO 00

Remark: This Multiply or Divide program will not work with negative fractions.

Try this challenging example: 676/13 x 3117/22

Answer: 81042/11 or 7367+ 5/11

Even on a fast 12C+ took about 10 seconds to compute.



Gamo


RE: (12C) Add or Subtract Fractions - Albert Chan - 09-19-2018 12:19 PM

(09-19-2018 07:55 AM)Gamo Wrote:  Remark: This Multiply or Divide program will not work with negative fractions.

Try this challenging example: 676/13 x 3117/22

Answer: 81042/11 or 7367+ 5/11

Even on a fast 12C+ took about 10 seconds to compute.

Why is above a challenging example ?

gcd(676*3117, 13*22) = gcd(2107092, 286) = gcd(130, 286) = gcd(130, 26) = 26 should not take long ...

-> fraction = (2107092/26) / (286/26) = 81042 / 11

BTW, there is no need to implement fraction division
Just replace CHS with X<>Y , multiply fraction inverse = fraction division


RE: (12C) Add or Subtract Fractions - Dieter - 09-19-2018 06:45 PM

(09-19-2018 12:19 PM)Albert Chan Wrote:  
(09-19-2018 07:55 AM)Gamo Wrote:  Try this challenging example: 676/13 x 3117/22

Answer: 81042/11 or 7367+ 5/11

Even on a fast 12C+ took about 10 seconds to compute.

Why is above a challenging example ?

Because the program uses a quite ineffective way of computing the GCD, cf. line 33 ff.

(09-19-2018 12:19 PM)Albert Chan Wrote:  gcd(676*3117, 13*22) = gcd(2107092, 286) = gcd(130, 286) = gcd(130, 26) = 26 should not take long ...

Yes, this (Euklid's) method is very fast. Which is why I also chose it for my program in the 11C fractions thread.
But the above program does it this way:

2107092 – 286 = 2106806
2106086 – 286 = 2106520
2106520 – 286 = 2106234
...
(more than 7300 subtractions later)
...
702 – 286 = 416
416 – 286 = 130
286 – 130 = 156
156 – 130 = 26
130 – 26 = 104
104 – 26 = 78
78 – 26 = 52
52 – 26 = 26
26 – 26 = 0

=> GCD = 26

That's almost 7400 loops, which takes quite some time even even on a fast calculator. On the other hand the modulo-based Euclidean algorithm only requires 3 loops. For the record: even a rather slow HP67 can do it in about 3 seconds. How much faster is the 12C+? 100 times? 200? 500? If it does 7400 loops in 10 seconds it should return the result instantly when the modulo method is used.

(09-19-2018 12:19 PM)Albert Chan Wrote:  BTW, there is no need to implement fraction division
Just replace CHS with X<>Y , multiply fraction inverse = fraction division

Yes, that's exactly what I suggested in the thread with the 11C program. 1/2 : 3/4 = 1/2 · 4/3.
Maybe Gamo hasn't noticed it.

Dieter


RE: (12C) Add or Subtract Fractions - Dieter - 09-24-2018 05:24 PM

(09-19-2018 07:55 AM)Gamo Wrote:  Continue form post #1

Here is the "Multiply and Divide Fractions" program.

You can have both programs at the same time, even on a 12C.

(09-19-2018 07:55 AM)Gamo Wrote:  Remark: This Multiply or Divide program will not work with negative fractions.

This one will:

Code:
01 STO 4
02 R↓
03 STO 3
04 R↓
05 STO 2
06 R↓
07 STO 1
08 CLX
09 R/S
10 1
11 -
12 X=0?
13 GTO 27
14 1
15 -
16 X=0?
17 GTO 29
18 1
19 -
20 X=0?
21 GTO 41
22 1
23 -
24 X=0?
25 GTO 44
26 GTO 08
27 RCL 3   add
28 GTO 31
29 RCL 3   subtract
30 CHS
31 RCL 2
32 x
33 RCL 1
34 RCL 4
35 x
36 +
37 RCL 2
38 RCL 4
39 x
40 GTO 51
41 RCL 4   multiply
42 RCL 3
43 GTO 46
44 RCL 3   divide
45 RCL 4
46 RCL 1
47 x
48 X<>Y
49 RCL 2
50 x
51 STO 2   store result
52 X<>Y    in R2 (denominator)
53 STO 1   and R1 (numerator)
54 ENTER   GCD loop starts here
55 ENTER
56 CLX
57 +
58 R↓
59 ÷
60 LstX
61 X<>Y
62 INTG
63 x
64 -
65 X=0?
66 GTO 68
67 GTO 54
68 +
69 STO÷1   reduce
70 STO÷2   fraction
71 RCL 1
72 PSE     display numerator
73 RCL 1
74 RCL 2
75 R/S     display denominator
76 X=0?
77 GTO 79  if input was zero, convert to mixed fraction
78 GTO 01  else continue calculation with new data
79 RCL 1
80 RCL 2
81 ÷
82 INTG
83 PSE
84 RCL 2
85 x
86 RCL 1
87 X<>Y
88 -
89 PSE
90 GTO 73

Enter program, switch back to run mode, f [PRGM] to reset.

Usage:
1. Enter both fractions:
a [ENTER] b [ENTER] c [ENTER] d
[R/S] => 0

2. Enter command code:
1 = add
2 = subtract
3 = multiply
4 = divide
[R/S]

3. Result is displayed as "numerator", denominator.

4. Now you can do one of the following:

4a. Convert result to mixed fraction:
Enter 0 [R/S]
=> "whole part", "numerator", denominator
Continue with step 4.

4b. Enter another fraction e/f to be added/subtracted/multiplied/divided:
e [ENTER] f [R/S]
Continue with step 2.

4c. Start a completely new calculation:
Continue with step 1.

(09-19-2018 07:55 AM)Gamo Wrote:  Try this challenging example: 676/13 x 3117/22

Answer: 81042/11 or 7367+ 5/11

676 [ENTER] 13 [ENTER] 3117 [ENTER] 22
[R/S] => 0
3 [R/S]
=> "81042" 11

Convert this into a mixed fraction:
0 [R/S]
=> "7367" "5" "11"

Now add 1/22
1 [ENTER] 22 [R/S]
1 [R/S]
=> "14735" 2

Finally subtract 1/4
1 [ENTER] 4 [R/S]
2 [R/S]
=> "29469" 4

View this as a mixed fraction:
0 [R/S]
=> "7367" "1" 4

New calculation: 1/3 – 2/5
1 [ENTER] 3 [ENTER] 2 [ENTER] 5 [R/S]
2 [R/S]
"1" –15

Add 1/25
1 [ENTER] 25 [R/S]
1 [R/S]
"–2" 75

Re. memory uisage:
g MEM yields P–92  r–08
No registers beyond R4 are used.

Dieter

Edit: replaced program with a slightly modified version, now down to 90 steps.


RE: (12C) Add or Subtract Fractions - Gamo - 09-25-2018 02:36 AM

Dieter

That's a very good Arithmetic with Fractions for
HP-12C while I'm still shuffling on how to do this and you already done it.

Your program is very good with easy to use program procedures.

Thank You

Gamo


RE: (12C) Add or Subtract Fractions - Dieter - 09-27-2018 04:52 PM

(09-24-2018 05:24 PM)Dieter Wrote:  New calculation: 1/3 – 2/5
1 [ENTER] 3 [ENTER] 2 [ENTER] 5 [R/S]
2 [R/S]
"1" –15

Update: there are still a few steps left, so here is a slightly modified version that adds a nicer display of negative results: now the numerator has the sign of the result and the denominator is always positive. So the above example will now be displayed as "–1" 15. The same holds for mixed fractions, so –3 1/4 is returned as "–3" "–1" 4.

Here is the code:

Code:
01 STO 4
02 R↓
03 STO 3
04 R↓
05 STO 2
06 R↓
07 STO 1
08 CLX
09 R/S
10 1
11 -
12 X=0?
13 GTO 27
14 1
15 -
16 X=0?
17 GTO 29
18 1
19 -
20 X=0?
21 GTO 41
22 1
23 -
24 X=0?
25 GTO 44
26 GTO 08
27 RCL 3   add
28 GTO 31
29 RCL 3   subtract
30 CHS
31 RCL 2
32 x
33 RCL 1
34 RCL 4
35 x
36 +
37 RCL 2
38 RCL 4
39 x
40 GTO 51
41 RCL 4   multiply
42 RCL 3
43 GTO 46
44 RCL 3   divide
45 RCL 4
46 RCL 1
47 x
48 X<>Y
49 RCL 2
50 x
51 STO 2   store result
52 X<>Y    in R2 (denominator)
53 STO 1   and R1 (numerator)
54 ENTER   GCD loop starts here
55 ENTER
56 CLX
57 +
58 R↓
59 ÷
60 LstX
61 X<>Y
62 INTG
63 x
64 -
65 X=0?
66 GTO 68
67 GTO 54
68 +
69 STO÷1   reduce
70 STO÷2   fraction
71 RCL 2
72 RCL 2
73 ENTER
74 x
75 √x
76 STO 2   store abs(denominator)
77 ÷       sign(denominator)
78 STOx1   adjust sign of numerator
79 RCL 1
80 PSE     display numerator
81 RCL 1
82 RCL 2
83 R/S     display denominator
84 X=0?
85 GTO 87  if input was zero, convert to mixed fraction
86 GTO 01  else continue calculation with new data
87 RCL 1
88 RCL 2
89 ÷
90 INTG
91 PSE
92 RCL 2
93 x
94 RCL 1
95 X<>Y
96 -
97 PSE
98 GTO 81

I think it won't get much better now.
There's still room for one more step. You may use this to add a FIX 0 (f 0) somewhere. ;-)

Dieter