số nguyên tố
Một số nguyên tố là một số nguyên lớn hơn 1 mà các yếu tố duy nhất là 1 và chính nó. Một yếu tố là một số nguyên có thể được chia đều thành một số khác.
Hợp lý
Để in tổng của tất cả các số nguyên tố lên đến n, chúng tôi phải lặp qua mỗi số lên đến số đã cho và kiểm tra xem số có phải là số nguyên tố hay không nếu nó là số nguyên tố thì chỉ cần tổng hợp nó hoặc thêm nó vào một biến tạm thời .
Khi vòng lặp bên ngoài được hoàn thành, chúng ta phải in biến tạm thời đó chứa tổng số nguyên tố.
Xem thêm: Kiểm tra xem một số có phải là số nguyên tố hay không
Chương trình
# Take input from user upto = int[input["Find sum of prime numbers upto : "]] sum = 0 for num in range[2, upto + 1]: i = 2 for i in range[2, num]: if [int[num % i] == 0]: i = num break; #If the number is prime then add it. if i is not num: sum += num print["\nSum of all prime numbers upto", upto, ":", sum]
Đầu ra
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
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
Examples:
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.
Bàn luậnsimple solution is to traverse all numbers from 1 to n. For every number,
check if it is a prime. If yes, add it to result.
An efficient solution is to use Sieve of Eratosthenes to find all prime numbers from till n and then do their sum.
C++
#include
Viết một chương trình để tìm tổng của tất cả các số nguyên tố trong khoảng từ 1 đến n.examples: & nbsp; & nbsp;
Một giải pháp đơn giản là đi qua tất cả các số từ 1 đến n. Đối với mỗi số, kiểm tra xem nó có phải là một nguyên tố chính không. Nếu có, hãy thêm nó vào kết quả. Một giải pháp hiệu quả là sử dụng sàng của eratosthenes để tìm tất cả các số nguyên tố từ đến N và sau đó thực hiện tổng của chúng. & NBSP;
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
using
namespace
std;
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.7
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.8
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
280
int
sumOfPrimes[
int
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.1
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.4
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.5
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
282
283
int
285
Find sum of prime numbers upto : 50
6
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
7
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
8
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
9
Sum of all prime numbers upto 50 : 326
286
#include
1Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
1286
287
288
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
0Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
282
283
int
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
5Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
int
#include
6Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326using
6
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
282
283
int
using
1#include
1
286
287
using
4Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
using
8 using
9Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
namespace
8int
namespace
2
#include
1
Input : 10
Output : 17
Explanation : Primes between 1 to 10 : 2, 3, 5, 7.
Input : 11
Output : 28
Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.
3int
namespace
6
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
using
8 std;
1Java
std;
3 std;
4
std;
3 std;
6
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
std;
7 std;
8
286
sumOfPrimes[
6Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
sumOfPrimes[
8Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
int
0 int
sumOfPrimes[
int
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.1
286
int
8 int
9sumOfPrimes[
0 int
8sumOfPrimes[
2Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
sumOfPrimes[
4286
282
283
int
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.03
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.044____105
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.18
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
7Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
8Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
9Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326#include
1
286
#include
1Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
287
288
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
0Find sum of prime numbers upto : 50
6
Sum of all prime numbers upto 50 : 326
282
283
int
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.15
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.04
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.17
Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
287
using
4Find sum of prime numbers upto : 50
6
Sum of all prime numbers upto 50 : 326using
6
286
using
8 using
9Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
1Độ phức tạp về thời gian: O [nloglogn]
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
Không gian phụ trợ: O [n] Bài viết này được đóng góp bởi Rohit Thapliyal. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng cách sử dụng PROPTENT.GeekSforGeeks.org hoặc gửi bài viết của bạn đến. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác. Xin vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên. & NBSP;
286
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.61
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
1#include
1
Python3
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Viết một chương trình để tìm tổng của tất cả các số nguyên tố trong khoảng từ 1 đến n.examples: & nbsp; & nbsp;
Một giải pháp đơn giản là đi qua tất cả các số từ 1 đến n. Đối với mỗi số, kiểm tra xem nó có phải là một nguyên tố chính không. Nếu có, hãy thêm nó vào kết quả. Một giải pháp hiệu quả là sử dụng sàng của eratosthenes để tìm tất cả các số nguyên tố từ đến N và sau đó thực hiện tổng của chúng. & NBSP;
using
namespace
std;
int
sumOfPrimes[
int
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.1
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.4
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.5
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
282
283
int
285
286
287
288
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
0Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
282
283
int
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
5286
287
2838
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
int
#include
6Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
282
283
int
using
1286
287
using
42850
2851
C#
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
using
8 using
9std;
3 std;
4
std;
3 std;
6
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
std;
7 std;
8
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
int
0 int
sumOfPrimes[
int
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.1
286
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
7Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
9286
int
8 int
9sumOfPrimes[
0 int
8sumOfPrimes[
2Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
sumOfPrimes[
4286
282
283
int
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.03
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.044____105
Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
287
288
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.77
Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
287
288
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
0Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.18
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
7Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
8Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
9Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326#include
1
286
#include
1286
int
#include
6286
282
283
int
using
1Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
287
using
4Find sum of prime numbers upto : 50
6
Sum of all prime numbers upto 50 : 326using
6
286
using
8 using
9Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
1Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.49
int
0 Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.51
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
28Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
286
int
namespace
6286
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
35Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
1#include
1
PHP
Find sum of prime numbers upto : 50
39
Sum of all prime numbers upto 50 : 326
Find sum of prime numbers upto : 50
40
Sum of all prime numbers upto 50 : 326sumOfPrimes[
Find sum of prime numbers upto : 50
42
Sum of all prime numbers upto 50 : 326
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.77
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
46 Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
47Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
48Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
49Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
42 Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
51Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
282
283
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
55 Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
56Find sum of prime numbers upto : 50
57
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
55
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
59
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
55
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
61
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
42
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
63
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
555
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
65
Sum of all prime numbers upto 50 : 326
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
286
287
283
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
46Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.70
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
555____374286
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
282
283
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
80 Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
47Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
55 Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
83Find sum of prime numbers upto : 50
84___
Sum of all prime numbers upto 50 : 326
Find sum of prime numbers upto : 50
6
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
46
Sum of all prime numbers upto 50 : 326
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.70
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
80Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
97286
#include
1Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
1Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
03 #include
04Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
282
283
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
80 #include
09Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
80286
287
283
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
46Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.70
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
80#include
22Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326#include
03 Find sum of prime numbers upto : 50
90
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
80
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
9
Sum of all prime numbers upto 50 : 326
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
using
8 #include
03Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
9#include
1
Find sum of prime numbers upto : 50
42
Sum of all prime numbers upto 50 : 326#include
34
#include
35 sumOfPrimes[
Find sum of prime numbers upto : 50
42
Sum of all prime numbers upto 50 : 326sumOfPrimes[
8
#include
39
JavaScript
#include
40
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
40 #include
43Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
286
#include
47sumOfPrimes[
0 #include
49286
282
#include
52Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326Find sum of prime numbers upto : 50
7
Sum of all prime numbers upto 50 : 326
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
9286
282
#include
59286
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
287
288
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.9
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.77
Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.2
Find sum of prime numbers upto : 50
6
Sum of all prime numbers upto 50 : 326
282
#include
71Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.18
Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
7Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
8Find sum of prime numbers upto : 50
Sum of all prime numbers upto 50 : 326
9Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326#include
1
286
#include
1286
#include
81286
282
#include
84Find sum of prime numbers upto : 50
1
Sum of all prime numbers upto 50 : 326
287
using
4Find sum of prime numbers upto : 50
6
Sum of all prime numbers upto 50 : 326using
6
286
using
8 using
9Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
1Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.3
#include
96#include
97#include
98
#include
99
Output:
28
Input : 10
Output : 17
Explanation : Primes between 1 to 10 : 2, 3, 5, 7.
Input : 11
Output : 28
Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.
3Input : 10
Output : 17
Explanation : Primes between 1 to 10 : 2, 3, 5, 7.
Input : 11
Output : 28
Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.
49 int
0 Input : 10
Output : 17
Explanation : Primes between 1 to 10 : 2, 3, 5, 7.
Input : 11
Output : 28
Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.
51 Find sum of prime numbers upto : 50
28O[nloglogn]
Sum of all prime numbers upto 50 : 326
28
6int
namespace
6 O[n]
This article is contributed by Rohit
Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or 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.