Problem 981. Find nearest prime number less than input number
Find nearest prime number less than input number.
For example: if the input number is 125, then the nearest prime number which is less than this number is 113.
Be careful, in that 1 is not a prime number. So there is NO prime less than 2.
Solution Stats
Problem Comments
-
8 Comments
I removed the test case where n==2, since 1 is indeed not a prime number. I also added a random test case, so the problem is not so easily cooked by knowing all the test cases.
1 is indeed prime. Aliens will laugh at us when we say it is not. 1 causes all sorts of problems by not being prime. As many as it causes by being prime. It is already an exception in thousands of mathematical theorems. Why mathematicians chose this one theorem to not include 1 as an exception is beyond me.
1 is a unit (in the ring-theoretic sense), and units are excluded from the definitions of prime elements and irreducible elements in rings. I'm not a mathematician and don't have more than a basic undergrad exposure to abstract rings, but there surely is a good reason. Any mathematicians reading this who can shed on light on this?
Solution Comments
Show commentsGroup

The Prime Directive
- 14 Problems
- 37 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 Solvers863
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!