Dhruv Marwha
Dhruv Marwha

Reputation: 1074

Finding the sum of prime Numbers in a List in Python

I am Trying this question as an exercise but i am stuck.I will try to be as precise as possible

I would like to find the sum of primes with the list as an input

Say the input given to my function is a list like=[17,51,29,39],so it should return 46 as the answer as 17+29=46

Here's the code what i could write:

def sumprimes(l):
    sum=0
    for value in l:
        for i in range(1,float((value)/2)):
            if value%i==0:
                      sum+=value     
    print(sum) 

Note-on passing a list,the program should work. I haven't written this part.

Thanks in advance

Upvotes: 0

Views: 10723

Answers (5)

divya maddipudi
divya maddipudi

Reputation: 1

def prime(n):
    count = 0
    if n > 1:
        for i in range(1,n+1):
            if n % i == 0:
                count = count + 1        
        if count == 2:
            return True
        else:
            return False

def sumprimes(l):
    x = list(filter(prime,l))
    y = sum(x)
    return y
print(sumprimes([-3,3,1,13]))

Upvotes: 0

Krishna Soni
Krishna Soni

Reputation: 1

def sumprime(list):
    sum =0
    flag =1
    for i in range(len(list)):
        num = list[i]
        for j in range(2, num):
            if list[i]%j ==0:
                flag = 0
                break
            else:
                flag = 1
        if num ==2:
            flag =1
        if flag ==1:
            sum =sum +list[i]
    return(sum)

list =[2,3,5,7]
print(sumprime(list))

Upvotes: 0

user7507118
user7507118

Reputation: 11

Here is the solution for your queries

def sumprimes(l):
primeNum = []
for item in l:
    is_prime = True
    if(item >= 2):
        maxInt = int(item ** 0.5) + 1
        for i in range(2, maxInt):
            if(item % i == 0):
                is_prime = False
                break
        if(is_prime):
            primeNum.append(item)
return(sum(primeNum))

print(function([-3,1,6]))

Upvotes: 1

Carlos Afonso
Carlos Afonso

Reputation: 1957

You'd better create a function to test if number is prime as @wim said.

def is_prime(n):
    if n < 2:
        return False
    elif n <= 3:
        return True
    elif n % 2 == 0:
        return False

    # round up the sqrt of n
    length = int(n**.5) + 1  # n**.5 is equal to sqrt(n)
    for i in range(3, length, 2):
        if n % i == 0:
            return False

    return True

This is a simple and efficient primality test. Then you can simply sum with:

def sum_primes(l):
    return sum(n for n in l if is_prime(n))

Upvotes: 1

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96172

The problem with your code is that you are using range with float value. What you really need here is integer division. In Python 3, that is the // operator:

def sum_primes(l):
    total = 0
    for value in l:
        for i in range(2, value // 2):
            if value%i == 0:
                break
        else:
            total += value     
    return total

Also, you need to check if value is divisible by every number except 1 and itself. All numbers will be divisible by 1. So start the range from 2. Furthermore, it is numbers that are divisible that aren't prime, so add an else clause to your for-loop which is only executed if your for-loop goes to completion without the breaking i.e. none of the numbers you checked divide value, thus value is prime!

Upvotes: 1

Related Questions