(42S) List Pythagorean Triples
10-16-2019, 07:33 PM (This post was last modified: 10-17-2019 03:13 PM by ijabbott.)
Post: #1 ijabbott Senior Member Posts: 1,065 Joined: Jul 2015
(42S) List Pythagorean Triples
This program lists Pythagorean triples, (natural numbers $$a, b, c$$, such that $$a^2 + b^2 = c^2$$) as alpha strings on the display, starting with "3,4,5". They are not listed in any monotonic order, but successive hypotenuses are listed in a reverse sawtooth pattern.

The triples ($$a,b,c$$) are generated from pairs of natural numbers $$m$$, $$n$$, such that $$m > n > 0$$, and $$m$$ and $$n$$ are co-prime and not both odd (to filter out non-primitive triples such as 6,8,10), using Euclid's formula:

$a = m^2 - n^2, b = 2mn, c = m^2 + n^2$

The program uses registers R00 and R01 to store $$m$$ and $$n$$, and uses the ALPHA register to show the triple "$$a,b,c$$" (if $$a < b$$), or "$$b,a,c$$" (if $$a > b$$). The program stops after each triple is displayed. Press R/S to continue.

Code:
00 { 90-Byte Prgm }
01 LBL "PYTRP"
02 2
03 STO 00
04 LBL 00
05 RCL 00
06 1
07 -
08 STO 01
09 LBL 01
10 RCL 00
11 RCL 01
12 X≤0?
13 GTO 05
14 LBL 02
15 X=0?
16 GTO 03
17 MOD
18 LASTX
19 X<>Y
20 GTO 02
21 LBL 03
22 R↓
23 1
24 X≠Y?
25 GTO 04
26 CLA
27 RCL 01
28 X^2
29 LASTX
30 RCL 00
31 X^2
32 LASTX
33 X<>Y
34 R↓
35 ×
36 ENTER
37 +
38 R↓
39 +
40 LASTX
41 X<>Y
42 R↓
43 -
44 X>Y?
45 X<>Y
46 AIP
47 ├","
48 R↓
49 AIP
50 ├","
51 R↓
52 AIP
53 AVIEW
54 STOP
55 LBL 04
56 2
57 STO- 01
58 GTO 01
59 LBL 05
60 1
61 STO+ 00
62 GTO 00
63 .END.

— Ian Abbott
 « Next Oldest | Next Newest »

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