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的倍數。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP