Python 查找范围内可以被7整除且是5的倍数的数字程序
一个数字的倍数也能被同样的数字整除。如果一个数字M是数字n的倍数,那么当我们用n除以M时,余数必须是零。为了在给定范围内生成数字n的倍数,我们可以多次添加n并检查生成的数字是否在给定范围内。另一种生成给定范围内数字n的倍数的方法是找到给定范围内的第一个倍数,然后找到与之相乘得到该倍数的数字q的值。然后将q增加1,并将n与之相乘,直至得到给定范围内的所有倍数。
示例1 -使用余数为零的方法在给定范围内查找可以被7整除且是5的倍数的数字
步骤
步骤1: 指定范围内的最小和最大数。
步骤2: 如果被7整除的数字在此范围内的余数为零,则选择此数字。打印出该数字。
步骤3: 如果被5整除的数字在此范围内的余数为零,则选择此数字。打印出该数字。
步骤4: 还要打印出那些同时遵循步骤2和步骤3规则的数字。
步骤5: 运行程序并检查结果。
Python文件包含如下内容:
lowNum=50
highNum=100
print("\nIn the given range from ", lowNum, " to", highNum, " :")
for item in range(lowNum, highNum):
if (item % 7 == 0):
print("This number ", item, "is divisible by 7")
print("\nIn the given range from ", lowNum, " to", highNum, " :")
for item in range(lowNum, highNum):
if (item % 5 == 0):
print("This number ", item, " is multiple of 5")
print("\nIn the given range from ", lowNum, " to", highNum, " :")
for item in range(lowNum, highNum):
if (item % 7 == 0) and (item % 5 == 0):
print("This number ", item, "is divisible by 7 and also a multiple of 5")
结果
要查看结果,请在命令行窗口中运行Python文件。
In the given range from 50 to 100 :
This number 56 is divisible by 7
This number 63 is divisible by 7
This number 70 is divisible by 7
This number 77 is divisible by 7
This number 84 is divisible by 7
This number 91 is divisible by 7
This number 98 is divisible by 7
In the given range from 50 to 100 :
This number 50 is multiple of 5
This number 55 is multiple of 5
This number 60 is multiple of 5
This number 65 is multiple of 5
This number 70 is multiple of 5
This number 75 is multiple of 5
This number 80 is multiple of 5
This number 85 is multiple of 5
This number 90 is multiple of 5
This number 95 is multiple of 5
In the given range from 50 to 100 :
This number 70 is divisible by 7 and also a multiple of 5
图1:显示在命令窗口中的结果。
示例2:利用加法方法在给定范围内找到7的倍数和5的倍数的数字
步骤
第一步 − 指定范围内的最小和最大数字。
第二步 − 首先确定范围内第一个能被7整除的最小数字。不断将7加到它上面,直到找到范围内最大的这样的数字。打印这些数字。
第三步 − 首先找到范围内第一个是5的倍数的最小数字。不断将5加到它上面,直到找到范围内最大的这样的数字。打印这些数字。
第四步 − 同时打印出满足第二步和第三步规则的数字,通过找到两个列表中的共同元素。
第五步 − 执行程序,然后验证结果。
Python文件包含以下内容
list1=[]
list2=[]
lowNum=200
highNum=300
tobeadded=7 - (lowNum % 7)
startNum= lowNum + tobeadded
for item in range(lowNum, highNum):
if item==startNum:
list1.append(startNum)
startNum += 7
tobeadded1= 5 - (lowNum % 5)
startNum1= lowNum + tobeadded1
for item in range(lowNum, highNum):
if item==startNum1:
list2.append(startNum1)
startNum1 += 5
print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe following numbers are divisible by 7 :")
print(list1)
print("\nThe following numbers are multiple of 5 :")
print(list2)
inbothlists = [ele for ele in list1 if ele in list2]
print("\nIn the given range from ", lowNum, " to", highNum, " :")
print("These numbers ", inbothlists, "are divisible by 7 and also a multiple of 5")
结果
打开命令提示窗口并运行Python文件以查看结果。
D:\articles\pythonarticles\rangedivby7mulby5>py main1.py
In the given range from 200 to 300 :
The following numbers are divisible by 7 :
[203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294]
The following numbers are multiple of 5 :
[205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295]
In the given range from 200 to 300 :
These numbers [210, 245, 280] are divisible by 7 and also a multiple of 5
图2:以列表形式显示的结果。
示例3:使用乘法方法在给定范围内找到可被7整除且是5的倍数的数
步骤
步骤1 − 设置范围内的最小和最大数。
步骤2 − 首先,确定在范围内可被7整除的最小数。如果它是第q个7的倍数,就将q增加1,并继续找到7*q,直到找到该范围内的最大数。打印这些数字。
步骤3 − 然后找到范围内最小的5的倍数。如果它是第q个5的倍数,就将q增加1,并继续找到5*q,直到找到该范围内的最大数。打印这些数字。
步骤4 − 同时打印满足步骤2和步骤3中规定的规则的数字,通过找到两个列表中的公共元素。
步骤5 − 运行程序,然后检查结果。
该Python文件包含以下内容
list1=[]
list2=[]
lowNum=200
highNum=300
tobeadded=7 - (lowNum % 7)
startNum= lowNum + tobeadded
numoftimes=int(startNum/7)
print(numoftimes)
for item in range(lowNum, highNum):
if item==startNum:
list1.append(startNum)
numoftimes += 1
startNum = 7 * (numoftimes)
tobeadded1=5 - (lowNum % 5)
startNum1= lowNum + tobeadded1
numoftimess=int(startNum1/5)
print(numoftimess)
for item in range(lowNum, highNum):
if item==startNum1:
list2.append(startNum1)
numoftimess += 1
startNum1 = 5 * numoftimess
print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe following numbers are divisible by 7 :")
print(list1)
print("\nThe following numbers are multiple of 5 :")
print(list2)
inbothlists = [ele for ele in list1 if ele in list2]
print("\nIn the given range from ", lowNum, " to", highNum, " :")
print("These numbers ", inbothlists, "are divisible by 7 and also a multiple of 5")
结果
打开命令行窗口并运行Python文件以查看结果。
29
41
In the given range from 200 to 300 :
The following numbers are divisible by 7 :
[203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294]
The following numbers are multiple of 5 :
[205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295]
In the given range from 200 to 300 :
These numbers [210, 245, 280] are divisible by 7 and also a multiple of 5
图3: 以列表形式显示结果。
结论
在这篇Python文章中,使用三个不同的示例,介绍了如何找到给定范围内能被7整除以及是5的倍数的所有数字的方法。使用三种方法,即使用找到余数等于零的方法,多次添加数字,以及将数字n乘以数字q,以产生给定范围内给定数字n的倍数。