1、求一个数是否是质数(素数)?
2、求50~150之间的质数是那些?
1,2是同一个题,会2就会1,直接输出2把
index = 50
while index <= 150:
s = 2
while s < index:
if index % s == 0:
break
s += 1
if s > index/2 :
print(index)
break
index += 1
*
**
***
****
*****
*
***
*****
*******
*
* *
* *
*******
*
***
*****
*******
*****
***
*
*
* *
* *
* *
* *
* *
*
*
***
* * *
*******
* * *
***
*
# *
# ***
# *****
#*******
n = int(input("请输入需要打印的行数:"))
for i in range(1,n+1):
for j in range(1,n-i+1):
print(" ",end="")
for k in range(1,2*i):
print("*",end="")
print()##换行
# *
# * *
# * *
# *******
n = int(input("请输入需要打印的行数:"))
for i in range(1,n+1):
if i < n :
for j in range(1,n-i+1):
print(" ",end="")
for k in range(1,2*i):
if k == 1 or k == 2*i -1 :
print("*",end = "")
else :
print(" ",end="")
else :
for z in range(1,2*n):
print("*",end = "")
print()##换行
##菱形,采用的是上下分别输出的形式,这个最容易想到
n = int(input("请输入需要打印的行数:"))
while n %2 == 0 or n < 2:
n = int(input("输入错误大于2行,且为奇数行,请重新输入:"))
for i in range(1,n//2 + 2):
for j in range(1,n//2 + 2-i):
print(" ",end = "")
for k in range(1,2*i):
print("*",end = "")
print("")
for m in range(1,n//2+1):
for a in range(1,m+1):
print(" ",end = "")
for b in range(1,2*(n//2+1-m)):
print("*",end="")
print("")
n = int(input("请输入需要打印的行数:"))
while n %2 == 0 or n < 2:
n = int(input("输入错误大于2行,且为奇数行,请重新输入:"))
for i in range(1,n//2+2):
for j in range(1,n//2 + 2-i):
print(" ",end = "")
for k in range(1,2*i):
if k == 1 or k == 2*i-1:
print("*",end = "")
else:
print(" ", end = "" )
print("")
for m in range(1,n//2+1):
for a in range(1,m+1):
print(" ",end = "")
for b in range(1,2*(n//2+1-m)):
if b == 1 or b == (2*(n//2+1-m)-1):
print("*",end="")
else:
print(" ",end = "")
print("")
n = int(input("请输入需要打印的行数:"))
while n %2 == 0 or n < 2:
n = int(input("输入错误大于2行,且为奇数行,请重新输入:"))
for i in range(1,n//2+1):
for j in range(1,n//2 + 2-i):
print(" ",end = "")
for k in range(1,2*i):
if k == 1 or k == 2*i-1 or k == i :
print("*",end = "")
else:
print(" ", end = "" )
print("")
for c in range(1,n+1):
print("*",end="")
print("")
for m in range(1,n//2+1):
for a in range(1,m+1):
print(" ",end = "")
for b in range(1,2*(n//2+1-m)):
if b == 1 or b == (2*(n//2+1-m)-1) or b == n//2+1-m :
print("*",end="")
else:
print(" ",end = "")
print("")
4、完整计算器程序
用户输入第一个数据
用户输入操作符号
用户输入第二个数据
运算展示结果
# 接收控制台上用户输入的数据
num1 = float(input("请输入第一个数:"))
op = input("请输入您要运算的符号(+、-、*、/、%、//、**):")
num2 = float(input("请输入第二个数:"))
# 数据运算
if op == "+":
print("%s + %s = %s"%(num1, num2, (num1 + num2)))
elif op == "-":
print("%s - %s = %s" %(num1, num2, (num1 - num2)))
elif op == "*":
print("%s * %s = %s" %(num1, num2, (num1 * num2)))
elif op == "/":
print("%s / %s = %s" %(num1, num2, (num1 / num2)))
elif op == "%":
print("%s %% %s \\n= %s " %(num1, num2, (num1 / num2)))
5、一个自然数与3的和是5的倍数,与3的差是6的倍数,这个自然数最小是几?
##自然这个数是比6大的,故从6开始,并且我输出了满足这个条件的10个数字
num = 6
count = 0
while True:
if (num+3)%5==0 and (num-3)%6==0:
print("这个数最小是:",num)
count +=1
num += 1
if count == 10:
break
else:
num += 1
6、在400–500之间求一个数,它被2除余1,被5除余3,被9除余1,这个数是多少?
##while实现
i = 400
while i <= 500:
if i%2 == 1 and i%5 == 3 and i%9 == 1:
print(i)
break
else:
i += 1
##for实现
for m in range(400,501):
if m%2 == 1 and m%5 == 3 and m%9 == 1:
print(m)
7、有一些四位数,百位数字都是3,十位数字都是6,并且它们既能被2整除,又能被3整除,求这样的四位数中最大的和最小的两数各是几?
#这里我采用了列表来存储满足的数字
ls = []
for i in range(1,10):
for j in range(1,10):
num = int(str(i)+"36"+str(j))
if num%2 ==0 and num %3 ==0:
ls.append(num)
ls.sort()#对数字进行排序,其实没有必要排序,第一个最小,最后一个最大。
print("满足要求的数字有:",ls)
print("最小值:{}最大值:{}".format(ls[0],ls[len(ls)-1])