Sum of Two Squares
08-17-2019, 01:35 PM
Post: #1
 Eddie W. Shore Senior Member Posts: 1,037 Joined: Dec 2013
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;