Showing posts with label Pythagorean Triple. Show all posts
Showing posts with label Pythagorean Triple. Show all posts

Saturday, July 25, 2020

Fun with the 71B '20

Fun with the 71B '20

Differential Equations:  Runge Kutta Method 4th Order

Find a numerical solution to the differential equation:

dy/dx = f(x, y)

x is the independent variable, y is the dependent variable.  You define f(x,y) on line 10.   For example, dy/dx = sin(x * y) should have this as line 10:

10 DEF FNF(X,Y) = SIN(X*Y)

Line 5 is a remark line.  Remarks are followed by exclamation points on the HP 71B.

HP 71B Program: RK4 

Size: About 300 - 330 bytes

5 ! FNF(X,Y) = dY/dX
10 DEF FNF(X,Y) = [ insert f(x,y) here ]
15 DESTROY X,Y,H,K1,K2,K3,K4
20 INPUT "X0? "; X
25 INPUT "Y0? "; Y
30 INPUT "STEP? "; H
40 K1 = FNF(X,Y)
45 K2 = FNF(X+H/2,Y+H*K1/2)
50 K3 = FNF(X+H/2,Y+H*K2/2)
55 K4 = FNF(X+H,Y+H*K3)
60 X=X+H
65 Y=Y+H*(K1+2*K2+2*K3+K4)/6
80 DISP "(";X;",";Y;")" @ PAUSE
85 DISP "NEXT? Y/N"
90 A$=KEY$
95 IF A$="Y" THEN 40
99 IF A$="N" THEN DISP "DONE" @ END ELSE 85

Example:
dy/dx = sin(x * y) with inital condition y(0) = 0.5,  h = 0.2 * π

First three results:
( .628318530718 , .607747199386 )   ( [ f ] [ +] (CONT), [ Y ] )
( 1.25663706144, 1.02432288082 )
( 1.88495559216, 1.51038862362 ) 

Hyperbolic Functions

[ S ] = sinh(x)
[ C ] = cosh(x)
[ A ] = asinh(x)
[ H ] = acosh(x)
[ X ]  to exit

HP 71B Program: HYP

Size:  401 bytes
acosh(x) requires that | x | ≥ 1

100 DESTROY A,X
115 DISP "sinh S/A, cosh C/H, X"
120 A$=KEY$
125 IF A$="S" THEN INPUT "X? ";X @ CALL SINH(X)
130 IF A$="C" THEN INPUT "X? ";X @ CALL COSH(X)
135 IF A$="A" THEN INPUT "X? ";X @ CALL ASINH(X)
140 IF A$="H" THEN INPUT "X? ";X @ CALL ACOSH(X)
145 IF A$="X" THEN 150 ELSE 115
150 DISP "DONE" @ END
200 SUB SINH(X)
205 DISP (EXP(X)-EXP(-X))/2 @ PAUSE
210 END SUB
300 SUB COSH(X)
305 DISP (EXP(X)+EXP(-X))/2 @ PAUSE
310 END SUB
400 SUB ASINH(X)
405 DISP LOG(X+SQR(X^2+1)) @ PAUSE
410 END SUB
500 SUB ACOSH(X)
510 DISP LOG(X+SQR(X^2-1)) @ PAUSE
515 END SUB

Example:
X = 2.86

sinh(2.86) returns 8.70212908815
cosh (2.86) returns 8.75939784845
asinh(2.86) returns 1.77321957441
acosh(2.86) returns  1.71190019325

Arithmetic-Geometric Mean

The arithmetic-geometric mean (AGM) is found by the iterative process:

a = 0.5 * (x + y)
g = √(x * y)

The values of a and g are stored into x and y, respectively.  The process repeats until the values of a and g converge.   A tolerance of 10^(-9) is used to display an 8-digit approximation.

HP 71B Program: AGM

Size:  148 Bytes

10 DESTROY X,Y,A,B
15 DISP "AGM(X,Y)" @ WAIT .5
20 INPUT "X? ";X
25 INPUT "Y? ";Y
30 A=.5*(X+Y)
35 G=SQR(X*Y)
40 X=A
45 Y=G
50 IF ABS(X-Y)>1E-9 THEN 30
55 DISP USING 60;X
60 IMAGE 10D.8D    // (10 digit integer parts with rounding to 8 decimal places)
65 END

Example:
AGM(178, 136)

Result:  156.29380544

Pythagorean Triple Generator

Given two positive integers m, n; where m > n, a Pythagorean triple is generated with the following calculations:

a = 2*m*n
b = m^2 - n^2
c = m^2 + n^2

Properties:

a^2 + b^2 = c^2
Perimeter: p = a + b + c
Area: r = a * b / 2

HP 71B Program: PYTHTRI

Size: 217 bytes

10 DESTROY M,N,A,B,C,R,P
20 DISP "M>N, INTEGERS" @ WAIT .5
25 INPUT "M? "; M
30 INPUT "N? "; N
35 A=2*M*N
40 B=M^2-N^2
45 C=M^2+N^2
50 P=A+B+C
55 R=A*B/2
60 DISP 'A = ';A @ PAUSE
65 DISP 'B = ';B @ PAUSE
70 DISP 'C = ';C @ PAUSE
75 DISP 'PERIM.=';P @ PAUSE
80 DISP 'AREA =';R
85 END

Example:
M = 16, N = 11

Results:
A = 352, B = 153, C = 377, P = 864, R = 23760

Impedance of An Alternating Current

The program ALTCURR calculates the impedance (magnitude and phase angle) of a sinusoidal alternating current consisting of one resistor, one capacitor, and one inductor in a series.

HP 71B Program: ALTCURR

Size: 210 bytes

10 DESTROY F,L,C, R,W,Z,T
15 DEGREES
20 INPUT "FREQUENCY? ";F
25 INPUT "INDUCTANCE? ";L
30 INPUT "CAPACITANCE? ";C
35 INPUT "RESISTANCE? ";R
40 W=2*PI*F
45 Z=SQR(R^2+(W*L-1/(W*C))^2)
50 T=ATAN((W*L-1/(W*C))/R)
55 DISP "MAGNITUDE= "; Z @ PAUSE
60 DISP "PHASE ANGLE = "; T

Example:
F = 152 Hz
L = 4.75E-3 H  (4.75 mH)
C = 8E-6 F  (8 μF)
R = 6400 Ω

Results: 
Magnitude:  6401.24704262
Phase Angle: -1.1309750812°

Source:
Rosenstein, Morton.  Computing With the Scientific Calculator Casio.  Japan. 1986.  ISBN 1124161430


Eddie

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Sunday, July 12, 2020

HP Prime: Pythagorean Triangle Search

HP Prime: Pythagorean Triangle Search

Three Integers Make a Right Triangle

A Pythagorean triple is a trio of positive integers a, b, and c, that describe the lengths of a right triangle, where a and b are the lengths of the sides while c is the length of the hypotenuse. 

The variables a, b, and c follow the Pythagorean Theorem:

a^2 + b^2 = c^2

Where the following measurements perimeter and area are calculated as:

p = perimeter = a + b + c
r = area = a * b / 2

With two positive integers m and n where m > n, Euclid gives a formula where a, b, and c are generated:

a = 2 * m * n
b = m^2 - n^2
c = m^2 + n^2

This can easily verified to satisfy the Pythagorean Theorem:

a^2 + b^2 = c^2
(2*m*n)^2 + (m^2 - n^2)^2 = (m^2 + n^2)^2
4*m^2*n^2 + m^4 - 2*m^2*m^2 + n^4 = m^4 + 2*m^2*m^2 + n^4
4*m^2*n^2 + m^4 - 4*m^2*m^2 + n^4 = m^4 + n^4
m^4 + n^4 = m^4 + n^4

The following program PYTHRI asks you for m and n and generates a Pythagorean triple.  Make sure that m > n. 

HP Prime Program PYTHRI

EXPORT PYTHTRI()
BEGIN
// 2020-06-13 EWS
// r: area
LOCAL a,b,c,p,r,m,n;
INPUT({m,n},"Pythagorean Triple
 Generator",{"m = ","n = "},
{"m > n, m,n ∈ Z+","m > n, m,n ∈ Z+"});
a:=2*m*n; b:=m^2-n^2; c:=m^2+n^2;
p:=a+b+c; r:=a*b/2;
PRINT();
PRINT(a+"^2+"+b+"^2="+c+"^2");
PRINT("a = "+a);
PRINT("b = "+b);
PRINT("c = "+c);
PRINT("perimeter = "+p);
PRINT("area = "+r);
END;

Can We Go the Other Way?

Let's say we have the area and the perimeter of a right triangle.  Can we find a Pythagorean triple?  In order to do so, we need to solve for m and n, and make sure that m and n are positive integers. 

Recall that:

a = 2 * m * n
b = m^2 - n^2
c = m^2 + n^2

Perimeter:

p = a + b + c
p = 2 * m * n + m^2 - n^2 + m^2 + n^2
p = 2 * m^2 + 2 * m * n

Area:

r = a * b / 2
r = m * n * (m^2 - n^2)
r = m^3 * n - m * n^3

Let's solve for n in the perimeter equation:

p = 2 * m^2 + 2 * m * n
p - 2 * m^2 = 2 * m * n
Since m is a positive integer, m ≠ 0 and by dividing by 2 * m:
p / (2* m) - m = n

Substitute in the area equation:

r = m^3 * n - m * n^3
r = m^3 * (p / (2* m) - m) - m * (p / (2* m) - m)^3

The program IPYTHTRI attempts to find a Pythagorean triple by solving for m in the above equation.   A first initial guess of 0 is used, but the initial guess  uses powers of 10 for any further iterations that are needed. 

Should a triple not be found, the program will indicate the finding.  Perfect search is not guaranteed.

If a suitable solution is found, then the program calculates and displays a, b, and c.

HP Prime Program IPYTHTRI
(inverse PYTHTRI)

EXPORT IPYTHTRI()
BEGIN
// 2020-06-13 EWS
// r: area
LOCAL a,b,c,p,r,m,n,k;
INPUT({p,a},"Pythagorean Triple
 Search",{"p = ","r = "},
{"perimeter","area"});
// search for integers
FOR k FROM 0 TO 7 DO
m:=fsolve(X^3*(p/(2*X)-X)
-X*(p/(2*X)-X)^3-a,X,10*k);
IF (FP(m)==0) AND (m>0) THEN
BREAK;
END;
END;
n:=p/(2*m)-m;
PRINT();
PRINT("m = "+m);
PRINT("n = "+n);
IF (FP(m)==0) AND (FP(n)==0) THEN
PRINT("Integer Solutions Found");
a:=2*m*n; b:=m^2-n^2; c:=m^2+n^2;
PRINT(a+"^2+"+b+"^2="+c+"^2");
PRINT("a = "+a);
PRINT("b = "+b);
PRINT("c = "+c);
ELSE
PRINT("No integer solutions found");
END;
END;

Examples
(p = perimeter, r = area)

m = 5, n = 3
a = 30, b = 16, c = 34
p = 80, r = 240

m = 11, n = 6
a = 132, b = 85, c = 157
p = 374, r = 5610

m = 18, n = 14
a = 504, b = 128, c = 520
p = 1152, r = 32256

m = 164, n = 133
a = 43624, b = 9207, c = 44585
p = 97416, r = 200,823,084

Source:
Pythagorean triple.  Wikipedia.  Last Edited June 13, 2020.  https://en.wikipedia.org/wiki/Pythagorean_triple  Accessed June 13, 2020

Eddie

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Wednesday, March 22, 2017

HP 15C: Pythagorean Triples



HP 15C:  Pythagorean Triples

This program calculates the Pythagorean triple (A, B, C) such that A^2 + B^2 = C^2 by the formulas:

A = K * (M^2 – N^2)
B = K * (2 * M * N)
C = K * (M^2 + N^2)

The conditions are M, N, and K are all positive integers where M > N. 
Store M into memory 0, N into memory 1, and K into memory 2.  A, B, and C are stored in memories 3, 4, and 5, respectively.  If no such combination can be found, a single zero (0) is returned.

Step
Key
Code
001
LBL A
42, 21, 11
002
RCL 1
45, 1
003
RCL 0
45, 0
004
X≤0
43, 10
005
GTO 0
22, 0
006
RCL 0
45, 0
007
X^2
43, 11
008
RCL 1
45, 1
009
X^2
43, 11
010
-
30
011
STO 3
44, 3
012
LST X
43, 36
013
2
2
014
*
20
015
+
40
016
STO 5
44, 5
017
RCL 0
45, 0
018
RCL* 1
45, 20, 1
019
2
2
020
*
20
021
STO 4
44, 4
022
RCL 2
45, 2
023
STO* 3
44, 20, 3
024
STO* 4
44, 20, 4
025
STO* 5
44, 20, 5
026
RCL 3
45, 3
027
X^2
43, 11
028
RCL 4
45, 4
029
X^2
43, 11
030
+
40
031
RCL 5
45, 5
032
X^2
43, 11
033
-
30
034
X=0
43, 20
035
GTO 1
22, 1
036
LBL 0
42, 22, 1
037
0
0
038
RTN
43, 32
039
LBL 1
42, 21, 1
040
RCL 3
45, 3
041
R/S
31
042
RCL 4
45, 4
043
R/S
31
044
RCL 5
45, 5
045
RTN
43, 32

Example:  Input:  R0 = M = 4,  R1 = N = 1, R2 = 2.   Output:  30, 16, 34

This blog is property of Edward Shore, 2017.

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues The programs are listed for the Swiss Micros DM42 an...