基础
读程序,总结程序的功能:
1
numbers = 1
for i in range(0,20):
numbers *= 2
print(numbers)
功能:求2的20次方
2
summation = 0
num = 1
while num <= 100:
if (num % 3 == 0 or num % 7 == 0) and num % 21 != 0:
summation += 1
num += 1
print(summation)
功能:找出在1~100中,能被3或7整除,且不能被21整除的数字个数。
编程实现(for和while各写一遍):
1
for
sum = 0
for x in range(1,101):
sum += x
print(sum,sum/100)
while
num = 0
sum = 0
while num <= 100:
sum += num
num += 1
print(sum,sum/100)
print(type(sum and sum/100))
2
for
sum = 0
for x in range(1,101):
if x % 3 == 0:
sum += x
print(sum)
while
x = 1
sum = 0
while x <= 100:
if x % 3 == 0:
sum += x
x += 1
print(sum)
3
for
sum = 0
for x in range(1,101):
if x % 7 != 0:
sum += x
print(sum)
while
x = 1
sum = 0
while x <= 100:
if x % 7 != 0:
sum += x
x += 1
print(sum)
求斐波那契数列中的第n个数是多少?1 1 2 3 5 8 13 21...
n = 6
pre_1 = 1
pre_2 = 1
current = 0
for x in range(1,n+1):
if x == 1 or x == 2:
# print(1)
continue
current = pre_1 + pre_2
# pre_1 = current
# pre_2 = pre_1
pre_2 = pre_1
pre_1 = current
print(current)
判断101-200之间有多少个素数,并输出所有素数。
for num in range(101,201):
count = 0
for x in range(2,num):
if num % x == 0:
count += 1
break # print('%d不是素数'%num)
if count == 0:
print('%d是素数'%num)
for x in (2,3,4):
print(x)
number = 101
x=(2-100)
x=2 101%2==0
x=3 101%3==0
x=4 101%4==0
x=5
x=6
x=100 101%100==0
number=102
x=(2,101)
x=2 102%2==0 102不是素数
number=103
x=(2,102)
打印出所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身。
例如:153是一个水仙花数,因为153 = 1^3 + 5^3 + 3^3
for num in range(100,999):
ge_wei = num % 10
shi_wei = num // 10 % 10
bai_wei = num // 100
if num == ge_wei**3 + shi_wei**3 + bai_wei**3:
print('%d是水仙数'%num)
网友评论