Hướng dẫn how to check if number is multiple of 3 in python - cách kiểm tra xem số có phải là bội số của 3 trong python không

Tôi cần kiểm tra xem mỗi số từ 1 đến 1000 là bội số của 3 hoặc bội số của 5.

Tôi đã thử mã này trong Python 2.x:

n = 0
s = 0

while (n < 1001):
    x = n/3
    if isinstance(x, (int, long)):
        print 'Multiple of 3!'
        s = s + n
    if False:
        y = n/5
        if isinstance(y, (int, long)):
            s = s + n

    print 'Number: '
    print n
    print 'Sum:'
    print s
    n = n + 1

Ý tưởng là thử chia số và xem kết quả có phải là số nguyên không. Tuy nhiên, tôi không nhận được kết quả mong đợi.

Làm cách nào để kiểm tra xem số đó là số nguyên?


Trong 2.x, sự phân chia như thế này sẽ tạo ra một số nguyên, loại bỏ phần còn lại; Xem làm thế nào tôi có thể buộc phân chia để trở thành điểm nổi? Bộ phận tiếp tục làm tròn xuống 0? Để biết chi tiết.

Trong 3.x, bộ phận sẽ tạo ra giá trị điểm nổi; Kết quả không phải là "một số nguyên" ngay cả khi nó là một số toàn bộ, vì vậy kiểm tra isinstance sẽ thất bại. Xem tại sao bộ phận số nguyên lại tạo ra một chiếc phao thay vì một số nguyên khác? Để biết chi tiết.is not "an integer" even if it is a whole number, so the isinstance check will fail. See Why does integer division yield a float instead of another integer? for details.

Nếu bạn cần phần còn lại từ bộ phận số nguyên thay vì chỉ kiểm tra khả năng chia rẽ, hãy xem phần còn lại của một số.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    Examples : 

    Bàn luậnn = 10
    Output: Count = 4
    The multiples are 3, 6, 7 and 9

    Cho một số nguyên dương N, tìm số lượng của tất cả các bội số của 3 hoặc 7 nhỏ hơn hoặc bằng n.examples: & nbsp; n = 25
    Output: Count = 10
    The multiples are 3, 6, 7, 9, 12, 14, 15, 18, 21 and 24

    Đầu vào: N = 10Output: Count = 4 Buliples là 3, 6, 7 và 9

    C++

    #include

    Đầu vào: n = 25Output: Count = 10 bội số là 3, 6, 7, 9, 12, 14, 15, 18, 21 và 24

    Một giải pháp đơn giản là lặp lại tất cả các số từ 1 đến N và số lượng tăng bất cứ khi nào một số là bội số của 3 hoặc 7 hoặc cả. & NBSP;

    count = n/3 + n/7 - n/21 
    1

    using namespace std;

    int countMultiples(int

    count = n/3 + n/7 - n/21 
    0

    count = n/3 + n/7 - n/21 
    2int
    count = n/3 + n/7 - n/21 
    4

    isinstance3isinstance4

    count = n/3 + n/7 - n/21 
    2
    count = n/3 + n/7 - n/21 
    6
    count = n/3 + n/7 - n/21 
    7int
    count = n/3 + n/7 - n/21 
    9

    isinstance8

    isinstance0isinstance1 isinstance2

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2isinstance6 isinstance7

    isinstance8

    int #include0

    #include2#include3#include4 #include5

    Java

    count = n/3 + n/7 - n/21 
    1

    #include7 #include8

    count = n/3 + n/7 - n/21 
    1

    #include9 using0

    using2 int countMultiples(int

    count = n/3 + n/7 - n/21 
    0

    count = n/3 + n/7 - n/21 
    2int namespace0namespace1namespace2

    int1isinstance4

    count = n/3 + n/7 - n/21 
    2
    count = n/3 + n/7 - n/21 
    6
    count = n/3 + n/7 - n/21 
    7int
    count = n/3 + n/7 - n/21 
    9

    isinstance8

    isinstance0isinstance1 isinstance2

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2countMultiples(3#include4countMultiples(5

    count = n/3 + n/7 - n/21 
    2countMultiples(7countMultiples(8countMultiples(9

    isinstance8

    isinstance8

    Python3

    count = n/3 + n/7 - n/21 
    2isinstance6 isinstance7

    int #include0

    #include2#include3#include4 #include5

    Java

    #include7 #include8

    count = n/3 + n/7 - n/21 
    2
    count = n/3 + n/7 - n/21 
    6
    count = n/3 + n/7 - n/21 
    7int
    count = n/3 + n/7 - n/21 
    9

    count = n/3 + n/7 - n/21 
    35
    count = n/3 + n/7 - n/21 
    7
    count = n/3 + n/7 - n/21 
    37
    count = n/3 + n/7 - n/21 
    38countMultiples(8countMultiples(9

    C#

    isinstance0isinstance1 isinstance2

    Java

    count = n/3 + n/7 - n/21 
    1

    #include7 #include8

    count = n/3 + n/7 - n/21 
    1

    using namespace std;

    int countMultiples(int

    count = n/3 + n/7 - n/21 
    0

    count = n/3 + n/7 - n/21 
    2int
    count = n/3 + n/7 - n/21 
    4

    int1isinstance4

    count = n/3 + n/7 - n/21 
    2
    count = n/3 + n/7 - n/21 
    6
    count = n/3 + n/7 - n/21 
    7int
    count = n/3 + n/7 - n/21 
    9

    isinstance8

    isinstance0isinstance1 isinstance2

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2
    count = n/3 + n/7 - n/21 
    75#include4countMultiples(5

    count = n/3 + n/7 - n/21 
    2
    count = n/3 + n/7 - n/21 
    79

    isinstance8

    isinstance8

    count = n/3 + n/7 - n/21 2isinstance6 isinstance7

    count = n/3 + n/7 - n/21 
    82

    int #include0

    count = n/3 + n/7 - n/21 
    1

    #include2#include3#include4 #include5

    Java

    #include7 #include8

    int1

    count = n/3 + n/7 - n/21 
    89isinstance11

    count = n/3 + n/7 - n/21 
    2isinstance6
    count = n/3 + n/7 - n/21 
    89namespace2

    isinstance8

    isinstance17 #include4 isinstance19

    isinstance20

    JavaScript

    isinstance21

    count = n/3 + n/7 - n/21 
    83 isinstance23

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2isinstance26

    count = n/3 + n/7 - n/21 
    2
    count = n/3 + n/7 - n/21 
    6 isinstance29

    count = n/3 + n/7 - n/21 
    2isinstance1
    count = n/3 + n/7 - n/21 
    62

    int1isinstance4

    count = n/3 + n/7 - n/21 
    2isinstance6 isinstance7

    isinstance8

    isinstance39#include4 isinstance41

    isinstance42

    Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (1) O(n)
    Auxiliary Space: O(1)

    Một giải pháp hiệu quả có thể giải quyết vấn đề trên trong thời gian O (1). Ý tưởng là đếm bội số của 3 và thêm bội số của 7, sau đó trừ bội số của 21 vì chúng được tính hai lần. & NBSP;

    count = n/3 + n/7 - n/21 

    C++

    #include

    using namespace std;

    int countMultiples(int

    count = n/3 + n/7 - n/21 
    0

    count = n/3 + n/7 - n/21 
    1

    #include2isinstance6 isinstance54

    isinstance8

    int #include0

    count = n/3 + n/7 - n/21 
    1

    #include2#include3#include4 #include5

    isinstance8

    Java

    #include7 #include8

    #include9 using0

    count = n/3 + n/7 - n/21 
    1

    using2 int countMultiples(int

    count = n/3 + n/7 - n/21 
    0

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2isinstance6 isinstance77std;3 isinstance79std;7

    isinstance8

    int7 using2 int9 isinstance88

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2isinstance91#include4
    count = n/3 + n/7 - n/21 
    07

    isinstance94countMultiples(countMultiples(8countMultiples(9

    isinstance8

    isinstance8

    Python 3

    int2 int3

    count = n/3 + n/7 - n/21 
    2isinstance6 #include04#include05 std;3
    count = n/3 + n/7 - n/21 
    07 #include04__

    #include04int6 #include18int#include20countMultiples(8#include22

    count = n/3 + n/7 - n/21 
    35
    count = n/3 + n/7 - n/21 
    7
    count = n/3 + n/7 - n/21 
    37#include26

    C#

    using

    count = n/3 + n/7 - n/21 
    42

    #include9 using0

    count = n/3 + n/7 - n/21 
    1

    using2 int countMultiples(int

    count = n/3 + n/7 - n/21 
    0

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2isinstance6 isinstance77std;3 isinstance79std;7

    isinstance8

    int7 using2 int9 isinstance88

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2isinstance91#include4
    count = n/3 + n/7 - n/21 
    07

    #include51#include52

    isinstance8

    isinstance8

    Python 3

    count = n/3 + n/7 - n/21 
    82

    int2 int3

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2isinstance6 #include04#include05 std;3
    count = n/3 + n/7 - n/21 
    07 #include04__

    isinstance8

    #include04int6 #include18int#include20countMultiples(8#include22

    isinstance20

    JavaScript

    isinstance21

    count = n/3 + n/7 - n/21 
    83 isinstance23

    count = n/3 + n/7 - n/21 
    1

    count = n/3 + n/7 - n/21 
    2
    count = n/3 + n/7 - n/21 
    6 isinstance29

    isinstance8

    isinstance39#include4 isinstance41

    isinstance42

    Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (1)O(1)
    Auxiliary Space: O(1)
    Exercise: 
    Now try the problem of finding sum of all numbers less than or equal to n and multiples of 3 or 7 or both in O(1) time.
    This article is contributed by Saurabh Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    Làm thế nào để bạn kiểm tra xem một số là bội số của 3?

    Một số, x, được cho là bội số của một số khác, y, nếu phần còn lại của x/y x/y x/y là 0. tương tự, một số, n, sẽ là bội số của 3 và 5 nếu phần còn lại của N/3 N/3 N/3 và N/5 N/5 N/5 bằng 0.

    Làm thế nào để bạn tìm thấy nếu một số là nhiều trong Python?

    Sử dụng toán tử modulo % để kiểm tra xem một số là bội số của 10, ví dụ: Nếu 100 % 10 == 0:. Toán tử modulo % trả về phần còn lại từ bộ phận của số thứ nhất cho số thứ hai. Nếu phần còn lại là 0, số là bội số của 10. Đã sao chép!, e.g. if 100 % 10 == 0: . The modulo % operator returns the remainder from the division of the first number by the second. If the remainder is 0 , the number is a multiple of 10 . Copied!

    Làm thế nào để bạn tìm thấy bội số của 3 và 5 trong Python?

    Giả sử chúng ta có một số n ...
    Nếu số được chia cho 3 và 5 cả hai, hãy đặt Fizzbuzz.
    Nếu không, khi số chia hết cho 3, hãy đặt Fizz Fizz.
    Nếu không, khi số chia hết cho 5, hãy đặt Buzz Buzz.
    Nếu không, hãy viết số dưới dạng chuỗi ..

    Làm thế nào để bạn kiểm tra bội số của 5 trong Python?

    Sử dụng toán tử mô đun: If (num % 5 == 0) // Số là bội số của 5. other // Số không phải là bội số của 5. Lưu câu trả lời này.if (num % 5 == 0) //the number is a multiple of 5. else // the number is not a multiple of 5. Save this answer.