Python程序:在给定范围内查找可被7整除且是5的倍数的数字


一个数n的倍数也同时可以被n整除。如果一个数M是n的倍数,那么当我们用n除M时,余数必须为零。此外,为了在给定范围内创建数n的倍数,我们可以多次添加n并检查结果数是否在给定范围内。另一种在给定范围内生成给定数n的倍数的方法是,找到n在给定范围内的第一个倍数,然后找到与n相乘得到该倍数的数q的值。然后将q递增1,并继续用它乘以n,直到我们得到给定范围内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")

查看结果 - 示例1

要查看结果,请在cmd窗口中运行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的倍数的数字。

算法

步骤1 - 指定范围内的最小和最大数字。

步骤2 - 首先确定范围内可被7整除的最小数字。不断向其添加7,直到找到该范围内最大的此类数字。打印这些数字。

步骤3 - 首先找到范围内5的倍数的最小数字。不断向其添加5,直到找到该范围内最大的此类数字。打印这些数字。

步骤4 - 通过查找两个列表中的公共元素,还要打印同时满足步骤2和步骤3中给定规则的数字。

步骤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")

查看结果 - 示例2

打开cmd窗口并运行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整除的最小数字。如果它是7的第q个倍数,则将q递增1,并继续查找7*q,直到找到该范围内最大的此类数字。打印这些数字。

步骤3 - 然后找到范围内5的倍数的最小数字。如果它是5的第q个倍数,则将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")

查看结果 - 示例3

打开cmd窗口并运行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的倍数。

更新于: 2023年7月10日

1K+ 阅读量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告