python课后题
求答案 =====第5题def check(a,b,c):
if (b**2)*(c**2)+(a**2)*(c**2) == (a**2)*(b**2):# 转换算式,浮点数计算验证会出错
return True
else:
return False
print('A\tB\tC')
for a in range(1,100):
for b in range(1,100):
for c in range(1,100):
if a > b and b >c and a+b+c<100:
if check(a,b,c):
print(a,'\t',b,'\t',c) 这么点悬赏给你做高等数学题?太天真了{:10_247:} =======第1题
def supersum(n):
result = 0
for i in range(1,n+1):
if i % 2 == 0:
x,y = i/2,i**3
else:
x,y = i,i**2
result += x - (y**2)
return result
print('请输入一个正整数:',end='')
while True:
temp = input()
try:
temp = int(temp.replace(' ','')) # 去掉空格后是否可为整数
except:
print('您的输入有误,请重新输入:',end='')
continue
if temp < 1:
print('您的输入有误,请重新输入:',end='')
continue
else:
break
result = supersum(temp)
print(result) =======第2题
def sum2(n):
s = 0
if n == 1:
s = 1
else:
for i in range(1,n):
s += 1/i
return s
i = 1 # 从1开始枚举,因为计算结果越来越大,所以只需要比较大于8的数和小于8的数谁更接近
while True:
if sum2(i) > 8:
temp = min((sum2(i)-8),(8-sum2(i-1)))
if temp > 8:
n,s = i,sum2(i)
else:
n,s = i-1,sum2(i-1)
break
i += 1
print('n:%d,s:%.6f' % (i,s)) =====第3题
def factor(n):
temp = []
for i in range(1,n+1):
if n % i == 0:
temp.append(i)
print(temp)
if len(temp) % 2 != 0:
return n
else:
return 0
result = []
for i in range(100,0,-1):
temp = factor(i)
if temp != 0:
result.append(i)
print('共有%d个,最大的整数是%d' % (len(result),result)) =====第4题
def isprime(n):
if n <=1:
return False
for i in range(2,n): # 素数从2开始,不包括自身
if n % i == 0:
return False
else:
return True
result = []
for i in range(1,22):
if isprime(2**i-1):
result.append(i)
print('共有%d个,最小的整数是%d,最大的整数是%d' % (len(result),result,result[-1]))
页:
[1]