positive integer m can be expresseed as the sum of three squares if it is of the form p + q + r where p, q, r ≥ 0, and p, q, r are all perfect squares. For instance, 2 can be written as 0+1+1 but 7 cannot be expressed as the sum of three squares. The first numbers that cannot be expressed as the sum of three squares are 7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, … (see Legendre's three-square theorem).
Write a Python function threesquares(m) that takes an integer m as input and returns True if m can be expressed as the sum of three squares and False otherwise. (If m is not positive, your function should return False.)
Here are some examples of how your function should work.
>>> threesquares(6) True >>> threesquares(188) False >>> threesquares(1000) True
Answers
Answered by
2
import math
def threesquares(m):
if m<0:
return False
else:
n=int(math.log10(m))
if n<1:
n=1
for a in range(n+1):
b=0
z=0
while z<=m:
z=((pow(4,a))*(8*b+7))
if z==m:
return False
b+=1
return True
Similar questions