Problem 44384. Find the nearest prime number

Created by Matthew Eicholtz in Community

Happy 5th birthday, Cody! Since 5 is a prime number, let's have some fun looking for other prime numbers.

Given a positive integer x, find the nearest prime number. Keep in mind that the nearest prime may be less than x.


nearestprime(5) = 5
nearestprime(36) = 37
nearestprime(200) = 199

NOTE: You may ignore cases in which two prime numbers are equally close to x. (e.g., x=12 --> 11 and 13 are both primes).

Solution Stats

32.82% Correct | 67.18% Incorrect
Last solution submitted on Mar 15, 2019

Problem Comments

Solution Comments

Recent Solvers390

Suggested Problems

More from this Author46

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!