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
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 9Cho 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/211
using
namespace
std;
int
countMultiples[
int
count = n/3 + n/7 - n/210
count = n/3 + n/7 - n/212
int
count = n/3 + n/7 - n/214
isinstance
3isinstance
4
count = n/3 + n/7 - n/212
count = n/3 + n/7 - n/216
count = n/3 + n/7 - n/217
int
count = n/3 + n/7 - n/219
isinstance
8
isinstance
0isinstance
1 isinstance
2
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
isinstance
6 isinstance
7isinstance
8
int
#include
0
#include
2#include
3#include
4 #include
5
Java
count = n/3 + n/7 - n/211
#include
7 #include
8
count = n/3 + n/7 - n/211
#include
9 using
0
using
2 int
countMultiples[
int
count = n/3 + n/7 - n/210
count = n/3 + n/7 - n/212
int
namespace
0namespace
1namespace
2int
1isinstance
4
count = n/3 + n/7 - n/212
count = n/3 + n/7 - n/216
count = n/3 + n/7 - n/217
int
count = n/3 + n/7 - n/219
isinstance
8
isinstance
0isinstance
1 isinstance
2
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
countMultiples[
3#include
4countMultiples[
5count = n/3 + n/7 - n/212
countMultiples[
7countMultiples[
8countMultiples[
9isinstance
8
isinstance
8
Python3
count = n/3 + n/7 - n/212
isinstance
6 isinstance
7int
#include
0
#include
2#include
3#include
4 #include
5
Java
#include
7 #include
8
count = n/3 + n/7 - n/212
count = n/3 + n/7 - n/216
count = n/3 + n/7 - n/217
int
count = n/3 + n/7 - n/219
count = n/3 + n/7 - n/2135
count = n/3 + n/7 - n/217
count = n/3 + n/7 - n/2137
count = n/3 + n/7 - n/2138
countMultiples[
8countMultiples[
9C#
isinstance
0isinstance
1 isinstance
2
Java
count = n/3 + n/7 - n/211
#include
7 #include
8
count = n/3 + n/7 - n/211
using
namespace
std;
int
countMultiples[
int
count = n/3 + n/7 - n/210
count = n/3 + n/7 - n/212
int
count = n/3 + n/7 - n/214
int
1isinstance
4
count = n/3 + n/7 - n/212
count = n/3 + n/7 - n/216
count = n/3 + n/7 - n/217
int
count = n/3 + n/7 - n/219
isinstance
8
isinstance
0isinstance
1 isinstance
2
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
count = n/3 + n/7 - n/2175
#include
4countMultiples[
5count = n/3 + n/7 - n/212
count = n/3 + n/7 - n/2179
isinstance
8
isinstance
8
count = n/3 + n/7 - n/21
2isinstance
6 isinstance
7
count = n/3 + n/7 - n/2182
int
#include
0
count = n/3 + n/7 - n/211
#include
2#include
3#include
4 #include
5
Java
#include
7 #include
8
int
1
count = n/3 + n/7 - n/2189
isinstance
11count = n/3 + n/7 - n/212
isinstance
6 count = n/3 + n/7 - n/2189
namespace
2isinstance
8
isinstance
17 #include
4 isinstance
19
isinstance
20
JavaScript
isinstance
21
count = n/3 + n/7 - n/2183
isinstance
23count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
isinstance
26count = n/3 + n/7 - n/212
count = n/3 + n/7 - n/216
isinstance
29count = n/3 + n/7 - n/212
isinstance
1 count = n/3 + n/7 - n/2162
int
1isinstance
4
count = n/3 + n/7 - n/212
isinstance
6 isinstance
7isinstance
8
isinstance
39#include
4 isinstance
41
isinstance
42
Độ 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/210
count = n/3 + n/7 - n/211
#include
2isinstance
6 isinstance
54
isinstance
8
int
#include
0
count = n/3 + n/7 - n/211
#include
2#include
3#include
4 #include
5
isinstance
8
Java
#include
7 #include
8
#include
9 using
0
count = n/3 + n/7 - n/211
using
2 int
countMultiples[
int
count = n/3 + n/7 - n/210
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
isinstance
6 isinstance
77std;
3 isinstance
79std;
7isinstance
8
int
7 using
2 int
9 isinstance
88
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
isinstance
91#include
4 count = n/3 + n/7 - n/2107
isinstance
94countMultiples[
countMultiples[
8countMultiples[
9
isinstance
8
isinstance
8
Python 3
int
2 int
3
count = n/3 + n/7 - n/212
isinstance
6 #include
04#include
05 std;
3 count = n/3 + n/7 - n/2107
#include
04__#include
04int
6 #include
18int
#include
20countMultiples[
8#include
22
count = n/3 + n/7 - n/2135
count = n/3 + n/7 - n/217
count = n/3 + n/7 - n/2137
#include
26C#
using
count = n/3 + n/7 - n/2142
#include
9 using
0
count = n/3 + n/7 - n/211
using
2 int
countMultiples[
int
count = n/3 + n/7 - n/210
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
isinstance
6 isinstance
77std;
3 isinstance
79std;
7isinstance
8
int
7 using
2 int
9 isinstance
88
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
isinstance
91#include
4 count = n/3 + n/7 - n/2107
#include
51#include
52
isinstance
8
isinstance
8
Python 3
count = n/3 + n/7 - n/2182
int
2 int
3
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
isinstance
6 #include
04#include
05 std;
3 count = n/3 + n/7 - n/2107
#include
04__isinstance
8
#include
04int
6 #include
18int
#include
20countMultiples[
8#include
22
isinstance
20
JavaScript
isinstance
21
count = n/3 + n/7 - n/2183
isinstance
23count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/212
count = n/3 + n/7 - n/216
isinstance
29isinstance
8
isinstance
39#include
4 isinstance
41
isinstance
42
Độ 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.