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 commentsProblem Recent Solvers862
Suggested Problems
-
Return a list sorted by number of occurrences
2822 Solvers
-
Who knows the last digit of pi?
672 Solvers
-
Determine Whether an array is empty
783 Solvers
-
We love vectorized solutions. Problem 1 : remove the row average.
836 Solvers
-
476 Solvers
More from this Author11
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!