Python:每日一题 178
本帖最后由 冬雪雪冬 于 2018-6-2 21:07 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。开始阶段是看不到其他人的回帖的,等答题完成,开始评分时再取消限制。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
求出所以的x, y, z值,使得x + y2 = z3
x, y, z均为1~100的正整数。
要求输出x, y, z的值,每种可能占一行,例如:
2 5 3
4 2 2
4 11 5
......
题目很简单,希望着重考虑执行的效率。
本帖最后由 Python小当家 于 2018-5-31 16:47 编辑
import math
for z in range(2,101):
z_3 = z*z*z
y_max = int(math.sqrt(z_3))+1
if 101 < y_max :
for y in range(1,101):
z_3_y_2 = z_3 - y*y
if z_3_y_2 > 0 and z_3_y_2<101 :
print(z_3_y_2 ,y ,z)
else :
for y in range(1,y_max):
z_3_y_2 = z_3 - y*y
if z_3_y_2 > 0 and z_3_y_2<101 :
print(z_3_y_2 ,y ,z)
7 1 2
4 2 2
26 1 3
23 2 3
18 3 3
11 4 3
2 5 3
63 1 4
60 2 4
55 3 4
48 4 4
39 5 4
28 6 4
15 7 4
100 5 5
89 6 5
76 7 5
61 8 5
44 9 5
25 10 5
4 11 5
95 11 6
72 12 6
47 13 6
20 14 6
87 16 7
54 17 7
19 18 7
71 21 8
28 22 8
53 26 9
100 30 10
39 31 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 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)
a=list(range(1,101))
for x in a:
for y in a:
f=1
for z in a:
if y+3>z:
break
b=pow(y,2)
c=pow(z,3)
if (x+b)==c:
print(x,y,z)
本帖最后由 z1446773686 于 2018-5-31 17:52 编辑
import time
tim1 = 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)
tim2 = time.clock()
print('***************')
tim3 = time.clock()
x = 1;y=1;z=1
while(x < 101):
while(y < 101):
while(z < 101):
if x + y**2 == z**3:
print(x,y,z)
z += 1
z = 1
y += 1
y = 1
x += 1
tim4 = time.clock()
print('For:',tim2 - tim1)
print('Wile:',tim4 - tim3)
IDLE运行时间如下:
For: 1.7575066624228262
Wile: 1.9743320664656385 本帖最后由 z1446773686 于 2018-5-31 17:47 编辑
z1446773686 发表于 2018-5-31 17:10
想不出来怎么用递归,哪位大佬能告诉我呢?{:5_96:} for x in range(1,101):
for y in range(1,100):
for z in range(1,100):
if x + y*y == z*z*z:
print(x, y, z)
不考虑效率的版本 maxz=int(pow(10100,1/3))
for z in range(1,maxz+1):
maxy=int(pow(z**3,1/2))
for y in range(1,maxy+1):
x=z**3-y*y
if x in range(1,101):
print(x,y,z) 本帖最后由 久疤K 于 2018-5-31 21:55 编辑
def fun(N = 101):
for x in range(1,N):
for y in range(1,N):
left_sum = x + y**2
sz = int( left_sum ** (1.0/3))
for z in range(sz,sz+2):
if z**3 == left_sum:
print(x,y,z)
break
def main():
fun()
if __name__ == "__main__":
main()
答案:2 5 3
4 2 2
4 11 5
7 1 2
11 4 3
11 58 15
13 70 17
15 7 4
18 3 3
19 18 7
20 14 6
23 2 3
25 10 5
26 1 3
28 6 4
28 22 8
35 36 11
39 5 4
39 31 10
40 52 14
44 9 5
45 96 21
47 13 6
47 41 12
48 4 4
53 26 9
54 17 7
55 3 4
56 76 18
60 2 4
61 8 5
63 1 4
71 21 8
72 12 6
76 7 5
79 89 20
81 46 13
87 16 7
89 6 5
95 11 6
100 5 5
100 30 10 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('%d %d %d'% (x, y, z))
def check(num):
a = num ** (1/3)
if int(a) == a:
return int(a)
else:
return False
def main():
l = []
for i in range(1, 100):
for j in range(1, 100):
for m in range(2, 100):
if i + j**2 == m**3 :
l.append()
for x in l:
print(x)
if __name__ == '__main__':
main() for x in range(1,101):
for y in range(1,101):
for z in range(1,21):
if x + y*y == z*z*z:
print(x,y,z) for z in range(1,101):
for y in range(1,101):
x = z**3 - y**2
if 100 >= x > 0:
print(x,y,z) def fun178(x):
for i in range(1,x+1):
for j in range(1,x+1):
for n in range(1,x+1):
if i + j**2 == n**3:
yield '%s %s %s'%(i,j,n)
if __name__ == '__main__':
for i in fun178(100):
print(i) import math
for a in range(1, 101):
for b in range(1, 101):
if math.ceil((a + b ** 2)**(1/3)) == (a + b ** 2)**(1/3):
print('%d %d %d' % (a, b, (a + b ** 2)**(1/3))) for x in range(1,100):
for y in range(1,100):
for z in range(1,100):
if z**3 == x + y**2:
print(x,y,z) 本帖最后由 qy0317 于 2018-6-1 16:02 编辑
import random
x=random.randint(1,101)
y=random.randint(1,101)
z=random.randint(1,101)
while 1 <= (x and y and z) <= 100:
if not(x + y**2 == z**3):
print(x,y,z)
x += 1
y += 1
z += 1
continue
0 for x in range(100):
for y in range(100):
for z in range(100):
if x+y*y==z*z*z:
print(x,y,z) 程序执行结果:
7 1 2
4 2 2
26 1 3
23 2 3
18 3 3
11 4 3
2 5 3
63 1 4
60 2 4
55 3 4
48 4 4
39 5 4
28 6 4
15 7 4
89 6 5
76 7 5
61 8 5
44 9 5
25 10 5
4 11 5
95 11 6
72 12 6
47 13 6
20 14 6
87 16 7
54 17 7
19 18 7
71 21 8
28 22 8
53 26 9
39 31 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
程序执行了0.178196s。
程序代码:
"""
求出所以的x, y, z值,使得x + y2 = z3
x, y, z均为1~100的正整数。
要求输出x, y, z的值,每种可能占一行,例如:
2 5 3
4 2 2
"""
import time
def square(start_number, end_number):
"""
求出范围内所有平方数,以列表返回
"""
result = []
for each in range(start_number, end_number):
sq = each * each
if sq <= end_number:
result.append()
else:
break
return result
def cubed(start_number, end_number):
"""
求出范围内所有立方数,以列表返回
"""
result = []
for each in range(start_number, end_number):
cube = each * each * each
if cube <= end_number:
result.append()
else:
break
return result
def main():
squ = square(1, 100 * 100)
cube = cubed(1, 100 * 100 * 100)
for z3 in cube:
for y2 in squ:
for x in range(1, 100):
if x + y2 == z3:
print(x, y2, z3)
if x >= z3:
break
if y2 >= z3:
break
if __name__ == '__main__':
start = time.clock()
main()
end = time.clock()
print("程序执行了%fs。" % (end - start))