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
1 isn't a prime number
agree @bmtran...
You should correct the test suite. 1 isn't a prime number.
Correct the test suite 1 is not prime number.
1 is not a prime number, in order to fulfill with the fundamental theorem of arithmetic:
https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic
https://en.wikipedia.org/wiki/Prime_number#Primality_of_one
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 Solvers709
Suggested Problems
-
958 Solvers
-
477 Solvers
-
Fix the last element of a cell array
1328 Solvers
-
1154 Solvers
-
Height of a right-angled triangle
1805 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!