Skip to main content
5 of 7
Formatted the test cases
Gymhgy
  • 8k
  • 11
  • 35

Closest Prime Number

Note: This is my first question on this site. If I need to improve anything, please comment an let me know before downvoting to oblivion. Thanks!

Challenge

This is a simple one. Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return itself, and if there are two primes equal length away from the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care HOW you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.) as long as it works.

This is a code golf, so standard rules apply, the program with the least bytes wins!

Test Cases

Input  =>  Output
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2