for i in range(1500,2700)
if i % 7 == 5:
print(i)
判断素数方法:用N(正整数且不为1)与所有小于等于根号N的数相除。
from math import sqrt
num = int(input('请输入正整数:'))
end = int(sqrt(num))
is_prime = True
for i in range(2, end+1):
if num % i == 0:
is_prime = False
break
if is_prime and num != 1:
print('%d是素数' % num)
else:
print('%d不是素数' % num)
本质是分解质因数
举例
x = a * b * c
y = e * f * g
如果x, y没有相同质因数,则无最大公约数。
最小公倍数就是a * b * c * e * f * g
。
如果存在a = e
(如果同时b = f,可以把a * b整体看成一个因数,以此类推),那么最大公约数即为 a,最小公倍数为 a * b * c * f * g
。
由此,我们可以得知,求出最大公约数,也就求得了最小公倍数,而最大公约数最大为二者小的那个数。
x = int(input('x = '))
y = int(input('y = '))
if x > y:
x, y = y, x
for i in range(x, 0, -1):
if x % i == 0 and y % i == 0:
print('%d和%d的最大公约数是%d' %(x, y, i))
print('%d和%d的最小公倍数是%d' %(x, y, x * y / i))
break
注:不能排除最大公约数是1的情况
rows = int(input('请输入行数:'))
for i in range(rows):
for j in range(i + 1):
print('*', end = '')
print()
for i in range(rows):
for k in range(rows):
if k < rows - i - 1:
print(' ', end = '')
else:
print('*', end = '')
print()
for i in range(rows):
for m in range(rows - i - 1):
print(' ', end = '')
for n in range(2 * i + 1):
print('*' ,end = '')
print()
说明:水仙花数也被称为超完全数字不变数、自恋数、自幂数、阿姆斯特朗数,它是一个3位数,该数字每个位上数字的立方之和正好等于它本身,例如:
1 3 + 5 3 + 3 3 = 153 1^3+ 5^3+ 3^3=153 13+53+33=153。
for num in range(100, 1000):
gw = num % 10
sw = num // 10 % 10
bw = num // 100
if gw ** 3 + sw ** 3 + bw ** 3 == num:
print(num, end = ' ')
# 153 370 371 407
通过整除与取余获取各数位上的数字,瞬间让问题变得简单了。通过这个方法,我们可以将一个正整数反转。例如:1234变为4321。
num = int(input('请输入一个正整数:'))
reversed_num = 0
while num > 0:
reversed_num = reversed_num * 10 + num % 10
num = num // 10
print(reversed_num)
说明:百钱百鸡是我国古代数学家张丘建在《算经》一书中提出的数学问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?翻译成现代文是:公鸡5元一只,母鸡3元一只,小鸡1元三只,用100块钱买一百只鸡,问公鸡、母鸡、小鸡各有多少只?
for x in range(0, 21):
for y in range(0, 34):
z = 100 - x - y
if 5 * x + 3 * y + z / 3 == 100:
print('公鸡:%d只\n母鸡:%d只\n小鸡:%d只' % (x, y, z))
说明:CRAPS又称花旗骰,是美国拉斯维加斯非常受欢迎的一种的桌上赌博游戏。该游戏使用两粒骰子,玩家通过摇两粒骰子获得点数进行游戏。简单的规则是:玩家第一次摇骰子如果摇出了7点或11点,玩家胜;玩家第一次如果摇出2点、3点或12点,庄家胜;其他点数玩家继续摇骰子,如果玩家摇出了7点,庄家胜;如果玩家摇出了第一次摇的点数,玩家胜;其他点数,玩家继续要骰子,直到分出胜负。
from random import randint
first = randint(2, 12)
if first == 7 or first == 11:
print('YOU WIN')
elif first == 2 or first == 3 or first == 12:
print('YOU LOSE')
else:
current = 0
while current not in [7, first]:
current = randint(2, 12)
continue
else:
if current == first:
print('YOU WIN')
elif current == 7:
print('YOU LOSE')
from random import randint
money = int(input('请输入充值金额:'))
while money > 0:
print('你的总资产为:', money)
needs_go_on = False
while True:
debt = int(input('请下注:'))
try:
assert 0 < debt <= money
except AssertionError:
print('下注错误,请重新下注:')
else:
break
first = randint(1, 6) + randint(1, 6)
print('你的点数是:', first)
if first == 7 or first == 11:
print('YOU WIN')
money += debt
elif first == 2 or first == 3 or first == 12:
print('YOU LOSE')
money -= debt
else:
needs_go_on = True
while needs_go_on:
needs_go_on = False
current = randint(1, 6) + randint(1, 6)
print('你的点数是:', current)
if current == 7:
print('YOU LOSE')
money -= debt
elif current == first:
print('YOU WIN')
money += debt
else:
needs_go_on = True
print('你破产了,游戏结束')
first, second, current = 1, 1, 1
count = 0
while count < 20:
count += 1
if count == 1 or count == 2:
print(current)
else:
current = first + second
first = second
second = current
print(current)
fb = [1, 1]
while len(fb) < 20:
fb.append(fb[-1] + fb[-2])
print(fb)
a, b = 0, 1
for i in range(20):
a, b = b, a + b
print(a, end = ' ')
for num in range(1, 10000):
sum = 0
for i in range(1, num // 2 + 1):
if num % i == 0:
sum += i
if num == sum:
print(num)
for num in range(1, 100):
end = num ** 0.5
is_prime = True
for i in range(2, int(end) + 1):
if num % i == 0:
is_prime = False
break
if is_prime and num != 1:
print(num)