Write a function in Matlab which takes as input an integer x and as output returns the biggest prime number that is less than or equal to x. For example, if the input is 7, the output would be 7. If the input is 40, the output would be 37. (Reminder: isprime.)

Respuesta :

Answer:

Explanation:

The objective is to write a Matlab function getPrime()

CODE:

function prime = getPrime(x) %function definition for getPrime which receives x as input and returns prime.

   while (x >= 1) % loop until x is zero.

       if isprime(x) % matlab built-in function to check if x is a prime number.

           prime = x; % assign x to prime.

           break % break out of loop. we got the prime number value in variable prime.

       end

       x = x - 1; % when x is not prime, x is decremented by 1 and loop continues.

   end

end

prime = getPrime(12); % passes 12 to function.

disp(prime) % display value in prime. 11 is the output since it is the biggest prime number below 12.