33
5
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 that number; if there are two primes equally close to 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 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
5
Hi and welcome to PPCG!. To avoid down voting due to lack of quality I suggest you to post it to the sandbox first and after a couple of days post it here
– Luis felipe De jesus Munoz – 2019-03-27T15:17:50.320This is one of the outputs requested in this challenge.
– Arnauld – 2019-03-27T15:24:13.603Very closely related but not quite identical. – Giuseppe – 2019-03-27T15:35:43.907
@Arnauld I saw that one, but I thought that they were different enough to warrant a new question. – Nathan Dimmer – 2019-03-27T15:37:30.270
@Giuseppe Yeah, I found out about that one after already posting... – Nathan Dimmer – 2019-03-27T15:42:11.840
@Arnauld Good idea! Added. – Nathan Dimmer – 2019-03-27T15:53:25.973
@Arnauld Oh! That’s really good! – Nathan Dimmer – 2019-03-27T16:19:24.503
Welcome to PPCG. Nice challenge and +1 for your opening note. Sandbox, as suggested by others, is a great place to start. – ElPedro – 2019-03-27T19:12:19.357
2
See also OEIS A051697.
– Eric Towers – 2019-03-28T08:52:32.563@EricTowers Oh wow... That's really convenient! How should I reference that in my question? – Nathan Dimmer – 2019-03-28T11:23:50.557
@Bobawob just add it in and say something like "This is OEIS A051697" with a link. Simple enough :-) – Giuseppe – 2019-03-28T14:30:43.543
Can anyone get this working with JS generators? I haven't had much luck here
– Pureferret – 2019-03-29T15:44:37.713