range:
Phạm vi [Bắt đầu, Dừng [, Bước]]
Đây là một hàm đa năng để tạo danh sách có chứa các tiến trình số học. Nó thường được sử dụng trong các vòng lặp. Các lập luận phải là số nguyên đơn giản. Nếu đối số bước bị bỏ qua, nó mặc định là 1. Nếu đối số bắt đầu bị bỏ qua, nó mặc định là 0. Hình thức đầy đủ trả về danh sách các số nguyên đơn giản [Bắt đầu, Bắt đầu + Bước, Bắt đầu + 2 * Bước, ...] . Nếu bước là dương, phần tử cuối cùng là bước khởi động lớn nhất + i * nhỏ hơn điểm dừng; Nếu bước là âm, phần tử cuối cùng là khởi động nhỏ nhất + i * bước lớn hơn điểm dừng. Bước không được bằng không [hoặc giá trị khác được nâng lên].
Mã của bạn sẽ như thế này:
for x in range[0, 100]:
if x % 3 == 0:
print "YES"
else:
print x
examples:
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
đây là những gì bạn cần:
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
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;
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2using
namespace
std;
int
countMultiples[
int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
1>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
5>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
4>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
5>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
7 >>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
8int
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
0>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
1>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
2 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
8>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9int
count = n/3 + n/7 - n/21
1
count = n/3 + n/7 - n/213
count = n/3 + n/7 - n/214
count = n/3 + n/7 - n/215
count = n/3 + n/7 - n/216
Java
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2count = n/3 + n/7 - n/218
count = n/3 + n/7 - n/219
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2#include
0 #include
1
#include
3 int
countMultiples[
int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
1>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3int
using
1using
22std;
2
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
5>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
7 >>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
8int
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
0>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
1>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
2 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3int
4count = n/3 + n/7 - n/215
int
6>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3int
8int
9countMultiples[
0>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9Python3
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
8int
count = n/3 + n/7 - n/211
count = n/3 + n/7 - n/213
count = n/3 + n/7 - n/214
count = n/3 + n/7 - n/215
count = n/3 + n/7 - n/216
Java
count = n/3 + n/7 - n/218
count = n/3 + n/7 - n/219
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
7 >>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
8int
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
0>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
36>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
8>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
38>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
39int
9countMultiples[
0C#
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
1>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
2 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
3Java
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2count = n/3 + n/7 - n/218
count = n/3 + n/7 - n/219
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2using
namespace
std;
int
countMultiples[
int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
1>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
5std;
2
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
5>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
7 >>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
8int
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
0>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
1>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
2 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
76count = n/3 + n/7 - n/215
int
6>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
80>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
8
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
83int
count = n/3 + n/7 - n/211
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2count = n/3 + n/7 - n/213
count = n/3 + n/7 - n/214
count = n/3 + n/7 - n/215
count = n/3 + n/7 - n/216
Java
count = n/3 + n/7 - n/218
count = n/3 + n/7 - n/219
std;
2
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
90>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
12#include
0 #include
1
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9#include
3 int
countMultiples[
int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
1>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
21>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3int
using
1using
22
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
7 >>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
8int
using
8using
9namespace
0>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
2 namespace
3namespace
4 namespace
5using
2 namespace
7namespace
8>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
27std;
8 #include
3 int
0 int
1
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
5std;
2
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
5>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
7 >>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
8int
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
0>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
40count = n/3 + n/7 - n/215
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
42>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
43Độ 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
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
1>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2count = n/3 + n/7 - n/213
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
55>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9int
count = n/3 + n/7 - n/211
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2count = n/3 + n/7 - n/213
count = n/3 + n/7 - n/214
count = n/3 + n/7 - n/215
count = n/3 + n/7 - n/216
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9Java
count = n/3 + n/7 - n/218
count = n/3 + n/7 - n/219
#include
0 #include
1
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2#include
3 int
countMultiples[
int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
1>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
78namespace
4 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
80namespace
8>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9std;
8 #include
3 int
0
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
89>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
92count = n/3 + n/7 - n/215
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
08>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
95countMultiples[
int
9countMultiples[
0>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9Python 3
countMultiples[
3 countMultiples[
4
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 count = n/3 + n/7 - n/2105
count = n/3 + n/7 - n/2106
namespace
4 >>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
08 count = n/3 + n/7 - n/2105__
count = n/3 + n/7 - n/2105
countMultiples[
7 count = n/3 + n/7 - n/2119
int
count = n/3 + n/7 - n/2121
int
9count = n/3 + n/7 - n/2123
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
36>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
8>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
38count = n/3 + n/7 - n/2127
C#
using
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
43#include
0 #include
1
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2#include
3 int
countMultiples[
int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
1>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
78namespace
4 >>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
80namespace
8>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9std;
8 #include
3 int
0
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
89>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
92count = n/3 + n/7 - n/215
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
08count = n/3 + n/7 - n/2152
count = n/3 + n/7 - n/2153
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9Python 3
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
83countMultiples[
3 countMultiples[
4
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 count = n/3 + n/7 - n/2105
count = n/3 + n/7 - n/2106
namespace
4 >>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
08 count = n/3 + n/7 - n/2105__
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9count = n/3 + n/7 - n/2105
countMultiples[
7 count = n/3 + n/7 - n/2119
int
count = n/3 + n/7 - n/2121
int
9count = n/3 + n/7 - n/2123
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
21using
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
43
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
22#include
3 int
countMultiples[
int
>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
1>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
2>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
3>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
7 count = n/3 + n/7 - n/2141
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
9>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
40count = n/3 + n/7 - n/215
>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
42>>> def myTest[]:
... for x in range[100]:
... if x%3==0 and x%5==0:
... print "Both"
... elif x%3==0:
... print "Three"
... elif x%5==0:
... print "Five"
...
43#include
3 std;
8 int
0
73O[1]>>> range[0,10] // without step
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range[0,10,3] // with step
^
step
[0, 3, 6, 9]
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.