Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Với một số nguyên ’n, nhiệm vụ là tìm tổng số các số nguyên tố đầu tiên. & Nbsp;
Input: N = 4 Output: 17 2, 3, 5, 7 are first 4 prime numbers so their sum is equal to 17 Input: N = 40 Output: 3087
Approach:
- Một vài số nguyên tố đầu tiên là: 2, 3, 5, 7, 11, 13, 17, 19, 23,
- Ví dụ: & nbsp; & nbsp;
- Tạo một cái sàng sẽ giúp chúng tôi xác định xem số đó có phải là nguyên tố hay không trong thời gian O (1).
- Chạy một vòng bắt đầu từ 1 cho đến và trừ khi chúng ta tìm thấy n số nguyên tố.
Thêm tất cả các số nguyên tố và bỏ bê những số không phải là nguyên tố.
Tóm Tắt
C++
#include <bits/stdc++.h>
Sau đó, hiển thị tổng số số 1 của N.
#define MAX 10000
Dưới đây là việc thực hiện giải pháp trên & NBSP; & NBSP;
using
namespace
std;
Sum of 1st N prime numbers are :17
1
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
3
Sum of 1st N prime numbers are :17
4
Sum of 1st N prime numbers are :17
5
Sum of 1st N prime numbers are :17
6
Sum of 1st N prime numbers are :17
7
Sum of 1st N prime numbers are :17
8
Sum of 1st N prime numbers are :17
2#include <bits/stdc++.h>
0#include <bits/stdc++.h>
1#include <bits/stdc++.h>
2
bool
prime[MAX + 1];
void
Sum of 1st N prime numbers are :17
0
Sum of 1st N prime numbers are :17
2#include <bits/stdc++.h>
4 #include <bits/stdc++.h>
5#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
using
8using
9#include <bits/stdc++.h>
1#include <bits/stdc++.h>
2
#include <bits/stdc++.h>
8namespace
3
Sum of 1st N prime numbers are :17
2namespace
3
namespace
3
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
9 using
0
Sum of 1st N prime numbers are :17
5using
2
using
3#include <bits/stdc++.h>
4 #include <bits/stdc++.h>
5#include <bits/stdc++.h>
6 using
7
#include <bits/stdc++.h>
6 namespace
8#include <bits/stdc++.h>
6 std;
0
Sum of 1st N prime numbers are :17
1
Sum of 1st N prime numbers are :17
2#include <bits/stdc++.h>
6 std;
4
Sum of 1st N prime numbers are :17
2std;
6 std;
6 #include <bits/stdc++.h>
6 std;
9
using
3#define MAX 10000
7
using
3#define MAX 10000
9
#include <bits/stdc++.h>
8namespace
3
#include <bits/stdc++.h>
8bool
3
Sum of 1st N prime numbers are :17
2namespace
3
Sum of 1st N prime numbers are :17
2#define MAX 10000
1 #define MAX 10000
2
namespace
3
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
9 #define MAX 10000
5
Sum of 1st N prime numbers are :17
1
Sum of 1st N prime numbers are :17
2prime[MAX + 1];
4
Sum of 1st N prime numbers are :17
2bool
7 bool
8
#include <bits/stdc++.h>
6 prime[MAX + 1];
1
Sum of 1st N prime numbers are :17
2#include <bits/stdc++.h>
6 prime[MAX + 1];
7
namespace
3
Sum of 1st N prime numbers are :172prime[MAX + 1];9void0 void1
Sum of 1st N prime numbers are :17
2bool
7 void
4
Java
void
6 void
7 void
8
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
00
Sum of 1st N prime numbers are :17
01
Sum of 1st N prime numbers are :17
02
Sum of 1st N prime numbers are :17
03
Sum of 1st N prime numbers are :17
04 #include <bits/stdc++.h>
2
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
1
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
01
Sum of 1st N prime numbers are :17
08
Sum of 1st N prime numbers are :17
09
Sum of 1st N prime numbers are :17
10
Sum of 1st N prime numbers are :17
08
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
01 void
Sum of 1st N prime numbers are :17
0
#include <bits/stdc++.h>
8
Sum of 1st N prime numbers are :17
35
Sum of 1st N prime numbers are :17
36
Sum of 1st N prime numbers are :17
37#include <bits/stdc++.h>
1#include <bits/stdc++.h>
2
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
4#include <bits/stdc++.h>
5#include <bits/stdc++.h>
6
Sum of 1st N prime numbers are :17
27
Sum of 1st N prime numbers are :17
28
Sum of 1st N prime numbers are :17
29
using
3using
9
Sum of 1st N prime numbers are :17
5 #include <bits/stdc++.h>
2
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
4 #include <bits/stdc++.h>
5#include <bits/stdc++.h>
6
Sum of 1st N prime numbers are :17
44
Sum of 1st N prime numbers are :17
45
Sum of 1st N prime numbers are :17
46
Sum of 1st N prime numbers are :17
59using
9#include <bits/stdc++.h>
1#include <bits/stdc++.h>
2
using
3namespace
3
#include <bits/stdc++.h>
8namespace
3
Sum of 1st N prime numbers are :17
2namespace
3
using
3#include <bits/stdc++.h>
9 using
0
Sum of 1st N prime numbers are :17
5using
2
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
1
using
8#include <bits/stdc++.h>
4 #include <bits/stdc++.h>
5#include <bits/stdc++.h>
6
Sum of 1st N prime numbers are :17
56
Sum of 1st N prime numbers are :17
45
Sum of 1st N prime numbers are :17
58
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
01 #include <bits/stdc++.h>
6 namespace
8#include <bits/stdc++.h>
6 std;
0
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
6
Sum of 1st N prime numbers are :17
79
Sum of 1st N prime numbers are :17
28
Sum of 1st N prime numbers are :17
81
Sum of 1st N prime numbers are :17
36#include <bits/stdc++.h>
2
#include <bits/stdc++.h>
8std;
6
Sum of 1st N prime numbers are :17
86
Sum of 1st N prime numbers are :17
28#include <bits/stdc++.h>
2
using
8#define MAX 10000
7
using
8#define MAX 10000
9
using
3namespace
3
using
3bool
3
#include <bits/stdc++.h>
8namespace
3
#include <bits/stdc++.h>
8#define MAX 10000
1 #define MAX 10000
2
Sum of 1st N prime numbers are :17
2namespace
3
using
3#include <bits/stdc++.h>
9 #define MAX 10000
5
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
1
#include <bits/stdc++.h>
8prime[MAX + 1];
4
#include <bits/stdc++.h>
8bool
7 #include <bits/stdc++.h>
5#include <bits/stdc++.h>
6#include <bits/stdc++.h>
09
Sum of 1st N prime numbers are :17
2void
6
Sum of 1st N prime numbers are :17
01 void
#include <bits/stdc++.h>
16
Sum of 1st N prime numbers are :17
2namespace
3
namespace
3
#include <bits/stdc++.h>8#include <bits/stdc++.h>6 #include <bits/stdc++.h>23#include <bits/stdc++.h>24#include <bits/stdc++.h>2
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
27void
0 #include <bits/stdc++.h>
29
Python 3
#include <bits/stdc++.h>
33 #include <bits/stdc++.h>
34
Sum of 1st N prime numbers are :17
04
#include <bits/stdc++.h>
36#include <bits/stdc++.h>
34 #include <bits/stdc++.h>
38#include <bits/stdc++.h>
39 #include <bits/stdc++.h>
4 #include <bits/stdc++.h>
41__
#include <bits/stdc++.h>
49 #include <bits/stdc++.h>
50
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
35
Sum of 1st N prime numbers are :17
36#include <bits/stdc++.h>
54#include <bits/stdc++.h>
34 #include <bits/stdc++.h>
56
Sum of 1st N prime numbers are :17
2#include <bits/stdc++.h>
4 #include <bits/stdc++.h>
59#include <bits/stdc++.h>
42 #include <bits/stdc++.h>
43__
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
9 #include <bits/stdc++.h>
71#include <bits/stdc++.h>
34#include <bits/stdc++.h>
34 #include <bits/stdc++.h>
39#include <bits/stdc++.h>
68
using
3#include <bits/stdc++.h>
41#include <bits/stdc++.h>
34 #include <bits/stdc++.h>
59#include <bits/stdc++.h>
80
Sum of 1st N prime numbers are :17
45
using
3#define MAX 10000
1#include <bits/stdc++.h>
84#include <bits/stdc++.h>
34 #include <bits/stdc++.h>
33#include <bits/stdc++.h>
68
using
8#include <bits/stdc++.h>
89#include <bits/stdc++.h>
34 #include <bits/stdc++.h>
56
using
8#include <bits/stdc++.h>
41#include <bits/stdc++.h>
34 #include <bits/stdc++.h>
41#include <bits/stdc++.h>
46 #include <bits/stdc++.h>
97
#include <bits/stdc++.h>
49 #include <bits/stdc++.h>
99
Sum of 1st N prime numbers are :17
2using
01#include <bits/stdc++.h>
34
Sum of 1st N prime numbers are :17
28
Sum of 1st N prime numbers are :17
2using
05#include <bits/stdc++.h>
34
Sum of 1st N prime numbers are :17
36
Sum of 1st N prime numbers are :17
2using
09#include <bits/stdc++.h>
34
Sum of 1st N prime numbers are :17
28
Sum of 1st N prime numbers are :17
2#define MAX 10000
1 using
14
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
9 using
17
using
3using
09#include <bits/stdc++.h>
34 using
09#include <bits/stdc++.h>
46 using
23
using
3using
01#include <bits/stdc++.h>
34 using
01#include <bits/stdc++.h>
46
Sum of 1st N prime numbers are :17
36
Sum of 1st N prime numbers are :17
0
#include <bits/stdc++.h>
8using
05#include <bits/stdc++.h>
34 using
05#include <bits/stdc++.h>
46
Sum of 1st N prime numbers are :17
36
Sum of 1st N prime numbers are :17
2bool
7 using
38
using
47using
48using
49
C#
using
40#include <bits/stdc++.h>
34 #include <bits/stdc++.h>
24
using
43#include <bits/stdc++.h>
5using
45 #include <bits/stdc++.h>
46
using
using
51
void
6 void
7 using
54
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
00
Sum of 1st N prime numbers are :17
01
Sum of 1st N prime numbers are :17
02
Sum of 1st N prime numbers are :17
03
Sum of 1st N prime numbers are :17
04 #include <bits/stdc++.h>
2
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
1
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
4#include <bits/stdc++.h>
5#include <bits/stdc++.h>
6 using
78
using
3using
9
Sum of 1st N prime numbers are :17
5 #include <bits/stdc++.h>
2
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
0#include <bits/stdc++.h>
1#include <bits/stdc++.h>
2
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
4 #include <bits/stdc++.h>
5#include <bits/stdc++.h>
6 #include <bits/stdc++.h>
7
using
3#include <bits/stdc++.h>
9 using
0
Sum of 1st N prime numbers are :17
5using
2
using
8#include <bits/stdc++.h>
4 #include <bits/stdc++.h>
5#include <bits/stdc++.h>
6 using
7
Sum of 1st N prime numbers are :17
59using
9#include <bits/stdc++.h>
1#include <bits/stdc++.h>
2
using
3namespace
3
#include <bits/stdc++.h>
8namespace
3
Sum of 1st N prime numbers are :17
2namespace
3
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
01 #include <bits/stdc++.h>
6 namespace
8#include <bits/stdc++.h>
6 std;
0
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
1
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
6 std;
4
#include <bits/stdc++.h>
8std;
6 std;
9
#include <bits/stdc++.h>
8#define MAX 10000
1 #define MAX 10000
2
using
3#include <bits/stdc++.h>
9 #define MAX 10000
5
using
8#define MAX 10000
7
using
8#define MAX 10000
9
using
3namespace
3
using
3bool
3
#include <bits/stdc++.h>
8namespace
3
#include <bits/stdc++.h>
8bool
7 #include <bits/stdc++.h>
5#include <bits/stdc++.h>
6#include <bits/stdc++.h>
09
Sum of 1st N prime numbers are :17
2namespace
3
Sum of 1st N prime numbers are :17
2
Sum of 1st N prime numbers are :17
01 void
6 void
namespace
53
#include <bits/stdc++.h>
8prime[MAX + 1];
4
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
6 prime[MAX + 1];
7
#include <bits/stdc++.h>
8namespace
60void
0 #include <bits/stdc++.h>
29
Sum of 1st N prime numbers are :17
2namespace
3
namespace
3
JavaScript
namespace
66
namespace
67 using
58
namespace
67 namespace
70#include <bits/stdc++.h>
1namespace
72
namespace
73
Sum of 1st N prime numbers are :17
0
Sum of 1st N prime numbers are :17
1
Sum of 1st N prime numbers are :17
2#include <bits/stdc++.h>
4namespace
78
#include <bits/stdc++.h>
8using
9
Sum of 1st N prime numbers are :17
5 #include <bits/stdc++.h>
2
Sum of 1st N prime numbers are :17
2#include <bits/stdc++.h>
0#include <bits/stdc++.h>
1#include <bits/stdc++.h>
2
Sum of 1st N prime numbers are :17
2#include <bits/stdc++.h>
4 namespace
89
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
9 using
0
Sum of 1st N prime numbers are :17
5using
2
using
3#include <bits/stdc++.h>
4 namespace
97
using
8using
9#include <bits/stdc++.h>
1#include <bits/stdc++.h>
2
#include <bits/stdc++.h>
8namespace
3
Sum of 1st N prime numbers are :17
2namespace
3
namespace
3
namespace
73 std;
08
Sum of 1st N prime numbers are :17
1
Sum of 1st N prime numbers are :17
2namespace
67 std;
4
Sum of 1st N prime numbers are :17
2namespace
67 std;
9
Sum of 1st N prime numbers are :17
2#define MAX 10000
1 #define MAX 10000
2
#include <bits/stdc++.h>
8#include <bits/stdc++.h>
9 #define MAX 10000
5
using
3#define MAX 10000
7
using
3#define MAX 10000
9
#include <bits/stdc++.h>
8namespace
3
#include <bits/stdc++.h>
8bool
3
Sum of 1st N prime numbers are :17
2namespace
3
Sum of 1st N prime numbers are :17
2bool
7 std;
34
namespace
3
prime[MAX + 1];
4
namespace
67 prime[MAX + 1];
7
std;
39void
0 #include <bits/stdc++.h>
29
std;
42
Output:
Sum of 1st N prime numbers are :17
Lưu ý (đối với lập trình cạnh tranh): Trong một vấn đề chứa một số lượng lớn các truy vấn, một vectơ có thể được sử dụng để lưu trữ tất cả các số nguyên tố trong phạm vi 10^8, điều này sẽ có thêm không gian O (n). Chúng ta cũng có thể sử dụng mảng tiền tố để lưu trữ tổng số n số đầu tiên trong phạm vi 10^8. & nbsp; In a
problem which contains a large number of queries, a vector can be used to store all the prime numbers in the range of 10^8, this will take extra O(N) space. We can also use prefix array to store the sum of first N prime numbers in the range of 10^8.