Answer to Question #164884 in Python for raj

Question #164884

Write a program to print the sum of non-primes in the given N numbers. The numbers which are not primes are considered as non-primes.


1
Expert's answer
2021-02-19T06:20:09-0500
# The start point of the program
def main():
    N=int(input("Enter N: "))
    sumNonPrimes=0
    for number in range(0, N):
        if(isPrime(number)==False):
            sumNonPrimes+=number
    print("The sum of non-primes = "+str(sumNonPrimes))


# This function checks if the number is prime
def isPrime(number) :
    if (number <= 1) : 
        return False
    if (number <= 3) : 
        return True
    #check if the number is divisible 2 or 3
    if (number % 2 == 0 or number % 3 == 0) : 
        return False
    i = 5
    # Iterate from 25 to number
    while(i * i <= number) : 
        if (number % i == 0 or number % (i + 2) == 0) : 
            return False
        i = i + 6


    return True
# Call the main method
main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS