1、求1到100之间所有数的和、平均值
sum1 = 0
for x in range(1,101):
sum +=1
print('1~100的和:%d,平均值为:%.2f' % (sum1,sum1/100))
sum1 = 0
x = 100
while x >=1:
sum1 += 1
x -= 1
print('1~100的和:%d,平均值为:%.2f' % (sum1,sum1/100))
2、计算1~100之间能被3整除的数的和
sum1 = 0
for x in range(3,101,3):
sum1 += x
print(sum1)
sum1 = 0
for x in range(1,101):
if x % 3 :
continue
sum1 +=x
print(sum1)
sum1 = 0
x = 1
while x <= 100:
if x % 3 ==0:
sum1 += x
x +=1
print(sum1)
3、求斐波那契数列中第n个数的值:1,1,2,3,5,8,13,21,34....
n = int(input('请给出获取第几个数:'))
if n==1 or n == 2:
current = 1
p1 = 1
p2 = 2
for index in range(3,n+1):
current = p1 +p2
p1 = p2
p2 = current
print(current)
4、判断101-200之间有多少个素数,并输出所有素数。
count = 0
for x in range(101,201):
for y in range(2,x):
if x % y ==0:
break
else:
count += 1
print(x,'是素数')
print('101~200总共有%d个素数'%(count))
for x in range(101,201):
count = 0
for y in range(2,x):
if x % y == 0:
count +=1
break
if count == 0:
print(x,'是素数')
print('101~200总共有%d个素数'%(count))
5、打印出所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身。 例如:153是一个水仙花数,因为153 = 1^3 + 5^3 + 3^3
for x in range(100,1000):
ge_wei = x %10
shi_wei = x//10%10
bai_wei = x // 100
if x == ge_wei**3 + shi_wei**3 + bai_wei**3
print('%d是水仙花数' % (x))
6.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的第20个分数。分字:上一个分数的分字加分母 分母:上一个分数的分子 fz=2 fm=1 fz+fm/fz
fz = 2
fm =1
for x in range(2,21):
fen_shu = (fz+fm)/fz
bian_liang = fz
fz = fz + fm
fm = bian_liang
print('这个数列的第20个分数为%d'%(fen_shu))
7、给出一个整数,要求:1、求它是几位数;2、逆序打印出各位数字
import random
number = random.randint(100,10000000)
print('数字',number)
num_str = str(number)
print('数字%d是%d位数' % (number,len(num_str)))
print(num_str[-1::-1])