1.讀程序虹蓄,總結(jié)功能
功能:冪運算普筹,本程序生成數(shù)0~19爆哑,即2^20
2.
功能:統(tǒng)計1~99中3和7的公倍數(shù)中奇數(shù)的個數(shù)
3.編程實現(xiàn)(for和while各寫一遍)
a.求1~100所有數(shù)的和什猖、平均值
for:
int_num = avg = 0
for n in range(101):
int_num += n
print(int_num)
avg = int_num /100
print(avg)
while:
num = sm =0
while num < 100:
num += 1
sm += num
print('和:',sm,'平均數(shù):',sm/100)
b.計算1~100間能被3整除的數(shù)的和
for:
sm =0
for num in range(1,100):
if(num % 3 == 0):
sm += num
print(sm)
while
sm = num = 0
while num >100:
num += 1
if(num % 3 == 0):
sm += num
print(sm)
c.計算1~100之間不能被7整除的數(shù)的和
for:
sm = 0
for num in range(100):
num += 1
if(num % 7 != 0):
sm += num
print(sm)
while:
sm = num =0
while num <= 100:
num += 1
if(num % 7 != 0):
sm += num
print(sm)
求斐波那契數(shù)列中第n個數(shù) 1牧愁,1素邪,2,3猪半,5兔朦,8,13办龄,21烘绽,34....
num = int(input(' 你想求斐波那契第幾個數(shù)傲苷选:'))
an = 1
an_1 = 1
if(num <= 2):
print(an)
else:
for _ in range(3,num):
cn = an
an += an_1
an_1 = cn
print(an)
判斷101-200之間有多少個素數(shù)俐填,并輸出所有素數(shù)。判斷素數(shù)的?法:??個數(shù)分別除2到sqrt(這個
數(shù))翔忽,如果能被整除英融,則表明此數(shù)不是素數(shù),反之是素數(shù)
con = con_1 = 0
for num in range(101,202):
c = 2
if con == 1:
con_1 += 1
print(num - 1)
while c < num/2:
con = 1
if(num % c == 0):
con += 1
break
c += 1
print(con_1)
打印出所有的?仙花數(shù),所謂?仙花數(shù)是指?個三位數(shù)歇式,其各位數(shù)字??和等于該數(shù)本身驶悟。例如: 153是
?個?仙花數(shù),因為153 = 1^3 + 5^3 + 3^3
for num in range(100,1000):
if(num == (num % 10) ** 3 +(num // 10 % 10) ** 3 + (num // 100) ** 3 ):
print(num)
-
有?分?jǐn)?shù)序列: 2/1,3/2,5/3,8/5,13/8,21/13...求出這個數(shù)列的第20個分?jǐn)?shù)
fz = 2
fm = 1
for num in range(20):
cn = fz
fz += fm
fm = cn
print(fz,'/',fm)
分?:上?個分?jǐn)?shù)的分?加分? 分?: 上?個分?jǐn)?shù)的分? fz = 2 fm = 1 fz+fm / fz
-
給?個正整數(shù),要求: 1材失、求它是?位數(shù) 2.逆序打印出各位數(shù)字
num = int(input('請輸入一個正整數(shù):'))
con = 0
while num != 0:
con += 1
w = num % 10
print(w)
num //=10
print('位數(shù) ',con)