(41C) Pythagorean Triples
06-25-2022, 03:40 PM (This post was last modified: 06-25-2022 03:52 PM by Thomas Klemm.)
Post: #2
 Thomas Klemm Senior Member Posts: 1,814 Joined: Dec 2013
RE: (41C) Pythagorean Triples
On page 26 we find:
Quote:Our calculators used Reverse Polish Notation, which is a great convenience and posed no difficulty to the children.

No one in this forum is surprised by that.

I picked the problem to print primitive Pythagorean triples.

Here's a program for the HP-42S:
Code:
00 { 99-Byte Prgm } # 01▸LBL "PPT"        # p 02 1ᴇ3              # 1000 p 03 ÷                # 0.ppp 04 2                # 2 0.ppp 05 +                # i=2.ppp 06 STO 00           # i -> R00 07▸LBL 00           # for i in 2.ppp 08 RCL 00           # i 09 IP               # m 10 STO 02           # m -> R02 11 2                # 2 m 12 MOD              # m%2 13 1                # 1 m%2 14 +                # b=m%2+1 15 RCL 02           # m b 16 1                # 1 m b 17 -                # e=m-1 b 18 .02              # 0.02 e b 19 +                # e.02 b 20 1ᴇ3              # 1000 e.02 b 21 ÷                # 0.eee02 22 +                # j=b.eee02 23 STO 01           # j -> R01 24▸LBL 01           # for j in b.eee02 25 RCL 02           # m 26 RCL 01           # j m 27 IP               # n m 28 STO 03           # n -> R03 29▸LBL 02           # ( m n -- gdc(m, n) ) 30 X<>Y             # m n 31 RCL ST Y         # n m n 32 MOD              # m%n n 33 X≠0?             # 34 GTO 02           # n' m' 35 R↓               # gcd(m, n) 36 1                # 1 gcd(m, n) 37 X≠Y?             # gcd(m, n) ≠ 1 ? 38 GTO 03           # skip triple 39 RCL 03           # n 40 X↑2              # n^2 41 RCL 02           # m n^2 42 RCL× 03          # m*n n^2 43 STO+ ST X        # 2*m*n n^2 44 RCL 02           # m 2*m*n n^2 45 X↑2              # m^2 2*m*n n^2 46 ENTER            # m^2 m^2 2*m*n n^2 47 R↑               # n^2 m^2 m^2 2*m*n 48 STO- ST Z        # n^2 m^2 m^2-n^2 2*m*n 49 +                # m^2+n^2 m^2-n^2 2*m*n 50 CLA              # "" 51 ARCL ST Y        # "Y" 52 ├" "             # "Y " 53 ARCL ST Z        # "Y Z" 54 ├" "             # "Y Z " 55 ARCL ST X        # "Y Z X" 56 AVIEW            #                                  57▸LBL 03           # resume 58 ISG 01           # j -> j+1 59 GTO 01           # 60 ISG 00           # i -> i+1 61 GTO 00           # 62 END              #
But it should also work with the HP-41C after the obvious transformations.

Example

12 XEQ "PPT"

3 4 5
5 12 13
15 8 17
7 24 25
21 20 29
9 40 41
35 12 37
11 60 61
45 28 53
33 56 65
13 84 85
63 16 65
55 48 73
39 80 89
15 112 113
77 36 85
65 72 97
17 144 145
99 20 101
91 60 109
51 140 149
19 180 181
117 44 125
105 88 137
85 132 157
57 176 185
21 220 221
143 24 145
119 120 169
95 168 193
23 264 265

Make sure to SF 21 and hit R/S to get to the next triple.

The program is based on the following Python program:
Code:
def primitive(m):     for n in range(2 if m % 2 else 1, m, 2):         if gcd(m, n) == 1:             triple(m, n) def gcd(a, b):     while b > 0:         a, b = b, a % b     return a def triple(m, n):     a = m*m - n*n     b = 2*m*n     c = m*m + n*n     print(f"{a} {b} {c}") for m in range(2, 13):     primitive(m)

References
 « Next Oldest | Next Newest »

 Messages In This Thread (41C) Pythagorean Triples - SlideRule - 10-15-2019, 12:57 PM RE: (41C) Pythagorean Triples - Thomas Klemm - 06-25-2022 03:40 PM RE: (41C) Pythagorean Triples - C.Ret - 06-26-2022, 09:29 AM RE: (41C) Pythagorean Triples - Thomas Klemm - 06-27-2022, 05:51 AM RE: (41C) Pythagorean Triples - Ángel Martin - 06-29-2022, 01:02 PM RE: (41C) Pythagorean Triples - John Keith - 07-01-2022, 11:55 AM RE: (41C) Pythagorean Triples - C.Ret - 07-03-2022, 08:18 AM RE: (41C) Pythagorean Triples - John Keith - 07-03-2022, 01:55 PM RE: (41C) Pythagorean Triples - Thomas Klemm - 07-03-2022, 10:46 AM RE: (41C) Pythagorean Triples - C.Ret - 07-03-2022, 01:41 PM

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