Sum of squares
Q. A positive integer n is a sum of squares if n = i2 + j2 for integers i,j such that i ≥ 1 and j ≥ 1. For instance, 10 is a sum of squares because 10 = 12 + 32, and so is 25 (32 + 42). On the other hand, 11 and 3 are not sums of squares.
Write a Python function sumofsquares(n) that takes a positive integer argument and returns True if the integer is a sum of squares, and False otherwise.
Ans-
def sumofsquares(n):
(i,j)=(1,1)
for i in range(1,n):
for j in range(1,n):
if n == (i*i) + (j*j):
return (True)
return False
Comments
Post a Comment