asdfg5954457
发表于 2018-6-3 03:44:58
堕落之翼 发表于 2018-6-2 09:38
這個代碼沒有打印出全部的答案阿!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
betlome
发表于 2018-6-3 16:20:26
def findxyz(max_num = 100):
max_z = int(10100**(1/3))
max_y = int(max_z**0.5)
max_x = max_num +1
for x in range(max_x):
for y in range(max_y):
for z in range(max_z):
if x + y**2 == z**3:
print(x,y,z)
betlome
发表于 2018-6-3 20:24:02
def findxyz(max_num = 100):
max_z = int(10100**(1/3))
max_y = int(max_z**0.5)
max_x = max_num +1
for x in range(max_x):
for y in range(max_y):
if type((x+y**2)**(1/3)) == int:
for z in range(max_z):
if x + y**2 == z**3:
print(x,y,z)
betlome
发表于 2018-6-3 20:24:49
def findxyz(max_num = 100):
max_z = int(10100**(1/3))
max_y = int(max_z**0.5)
max_x = max_num +1
for x in range(max_x):
for y in range(max_y):
if type((x+y**2)**(1/3)) == int:
for z in range(max_z):
if x + y**2 == z**3:
print(x,y,z)
badaoqingchen
发表于 2018-6-4 09:30:39
x, y 都取最大值时,x+y**2 == 10100, 10100开三次方大约20.xx,由此可见, z的范围不用写到(1,101),只要写到(1,21)就足够了。
所以,x循环100次时 y要循环一百次,总共是10000次, 而每一次循环中,z可以少80次,如此,可减少近80万次计算。。。
import time
start = time.time()
for i in xrange(1, 101):
for j in xrange(1, 101):
for k in xrange(1,21):
if i + j**2 == k**3:
print i, j, k
print time.time()-start
grf1973
发表于 2018-6-4 09:46:02
res=sorted([(z**3-y**2,y,z) for y in range(1,101) for z in range(1,101) if 1<=z**3-y**2<=100])
for x,y,z in res:
print(x,y,z)
tsembrace
发表于 2018-6-4 10:04:29
'''
求出所以的x, y, z值,使得x + y2 = z3
x, y, z均为1~100的正整数。
要求输出x, y, z的值,每种可能占一行,例如:
2 5 3
4 2 2
4 11 5
'''
import math
#求z的最大取值
z_max = int((100+100*100)**(1/3))+1
for z in range(1,z_max):
for y in range(1,101):
x = z**3 - y**2
if x < 100 and x > 0:
print ('%d %d %d ' %(x,y,z))
qq403711471
发表于 2018-6-4 17:03:08
for x in range(1,101):
for y in range(1,101):
for z in range(1,21):
if x + y**2 == z**3:
print(x,y,z)
大头目
发表于 2018-6-5 13:41:13
import time
t1 = time.clock()
for x in range(1,101):
for y in range(1,101):
for z in range(1,101):
if x + y**2 == z**3:
print(x,y,z)
t2 = time.clock()
print(t2 - t1)
t1 = time.clock()
maxz=int(pow(10100,1/3))#x+y**2最大值为10100
for z in range(1,maxz+1):
maxy=int(pow(z**3,1/2)) #y**2的最大值为z**3,此时x为0
for y in range(1,maxy+1):
x=z**3-y*y
if x in range(1,101): #算出x的值需要在1~100之间
print(x,y,z)
t2 = time.clock()
print(t2 - t1)
kk的作业
发表于 2018-6-7 01:43:04
import math
for x in range(1,101):
for y in range(1,101):
m = x + y ** 2
n = int(m ** (1/3))
for z in range(n,n+2):
if z ** 3 == m:
print(x,y,z)
夏未至夜未央
发表于 2018-6-12 14:43:54
list1 = []
list2 = []
list3 = []
for z in range(1, 101):
for y in range(1, 101):
if (z**3 - y**2) - int((z**3 - y**2)) == 0 and (z**3 - y**2)>0 and (z**3 - y**2)<101:
list1.append(z**3 - y**2)
list2.append(y)
list3.append(z)
for x in zip(list1, list2, list3):
print(x, x, x)
print(len(list1))
victor.xu
发表于 2018-6-15 10:11:22
def fun(n):
lis =
lis1 = []
for x in lis:
for y in lis:
for z in lis:
if x + y**2 == z**3:
lis2 =
lis1.append(lis2)
return lis1
n = 100
result = fun(100)
for i in result:
print('%d %d %d' % (i,i,i))
puddingYevtee
发表于 2018-6-15 11:04:03
for x in range(100):
for y in range(100):
for z in range(100):
if x + y **2 == z**3:
print(x,'\t',y,'\t',z)
victor.xu
发表于 2018-6-19 16:08:42
def fun(n):
lis =
lis1 = []
for x in lis:
for y in lis:
for z in lis:
if x + y**2 == z**3:
lis2 =
lis1.append(lis2)
return lis1
n = 100
result = fun(100)
for i in result:
print('%d %d %d' % (i,i,i))
海怪橙橙
发表于 2018-6-20 17:13:20
shoufei 发表于 2018-6-2 19:03
for x in range(1,100):
for y in range(1,100):
for z in range(1,100):
pow效率不如**,**就是快10倍以上!
duliping
发表于 2018-7-11 11:27:48
本帖最后由 duliping 于 2018-7-11 11:36 编辑
x = list(range(1, 101))
# 生成 y 列表
y = []
for i in x:
y.append(i*i)
# 根据 x 和 y 列表计算出 x + y 的最大值
max = x[-1] + y[-1]
# 生成 z 列表
z = []
for each in zip(x, y):
item = each * each
if item > max:
break
z.append(item)
# 计算结果
for i in z:
for j in x:
diff = i - j
if diff <= 0:
break
if diff in y:
print(j, y.index(diff)+1, z.index(i)+1)
运行结果:
4 2 2
7 1 2
2 5 3
11 4 3
18 3 3
23 2 3
26 1 3
15 7 4
28 6 4
39 5 4
48 4 4
55 3 4
60 2 4
63 1 4
4 11 5
25 10 5
44 9 5
61 8 5
76 7 5
89 6 5
100 5 5
20 14 6
47 13 6
72 12 6
95 11 6
19 18 7
54 17 7
87 16 7
28 22 8
71 21 8
53 26 9
39 31 10
100 30 10
35 36 11
47 41 12
81 46 13
40 52 14
11 58 15
13 70 17
56 76 18
79 89 20
45 96 21
graceasyi
发表于 2018-7-16 17:54:21
z3 =
y2 =
x1 =
for i in x1:
for j in y2:
if (i+j) in z3:
print(i, y2.index(j)+1, z3.index(i+j)+1)
lunagua
发表于 2018-7-16 19:25:32
本帖最后由 lunagua 于 2018-7-16 19:26 编辑
for x in range(1,101):
for y in range(1,101):
for z in range(1,101):
if (x+y**2) == z**3:
print(x,y,z)
solomonxian
发表于 2018-7-19 23:59:23
这样不涉及小数,但是嵌套循环免不了
def fun2(n=100):
result = ""
for z in range(1, n+1):
for y in range(1, n+1):
x = z**3 - y**2
if 1<= x <= n:
result += "{} {} {}\n".format(x, y, z)
return result
子沙
发表于 2018-10-2 12:55:10
本帖最后由 子沙 于 2018-10-2 13:12 编辑
import math
def is_square(num):
if isinstance(num,int):
if int(math.sqrt(num))**2==num:
return True
def fun_178(y):
times=0
for i in range(2,int((y+y**2)**(1/3))+1):
temp=int(i**(3/2)) if not is_square(i) else int(i**(3/2))-1
for j in range(temp,0,-1):
if i**3-temp**2<=y and i**3-temp**2>0:
print(i**3-temp**2,temp,i)
temp,times=temp-1,times+1
else:
break
print('共有%d组符合条件的数'%times)
fun_178(100)
结果如下:
4 2 2
7 1 2
2 5 3
11 4 3
18 3 3
23 2 3
26 1 3
15 7 4
28 6 4
39 5 4
48 4 4
55 3 4
60 2 4
63 1 4
4 11 5
25 10 5
44 9 5
61 8 5
76 7 5
89 6 5
100 5 5
20 14 6
47 13 6
72 12 6
95 11 6
19 18 7
54 17 7
87 16 7
28 22 8
71 21 8
53 26 9
39 31 10
100 30 10
35 36 11
47 41 12
81 46 13
40 52 14
11 58 15
13 70 17
56 76 18
79 89 20
45 96 21
共有42组符合条件的数