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 the total exceeds n. Associate this number with the variable k.

Respuesta :

Answer:

The solution code is written in Python

  1. def isPrime(num):
  2.    for x in range(2, num):
  3.        if(num % x == 0):
  4.            return True  
  5.    return False  
  6. n = 1000
  7. k = 0
  8. total = 0
  9. value = 2
  10. while(total <= n):
  11.    if(isPrime(value)):
  12.        total += value  
  13.        k += 1
  14.    
  15.    value += 1
  16. print(k)

Explanation:

Given a function isPrime to check if the input number, num, is a prime (Line 1 - 5). Create the variable n and set it to 1000. Create variable k to hold the number of prime numbers which are added before the total exceed n (Line 8).

Create a while loop and set the loop condition to ensure the loop will keep going until the total of prime value exceed n (Line 12). Within the loop call the isPrime function and if the current input value is a prime, add the value to total and increment k by one (Line 13 - 15). Increment value by one before proceed to next iteration (Line 17).

Lastly, print the value of k (Line 19) and the sample output value is 36.

ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE