Hướng dẫn sum of prime numbers from 1 to n in python - tổng các số nguyên tố từ 1 đến n trong python

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
  • Bàn luận
  • 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
    28
    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

    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
    28
    2
    28
    3int
    28
    5

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    6Find 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
    9

    28
    6#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.
    3#include 1

    28
    6
    28
    7
    28
    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.
    9Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    0

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1

    28
    2
    28
    3int Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    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.
    3int #include 6

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1using6

    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
    28
    2
    28
    3int using1

    #include 1

    28
    6
    28
    7 using4

    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.
    3using8 using9

    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.
    3namespace8

    int namespace2

    #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 namespace6

    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.
    3using8 std;1

    Java

    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

    28
    6sumOfPrimes(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.
    9sumOfPrimes(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.
    3int0 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

    28
    6int8 int9sumOfPrimes(0 int8sumOfPrimes(2Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    sumOfPrimes(4

    28
    6
    28
    2
    28
    3int
    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.
    18Find 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
    9

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1#include 1

    28
    6#include 1

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1

    28
    7
    28
    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.
    9Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    0

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    6

    28
    2
    28
    3int
    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
    Sum of all prime numbers upto 50 : 326
    1

    28
    7 using4

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    6using6

    28
    6using8 using9

    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

    Độ 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;

    28
    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.
    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
    28
    2
    28
    3int
    28
    5

    28
    6
    28
    7
    28
    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.
    9Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    0

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1

    28
    2
    28
    3int Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    5

    28
    6
    28
    7
    28
    38

    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 #include 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
    28
    2
    28
    3int using1

    28
    6
    28
    7 using4

    28
    50
    28
    51

    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.
    3using8 using9

    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

    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.
    3int0 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

    28
    6Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    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.
    9Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    9

    28
    6int8 int9sumOfPrimes(0 int8sumOfPrimes(2Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    sumOfPrimes(4

    28
    6
    28
    2
    28
    3int
    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
    Sum of all prime numbers upto 50 : 326
    1

    28
    7
    28
    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
    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
    Sum of all prime numbers upto 50 : 326
    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.
    2

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1

    28
    7
    28
    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.
    9Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    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.
    18Find 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
    9

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1#include 1

    28
    6#include 1

    28
    6int #include 6

    28
    6
    28
    2
    28
    3int using1

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1

    28
    7 using4

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    6using6

    28
    6using8 using9

    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

    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.
    49 int0
    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
    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.
    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

    28
    6int namespace6

    28
    6Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    35

    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

    PHP

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    39

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    40 sumOfPrimes(Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    42

    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.
    3Find 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
    51

    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
    28
    2
    28
    3Find 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
    56

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    57Find 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
    59Find 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
    61Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    42Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    63Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    555Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    65

    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

    28
    6
    28
    7
    28
    3Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    46
    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.
    70Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    555____374

    28
    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.
    2

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1

    28
    2
    28
    3Find 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
    83

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    84___

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    6Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    46

    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.
    70Find 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
    97

    28
    6#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.
    3#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.
    3#include 03 #include 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.
    3
    28
    2
    28
    3Find 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
    80

    28
    6
    28
    7
    28
    3Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    46
    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.
    70Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    80#include 22

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1#include 03 Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    90Find 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
    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.
    3using8 #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
    Sum of all prime numbers upto 50 : 326
    42 #include 34

    #include 35 sumOfPrimes(Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    42sumOfPrimes(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.
    3Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    40 #include 43

    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

    28
    6#include 47sumOfPrimes(0 #include 49

    28
    6
    28
    2#include 52

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    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.
    9Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    9

    28
    6
    28
    2 #include 59

    28
    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.
    2

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1

    28
    7
    28
    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
    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
    Sum of all prime numbers upto 50 : 326
    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.
    2

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    6

    28
    2 #include 71

    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.
    18Find 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
    9

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1#include 1

    28
    6#include 1

    28
    6#include 81

    28
    6
    28
    2 #include 84

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    1

    28
    7 using4

    Find sum of prime numbers upto : 50
    Sum of all prime numbers upto 50 : 326
    6using6

    28
    6using8 using9

    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

    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 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.
    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 int0
    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
    28
    O(nloglogn)

    28
    6int namespace6 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.