4 回答

TA贡献1835条经验 获得超7个赞
你提到勒让德的三平方定理。这给出了一个数字 n 可以表示为三个平方和的条件:如果 n != 4^a(8b+7)。
这给出了一个简单的 O(log(n)) 测试,用于打印小于 500 且不是三个平方和的数字。
def is_3square(n):
while n > 0 and n % 4 == 0:
n //= 4
return n % 8 != 7
for i in range(500):
if not is_3square(i):
print(i, end=', ')
print()

TA贡献1833条经验 获得超4个赞
我认为这是一个 NPTEL 问题。一开始我发现它很棘手,现在我已经完成了。
def threesquares(n):
list1=[]
flag=False
for i in range(0,100):
for j in range(0,100):
tempVar=(4^i)*(8*j)
list1.append(tempVar)
if n not in list1 and n > 0:
flag = True
else:
flag = False
return(flag)

TA贡献2039条经验 获得超8个赞
这可能有效:
failed = set( 7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, 79, 87, 92, 95,
103, 111, 112, 119, 124, 127, 135, 143, 151, 156, 159, 167, 175, 183,
188, 191, 199, 207, 215, 220, 223, 231, 239, 240, 247, 252, 255, 263,
271, 279, 284, 287, 295, 303, 311, 316, 319, 327, 335, 343 )
def squares( m ) :
if m > 343 :
print( 'choose a smaller number' )
return m not in the failed
您可以使用一个简单的公式创建任意大小的列表:4^i(8j+7), i >= 0, j >= 0

TA贡献1805条经验 获得超10个赞
它会起作用的
import math
def threesquares(m):
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
我正在使用勒让德的三平方定理
添加回答
举报