Find the sum of all prime numbers in Python

Here, we will figure out how to discover the whole of every prime number till 1000 in Python programming language?

To carry out this responsibility, we will utilize the Sieve of Eratosthenes which is one of the most well-known calculations of Python language which is utilized to discover prime numbers. No compelling reason to stress over it that one thousand is the huge number and how we will locate the all Prime number short of what one thousand. Along these lines, before going to take care of this issue in the most straightforward manner we will gain proficiency with a tad about what is Sieve of Eratosthenes and it’s calculation how relevant in our assignment.

Sifter of Eratosthenes and its calculation

It is a basic and old technique for discovering every single Prime number not exactly or equivalent to N and here the estimation of N is one thousand.

The calculation to discover the aggregate of Prime numbers not exactly or equivalent to one thousand by Sieve of Eratosthenes,

We make a boolean cluster of a size equivalent to the given number (N) and imprint each position in the exhibit True.

We instate a variable p equivalent to 2 and s equivalent to 0.

On the off chance that the variable p is prime, at that point mark each numerous of number False in the cluster.

Update the variable p by an augmentation of 1 i.e p = p+1.

Rehash stage 2 until the square of the variable is not exactly the given number (N).

The components in the exhibit with True contain every Prime number not exactly or equivalent to the given number and the components of the cluster which is our Prime number.

After the above procedure, we will essentially discover the entirety of the prime numbers.

We should begin composing a Python program utilizing the above calculation in a basic manner.

Code:

N=1000
s=0  # variable s will be used to find the sum of all prime.
Primes=[True for k in range(N+1)] 
p=2 
Primes[0]=False # zero is not a prime number.
Primes[1]=False #one is also not a prime number.
while(p*p<=N):
    if Primes[p]==True: 
        for j in range(p*p,N+1,p): 
            Primes[j]=False 
    p+=1 
for i in range(2,N): 
    if Primes[i]: 
        s+=i 
print('The sum of prime numbers:',s)

Output:

The sum of prime numbers: 76127

Leave a Comment

error: Alert: Content is protected!!