(工具:Python)
1.从下面的无限序列中计算出π的值:
n=4;sum=0;a=0
for i in range(1,10000000):
if i%2!=0:
a=n/i
sum+=a
n=-n
print('%.5f'%sum)
2.输出所有的水仙花数(水仙花数:各位数字立方之和等于数本身的三位正整数):
a=0;b=0;c=0
for i in range(100,1000):
a = i%10
b = (i//10)%10
c = i//100
if i==a**3+b**3+c**3:
print('{};'.format(i))
3.从键盘输入一个正整数n(1-10之间),编程求的和:
sum=0;a=0
num = int(input('Please enter an integer between 1 and 10:'))
if (num >= 1) & (num <= 10):
for x in range(1,num+1):
a=2**x
sum+=a
print(sum)
else:
print('The number is not between 1 and 10')
4.
sum=0;a=2;b=1;m=0
n = int(input('Please enter n:'))
for i in range(1,n+1):
sum += (a/b)
m = a+b
b = a
a = m
print('%.2f'%sum)
写在后面:事实证明,在完成这种小数学题方面,Python的代码简洁性把C给爆了。
派神(Python),启动!