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.
Examples
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
Problem Comments
1 Comment
Solution Comments
Show comments
Loading...
Problem Recent Solvers672
Suggested Problems
-
Maximum running product for a string of numbers
2259 Solvers
-
What is the distance from point P(x,y) to the line Ax + By + C = 0?
562 Solvers
-
Rotate input square matrix 90 degrees CCW without rot90
700 Solvers
-
Write c^3 as sum of two squares a^2+b^2
332 Solvers
-
Find the Oldest Person in a Room
21801 Solvers
More from this Author43
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Test case No. 5
x = 911;
y_correct = 911;
Should the nearest prime number for 911 have been 907?