Ask Question
12 September, 00:04

Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13, ... ]) can be added before exceeding n. Associate this number with the variable k.

+1
Answers (1)
  1. 12 September, 03:58
    0
    def main ():

    n = int (input ('Enter the value of the variable n:'))

    k=2;

    totalSum = 0

    print ('The list of the prime numbers are as follows:')

    while k < = n:

    totalSum = totalSum+is_prime (k)

    k=k+1

    print ('Total sum of the prime numbers:', totalSum)

    def is_prime (k):

    primeNumber = 0

    i=1

    while i<=int (k):

    if (k % i) = = 0:

    primeNumber = primeNumber + 1

    i=i+1

    if (primeNumber==2):

    print (k)

    return k;

    else:

    return 0;

    main ()

    Explanation:

    Run the while loop until k is less than n. Determine if the variable k is prime then add it to the totalSum variable. Increment the value of k by 1. Create a function isPrime to check whether the number is prime or not by determining the factors of k which can be found using the modulus operator. Call the main function at the end.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers