Danh sách các số chia hết cho 3 trong python

Viết chương trình Python để tìm các số chia hết cho mười chín hoặc mười ba từ danh sách các số bằng Lambda

Giải pháp mẫu

Mã Python

nums = [19, 65, 57, 39, 152, 639, 121, 44, 90, 190]
print["Orginal list:"]
print[nums] 
result = list[filter[lambda x: [x % 19 == 0 or x % 13 == 0], nums]] 
print["\nNumbers of the above list divisible by nineteen or thirteen:"]
print[result]

Đầu ra mẫu

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]

Trực quan hóa việc thực thi mã Python

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên

//Để in các số chia hết cho cả 3 và 7 từ 1 đến 100
print["Các số chia hết cho 3 và 7 từ 1 đến 100 là. "]
cho tôi trong phạm vi [1.100].
if [i % 3 == 0 và i % 7 == 0].
in[i]

Trong chương trình bên dưới, chúng tôi đã sử dụng hàm ẩn danh [lambda] bên trong hàm có sẵn filter[] để tìm tất cả các số chia hết cho 13 trong danh sách

Mã nguồn

# Take a list of numbers
my_list = [12, 65, 54, 39, 102, 339, 221,]

# use anonymous function to filter
result = list[filter[lambda x: [x % 13 == 0], my_list]]

# display the result
print["Numbers divisible by 13 are",result]

đầu ra

Numbers divisible by 13 are [65, 39, 221]

Tìm hiểu thêm về filter[] tại Python filter[]

Cách tiếp cận. Ví dụ: giả sử N = 20 làm giới hạn, thì chương trình sẽ in tất cả các số nhỏ hơn 20 chia hết cho cả 3 và 5. Đối với điều này, hãy chia mỗi số từ 0 đến N cho cả 3 và 5 và kiểm tra phần còn lại của chúng. Nếu phần còn lại là 0 trong cả hai trường hợp thì chỉ cần in số đó

Dưới đây là việc thực hiện.  

Python3




# Python program to print all the numbers

# divisible by 3 and 5 for a given number

________số 8

# Result function with N

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
0
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
1

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
2

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
3____14

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
3# Python program to print all the numbers1 # Python program to print all the numbers2# Python program to print all the numbers3 # Python program to print all the numbers4# Python program to print all the numbers5

# Python program to print all the numbers6

# Python program to print all the numbers7# Python program to print all the numbers8

# Python program to print all the numbers7# divisible by 3 and 5 for a given number0 # Python program to print all the numbers2# divisible by 3 and 5 for a given number2 # divisible by 3 and 5 for a given number3 # divisible by 3 and 5 for a given number4# divisible by 3 and 5 for a given number4 # divisible by 3 and 5 for a given number6 # divisible by 3 and 5 for a given number7 # Python program to print all the numbers2# divisible by 3 and 5 for a given number2  0 # divisible by 3 and 5 for a given number4# divisible by 3 and 5 for a given number4 # divisible by 3 and 5 for a given number6 4

 5 6 7 8 9# Result function with N0 # Result function with N1# Result function with N2# divisible by 3 and 5 for a given number4 # Result function with N4

# Result function with N5

# Python program to print all the numbers7# Result function with N7____84

 5

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
00

________số 8

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
02

# divisible by 3 and 5 for a given number0

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
04# divisible by 3 and 5 for a given number4# divisible by 3 and 5 for a given number4
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
07 4

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
2

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
3____111

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
3____113# divisible by 3 and 5 for a given number4
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
15

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
2

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
3____118

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
3____120

Độ phức tạp về thời gian. O[N]
Không gian phụ. Ô[1]

Phương pháp thay thế. Điều này cũng có thể được thực hiện bằng cách kiểm tra xem số đó có chia hết cho 15 hay không, vì BCNN của 3 và 5 là 15 và bất kỳ số nào chia hết cho 15 cũng chia hết cho 3 và 5 và ngược lại.  

Python3




Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
21

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
22

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
23# divisible by 3 and 5 for a given number4
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
25

# Python program to print all the numbers1

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
27# Python program to print all the numbers3 # Python program to print all the numbers4 7# divisible by 3 and 5 for a given number6
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
32

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
33
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
34

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
33# divisible by 3 and 5 for a given number0
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
27# divisible by 3 and 5 for a given number2
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
39# divisible by 3 and 5 for a given number4# divisible by 3 and 5 for a given number4# divisible by 3 and 5 for a given number6 4

Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
3______86
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
46# divisible by 3 and 5 for a given number4# Result function with N1
Orginal list:
[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]

Numbers of the above list divisible by nineteen or thirteen:
[19, 65, 57, 39, 152, 190]
49

Đầu ra

0 15 30 45 

Độ phức tạp về thời gian. O[n]
Không gian phụ. Ô[1]

Mời các bạn tham khảo bài viết đầy đủ về Chương trình in các số chia hết cho 3 và 5 cho một số cho trước để biết thêm chi tiết

Danh sách chia hết cho 3 là gì?

3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69 . Có 33 số có 2 chữ số chia hết cho 3. . There are 33 2-digit numbers that are divisible by 3.

Có bao nhiêu số chia hết cho 3?

Trả lời. [i] Các số có hai chữ số chia hết cho 3 là 12, 15, 18, 21, 24,…. ,99. Các số trên là A. P. Do đó 30 số có hai chữ số chia hết cho 3.

Tất cả các số chia hết cho 3 IN 1 đến 100 là gì?

bội số của 3. 3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63 . .

Chủ Đề