Problem 44384. Find the nearest prime number
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
Tran Hiep Dinh
on 21 Mar 2019
Test case No. 5
x = 911;
y_correct = 911;
Should the nearest prime number for 911 have been 907?
Solution Comments
Show commentsGroup

The Prime Directive
- 14 Problems
- 36 Finishers
- Find the nearest prime number
- Extra safe primes
- Prime factor digits
- Twin Primes
- Twins in a Window
- The Goldbach Conjecture
- The Goldbach Conjecture, Part 2
- Goldbach's marginal conjecture - Write integer as sum of three primes
- Sophie Germain prime
- Mersenne Primes vs. All Primes
- Circular Primes (based on Project Euler, problem 35)
- Numbers spiral diagonals (Part 2)
- Pernicious Anniversary Problem
- Prime Ladders
Problem Recent Solvers639
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!