Post Reply 
Sum of Two Squares
08-17-2019, 01:35 PM
Post: #1
Sum of Two Squares
Given a positive integer n, can we find two non-negative integers x and y such that:

n = x^2 + y^2

The program presented here is the use of iterations to find all possible pairs which fit n = x^2 + y^2. Some integers do not have representations, others have more than one. The program will show all possible combinations.

HP Prime Program SUM2SQ
Code:

EXPORT SUM2SQ(n)
BEGIN
// EWS 2019-07-21
// breaking n into a sum of 2 squares
LOCAL r,j,k,l;
// we can more than 1 representation
r:=IP((n/2)^0.5);
l:={};
FOR j FROM 0 TO r DO
k:=(n-j^2)^0.5;
IF FP(k)==0 THEN
l:=CONCAT(l,
{STRING(j)+"^2 + "+
STRING(k)+"^2 = "+
STRING(n)});
END;
END;

RETURN l;
END;

Blog link: http://edspi31415.blogspot.com/2019/08/h...f-two.html
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Sum of Two Squares - Eddie W. Shore - 08-17-2019 01:35 PM
RE: Sum of Two Squares - klesl - 08-17-2019, 07:05 PM
RE: Sum of Two Squares - Eddie W. Shore - 08-20-2019, 05:25 AM
RE: Sum of Two Squares - Eddie W. Shore - 08-18-2019, 05:27 PM



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