Home > Back-end >  How to find the sum of prime numbers upto a certain range in Python?
How to find the sum of prime numbers upto a certain range in Python?

Time:12-22

I'm absolutely new to Python and programming in general so it'd be helpful if someone can give very beginner friendly help. I'm trying to get the sum of prime numbers upto say 50.

I used the below code but it adds additional 2 on my sum. for example sum of prime number upto 50 should be 326 but I get 328 as my output

upto = int(input("Find sum of prime numbers upto : "))

sum = 0

for num in range(2, upto   1):

    
    for i in range(2, num):
        if (int(num % i) == 0):
            break;

    #If the number is prime then add it.
    else:
        sum  = num

print("\nSum of all prime numbers upto", upto, ":", sum)

CodePudding user response:

Your Code is working fine and the answer of " sum of prime numbers from 1 to 50" is 328 not 326, if you still have doubt about it you can check your result with this code below :

Explanation: Given a range [l, r], the task is to find the sum of all the prime numbers within that range.


# from math lib import sqrt method
from math import sqrt

# Function to compute the prime number
def checkPrime(numberToCheck) :

    if numberToCheck == 1 :
        return False

    for i in range(2, int(sqrt(numberToCheck))   1) :

        if numberToCheck % i == 0 :
            return False

    return True

# Function to iterate the loop
# from l to r. If the current
# number is prime, sum the value
def primeSum(l, r) :

    sum = 0

    for i in range(r, (l - 1), -1) :

        # Check for prime
        isPrime = checkPrime(i)
        
        if (isPrime) :

            # Sum the prime number
            sum  = i

    return sum





# Driver code   
if __name__ == "__main__" :

    l, r = 1, 50

    # Call the function with l and r
    print(primeSum(l, r))


I set your range in the code above just copy and paste the code in your editor and run the code , you will see the result.

CodePudding user response:

Try the first range until upto, not upto 1

CodePudding user response:

There are two parts you need to handle. One is to check if the number is a prime number or not. Second is to iterate through the range of numbers. Refer to the code below,

from math import sqrt

sum = 0
#some explanation here we are iterating from 50 all the way to 1. So you can indicate your range here. -1 is the step value which tells the difference between each iteration.
for i in range(50, 0, -1):

    # Check whether it is a prime number here
    bPrime = True  #initialize a boolean here to be indicated by the loop below
    for a in range(2, int(sqrt(i))   1):
        if i % a == 0:
            bPrime = False    #definitely not a prime,break the loop and set boolean to false
            break
        bPrime = True         #not ==0 so it is a prime, set boolean to true
        
    if (bPrime) :
        # Add up if it is a prime number
        sum  = i
        
#view the results 
print(sum)

It should get you 328

CodePudding user response:

Hi Sumit firstly Welcome to Stackoverflow :)

Now as per your question the sum of prime numbers upto range 50 is 328 only, not 326.

But if you want the sum of first 50 odd prime numbers then you can make minor change in your code as below which will give you 326 [which is the sum of odd prime nos till range 50]

upto = int(input("Find sum of prime numbers upto : "))

sum = 0

for num in range(3, upto 1):

    
    for i in range(2, num):
        if (int(num % i) == 0):
            break;

    #If the number is prime then add it.
    else:
        sum  = num

print("\nSum of all prime numbers upto", upto, ":", sum)
  • Related