Sum of prime numbers in python

Prime number

A prime number is an integer greater than 1 whose only factors are 1 and itself. A factor is an integer that can be divided evenly into another number.

Logic

To print the sum of all prime numbers up to N we have to iterate through each number up to the given number and check if the number is a prime or not if it is a prime number then simply sum it or add it in one temporary variable.

Once the outer loop is completed we have to print that temporary variable containing the sum of primes.

See also: Check whether a number is prime number or not

Program

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

Output

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

Solution 1: Sum of n Prime Numbers in Python

n = int(input())
list = []
sum = 0
for i in range(2, n+1):
    for j in range(2, i):
        if i % j == 0:
           break
    else:
        sum = sum + i
print(sum)

Solution 2: Sum of Prime Numbers in the Input in Python

def sumOfPrimes(n): 
    prime = [True] * (n + 1) 
      
    p = 2
    while p * p <= n: 
        if prime[p] == True: 
            i = p * 2
            while i <= n: 
                prime[i] = False
                i += p 
        p += 1    
           
    sum = 0
    for i in range (2, n + 1): 
        if(prime[i]): 
            sum += i 
    return sum

print(sumOfPrimes(int(input())))

Solution 3: Sum of Prime Numbers in the input Python

n = int(input())

def is_prime(num):
    for i in range(2, num):
        if num % i == 0:
            return False
    return True

sum = 0
for i in range(2, n+1):
    if(is_prime(i)):
        sum = sum + i
print(sum)

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Write a program to find sum of all prime numbers between 1 to n.
    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.

    A simple 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

    using namespace std;

    int sumOfPrimes(int n)

    {

        bool prime[n + 1];

        memset(prime, true, n + 1);

        for (int p = 2; p * p <= n; p++) {

            if (prime[p] == true) {

                for (int i = p * 2; i <= n; i += p)

                    prime[i] = false;

            }

        }

        int sum = 0;

        for (int i = 2; i <= n; i++)

            if (prime[i])

                sum += i;

        return sum;

    }

    int main()

    {

        int n = 11;

        cout << sumOfPrimes(n);

        return 0;

    }

    Java

    import java.io.*;

    import java.util.*;

    class GFG {

        static int sumOfPrimes(int n)

        {

            boolean prime[]=new boolean[n + 1];

            Arrays.fill(prime, true);

            for (int p = 2; p * p <= n; p++) {

                if (prime[p] == true) {

                    for (int i = p * 2; i <= n; i += p)

                        prime[i] = false;

                }

            }

            int sum = 0;

            for (int i = 2; i <= n; i++)

                if (prime[i])

                    sum += i;

            return sum;

        }

        public static void main(String args[])

        {

            int n = 11;

            System.out.print(sumOfPrimes(n));

        }

    }

    Python3

    def sumOfPrimes(n):

        prime = [True] * (n + 1)

        p = 2

        while p * p <= n:

            if prime[p] == True:

                i = p * 2

                while i <= n:

                    prime[i] = False

                    i += p

            p += 1   

        sum = 0

        for i in range (2, n + 1):

            if(prime[i]):

                sum += i

        return sum

    n = 11

    print(sumOfPrimes(n))

    C#

    using System;

    class GFG {

        static int sumOfPrimes(int n)

        {

            bool []prime=new bool[n + 1];

            for(int i = 0; i < n + 1; i++)

            prime[i] = true;

            for (int p = 2; p * p <= n; p++)

            {

                if (prime[p] == true)

                {

                    for (int i = p * 2; i <= n; i += p)

                        prime[i] = false;

                }

            }

            int sum = 0;

            for (int i = 2; i <= n; i++)

                if (prime[i])

                    sum += i;

            return sum;

        }

        public static void Main()

        {

            int n = 11;

            Console.Write(sumOfPrimes(n));

        }

    }

    PHP

    function sumOfPrimes($n)

    {

        $prime = array_fill(0, $n + 1, true);

        for ($p = 2;

             $p * $p <= $n; $p++)

        {

            if ($prime[$p] == true)

            {

                for ($i = $p * 2;

                     $i <= $n; $i += $p)

                    $prime[$i] = false;

            }

        }

        $sum = 0;

        for ($i = 2; $i <= $n; $i++)

            if ($prime[$i])

                $sum += $i;

        return $sum;

    }

    $n = 11;

    echo sumOfPrimes($n);

    ?>

    Javascript

    Output: 
     

    28

    Time Complexity: O(nloglogn)

    Auxiliary Space: 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.
     


    What is the formula of sum of prime numbers?

    How to find the sum of prime numbers up to a prime number n, that is for example: the sum of prime numbers up to 7 is: 2+3+5+7=17.

    How do you find the sum of all prime numbers between 1 and n?

    A simple 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.

    What is the sum of prime numbers from 1 to 100?

    So the sum of all the good prime numbers between 1 and 100 is 29+89=118 29 + 89 = 118 .

    What is the formula for prime numbers in Python?

    Enter an integer in the input below to find if it is a prime number or not. x_int = int(x) factors = [] if x_int <= 1: print(f"{x} is not a prime number") else: for factor in range(2, x_int): if x_int % factor == 0: factors.