Question

In: Computer Science

Write a python program to sum the prime numbers existing in an array . For instance...

Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10

Solutions

Expert Solution

This can be done using two methods
1st - we go through the given array and one by one check if each number is prime or not.
2nd - this is the efficient solution and the one we will implement.
In this method , we will find the max value in our array then we create a new boolean array prime[0..max]. All the prime numbers in this array will have True value and rest of them will have false.
Now we traverse the array and find the sum of prime numbers using our newly created array.

def primeSum(arr):
    n=len(arr)
    maxVal = max(arr)    # find the maximum value in our array
# create a Boolean array prime[0....max_val], this contains status of all the numbers from 0 to maximum value in array. If the specific number is prime the value in the array will be true else it will be false.
    prime=[True for i in range(maxVal + 1)] #creating the array
#0 and 1 are not prime so setting them to false 
#you might think we don't need these two numbers so why add them ? This is so that their location can match there value. Eg. In this array the value 2 is at location 2, 3 is at location 3 and so on.
# this way we can access the array easily. Eg. To check if 7 is prime we can do prime[7] and this will return True.
    prime[0] = False 
    prime[1] = False
# now changing the status of rest of the numbers here we take the first prime and change values of its multiples to false. Eg. We see 2 is prime so we change values of number 4,6,8.... To False in the array.Similary next number is 3 and its multiple 9,12,15,18.... will be set to False and so on
    for p in range(2, maxVal + 1): 
        if(p * p > maxVal):       # we only need to change the values until this holds as the rest of the array will be set automatically
            break
        if (prime[p] == True):
            for i in range(p * 2, maxVal+1, p): 
                prime[i] = False
# Now we calculate the sum of all prime numbers in our array. For a number in our array we check if the number is prime or not using the prime array we created.
    sum = 0 
    for i in range(n): 
        if (prime[arr[i]]): 
            sum += arr[i] 
    return sum
    
    
arr =[4,7,12,3,9]
print(primeSum(arr))

Output - 10


Related Solutions

Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
use Python The assertion that every even number is the sum of two prime numbers is...
use Python The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture. You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number. Assignment Requirements Three functions are required: get_input(): This function takes no parameters, but will ask the user for an integer number. It will return a valid integer....
1. Write a python function that receives two positive numbers and displays the prime numbers between...
1. Write a python function that receives two positive numbers and displays the prime numbers between them.Note: A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself. 2. Using either Whileor Foriteration loops, write a python code that prints the first Nnumbers in Fibonacci Sequence, where N is inputted by the user. Now, revise this code to instead print a) the Nthnumber in Fibonacci Sequence.b) the...
Write a program in Easy68K: a) Define an array of numbers in the memory.
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read two numbers from keyboard. The first number is the size of the array and the second number is what index of the array you want to access. The index you entered can be larger than the array. c) Display the element indexed by (index % size) in the array. 
Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the...
Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the user to enter a series of positive numbers. The user should enter "0" (zero) to signal the end of the series. After all the positive numbers have been entered, the program should display their sum. The program should display the following: • Contain proper indentation. • Comments (Blocks or Lines). • Good Variables initializations. • Good questions asking for the parameters of the code....
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
write a program to find the maximum possible sum such that no two chosen numbers are...
write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java
Part 1: Write a program that finds the sum and average of a series of numbers...
Part 1: Write a program that finds the sum and average of a series of numbers entered by the user. The program will first ask the user how many numbers there are. It then prompts the user for each of the numbers in turn and prints out the sum, average, the list of number from the user. Note: the average should always be a float, even if the user inputs are all ints. Part 2: Same as part 1 except...
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT