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.
# 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()
Comments
Leave a comment