Return the Closest Prime Number












19












$begingroup$


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 of 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 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









share|improve this question











$endgroup$








  • 3




    $begingroup$
    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
    $endgroup$
    – Luis felipe De jesus Munoz
    14 hours ago










  • $begingroup$
    This is one of the outputs requested in this challenge.
    $endgroup$
    – Arnauld
    14 hours ago










  • $begingroup$
    Very closely related but not quite identical.
    $endgroup$
    – Giuseppe
    14 hours ago










  • $begingroup$
    @Arnauld I saw that one, but I thought that they were different enough to warrant a new question.
    $endgroup$
    – Bobawob
    14 hours ago










  • $begingroup$
    @Giuseppe Yeah, I found out about that one after already posting...
    $endgroup$
    – Bobawob
    14 hours ago
















19












$begingroup$


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 of 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 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









share|improve this question











$endgroup$








  • 3




    $begingroup$
    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
    $endgroup$
    – Luis felipe De jesus Munoz
    14 hours ago










  • $begingroup$
    This is one of the outputs requested in this challenge.
    $endgroup$
    – Arnauld
    14 hours ago










  • $begingroup$
    Very closely related but not quite identical.
    $endgroup$
    – Giuseppe
    14 hours ago










  • $begingroup$
    @Arnauld I saw that one, but I thought that they were different enough to warrant a new question.
    $endgroup$
    – Bobawob
    14 hours ago










  • $begingroup$
    @Giuseppe Yeah, I found out about that one after already posting...
    $endgroup$
    – Bobawob
    14 hours ago














19












19








19


3



$begingroup$


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 of 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 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









share|improve this question











$endgroup$




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 of 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 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






code-golf primes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









Cody Gray

2,029416




2,029416










asked 15 hours ago









BobawobBobawob

16611




16611








  • 3




    $begingroup$
    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
    $endgroup$
    – Luis felipe De jesus Munoz
    14 hours ago










  • $begingroup$
    This is one of the outputs requested in this challenge.
    $endgroup$
    – Arnauld
    14 hours ago










  • $begingroup$
    Very closely related but not quite identical.
    $endgroup$
    – Giuseppe
    14 hours ago










  • $begingroup$
    @Arnauld I saw that one, but I thought that they were different enough to warrant a new question.
    $endgroup$
    – Bobawob
    14 hours ago










  • $begingroup$
    @Giuseppe Yeah, I found out about that one after already posting...
    $endgroup$
    – Bobawob
    14 hours ago














  • 3




    $begingroup$
    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
    $endgroup$
    – Luis felipe De jesus Munoz
    14 hours ago










  • $begingroup$
    This is one of the outputs requested in this challenge.
    $endgroup$
    – Arnauld
    14 hours ago










  • $begingroup$
    Very closely related but not quite identical.
    $endgroup$
    – Giuseppe
    14 hours ago










  • $begingroup$
    @Arnauld I saw that one, but I thought that they were different enough to warrant a new question.
    $endgroup$
    – Bobawob
    14 hours ago










  • $begingroup$
    @Giuseppe Yeah, I found out about that one after already posting...
    $endgroup$
    – Bobawob
    14 hours ago








3




3




$begingroup$
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
$endgroup$
– Luis felipe De jesus Munoz
14 hours ago




$begingroup$
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
$endgroup$
– Luis felipe De jesus Munoz
14 hours ago












$begingroup$
This is one of the outputs requested in this challenge.
$endgroup$
– Arnauld
14 hours ago




$begingroup$
This is one of the outputs requested in this challenge.
$endgroup$
– Arnauld
14 hours ago












$begingroup$
Very closely related but not quite identical.
$endgroup$
– Giuseppe
14 hours ago




$begingroup$
Very closely related but not quite identical.
$endgroup$
– Giuseppe
14 hours ago












$begingroup$
@Arnauld I saw that one, but I thought that they were different enough to warrant a new question.
$endgroup$
– Bobawob
14 hours ago




$begingroup$
@Arnauld I saw that one, but I thought that they were different enough to warrant a new question.
$endgroup$
– Bobawob
14 hours ago












$begingroup$
@Giuseppe Yeah, I found out about that one after already posting...
$endgroup$
– Bobawob
14 hours ago




$begingroup$
@Giuseppe Yeah, I found out about that one after already posting...
$endgroup$
– Bobawob
14 hours ago










22 Answers
22






active

oldest

votes


















6












$begingroup$


05AB1E, 5 bytes



Åps.x


Try it online!
or as a Test Suite



Inefficient for big numbers






share|improve this answer









$endgroup$





















    6












    $begingroup$

    JavaScript (ES6), 53 bytes





    n=>(g=(o,d=N=n+o)=>N%--d?g(o,d):d-1?g(o<0?-o:~o):N)``


    Try it online!



    Commented



    n => (            // n = input
    g = ( // g = recursive function taking:
    o, // o = offset
    d = // d = current divisor, initialized to N
    N = n + o // N = input + offset
    ) => //
    N % --d ? // decrement d; if d is not a divisor of N:
    g(o, d) // do recursive calls until it is
    : // else:
    d - 1 ? // if d is not equal to 1 (either N is composite or N = 1):
    g( // do a recursive call with the next offset:
    o < 0 ? // if o is negative:
    -o // make it positive (e.g. -1 -> +1)
    : // else:
    ~o // use -(o + 1) (e.g. +1 -> -2)
    ) // end of recursive call
    : // else (N is prime):
    N // stop recursion and return N
    )`` // initial call to g with o = [''] (zero-ish)





    share|improve this answer











    $endgroup$





















      5












      $begingroup$


      Octave, 40 bytes





      @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))


      Try it online!



      This uses the fact that there is always a prime between n and 2*n (Bertrand–Chebyshev theorem).



      How it works



      @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))

      @(n) % Define anonymous function with input n
      p=primes(2*n) % Vector of primes up to 2*n. Assign to p
      abs(n-( )) % Absolute difference between n and each prime
      [~,k]=min( ) % Index of first minimum (assign to k; not used)
      p( ) % Apply that index to p





      share|improve this answer











      $endgroup$





















        3












        $begingroup$

        Pyth, 10 bytes



        haDQfP_TSy


        Try it online here, or verify all the test cases at once here.



        haDQfP_TSyQ   Implicit: Q=eval(input())
        Trailing Q inferred
        yQ 2 * Q
        S Range from 1 to the above
        f Filter keep the elements of the above, as T, where:
        P_T Is T prime?
        D Order the above by...
        a Q ... absolute difference between each element and Q
        This is a stable sort, so smaller primes will be sorted before larger ones if difference is the same
        h Take the first element of the above, implicit print





        share|improve this answer









        $endgroup$





















          3












          $begingroup$


          Japt, 5 bytes



          _j}cU


          Try it or run all test cases



          _j}cU     :Implicit input of integer U
          _ :Function taking an integer as an argument
          j : Test if integer is prime
          } :End function
          cU :Return the first integer in [U,U-1,U+1,U-2,...] that returns true





          share|improve this answer











          $endgroup$





















            3












            $begingroup$


            Gaia, 3 bytes



            ṅD⌡


            Try it online!



            Rather slow for large inputs, but works given enough memory/time.



            I'm not sure why D⌡ implicitly pushes z again, but it makes this a remarkably short answer!



            ṅ	| implicit input z: push first z prime numbers, call it P
            D⌡ | take the absolute difference between P and (implicit) z,
            | returning the smallest value in P with the minimum absolute difference





            share|improve this answer











            $endgroup$





















              2












              $begingroup$


              Wolfram Language (Mathematica), 53 bytes



              If[PrimeQ[s=#],s,#&@@Nearest[s~NextPrime~{-1, 1},s]]&


              Try it online!






              share|improve this answer









              $endgroup$





















                2












                $begingroup$


                Python 2, 96 bytes





                l=lambda p:min(filter(lambda p:all(p%n for n in range(2,p)),range(2,p*2)),key=lambda x:abs(x-p))


                Try it online!






                share|improve this answer









                $endgroup$









                • 2




                  $begingroup$
                  This seems to fail for $n=1$.
                  $endgroup$
                  – Arnauld
                  13 hours ago



















                2












                $begingroup$


                VDM-SL, 161 bytes





                f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                A full program to run might look like this - it's worth noting that the bounds of the set of primes used should probably be changed if you actually want to run this, since it will take a long time to run for 1 million:



                functions
                f:nat1+>nat1
                f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                Explanation:



                f(i)==                                        /* f is a function which takes a nat1 (natural number not including 0)*/
                (lambda p:set of nat1 /* define a lambda which takes a set of nat1*/
                &let z in set p be st /* which has an element z in the set such that */
                forall m in set p /* for every element in the set*/
                &abs(m-i) /* the difference between the element m and the input*/
                >=abs(z-i) /* is greater than or equal to the difference between the element z and the input */
                in z) /* and return z from the lambda */
                ( /* apply this lambda to... */
                { /* a set defined by comprehension as.. */
                x| /* all elements x such that.. */
                x in set{1,...,9**7} /* x is between 1 and 9^7 */
                &forall y in set{2,...,1003} /* and for all values between 2 and 1003*/
                &y<>x=>x mod y<>0 /* y is not x implies x is not divisible by y*/
                }
                )





                share|improve this answer









                $endgroup$





















                  2












                  $begingroup$

                  APL(NARS), 38 chars, 76 bytes



                  {⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}


                  0π is the test for prime, ¯1π the prev prime, 1π is the next prime; test:



                    f←{⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}
                  f¨80 100 5 9 532 1
                  79 101 5 7 523 2





                  share|improve this answer









                  $endgroup$





















                    2












                    $begingroup$


                    Jelly, 9 7 bytes



                    ḤÆRạÞµḢ


                    Try it online!



                    Slow for larger input, but works ok for the requested range. Thanks to @EriktheOutgolfer for saving 2 bytes!






                    share|improve this answer











                    $endgroup$













                    • $begingroup$
                      Hey, that's clever! Save two by substituting _A¥ with (absolute difference). Oh, and can really be .
                      $endgroup$
                      – Erik the Outgolfer
                      11 hours ago












                    • $begingroup$
                      @EriktheOutgolfer thanks. Surely using won’t always work? It means that only primes up to n+1 will be found, while the closest might be n+2.
                      $endgroup$
                      – Nick Kennedy
                      7 hours ago










                    • $begingroup$
                      Hm, that's a concern.
                      $endgroup$
                      – Erik the Outgolfer
                      7 hours ago





















                    2












                    $begingroup$


                    Tidy, 43 bytes



                    {x:(prime↦splice(]x,-1,-∞],[x,∞]))@0}


                    Try it online!



                    Explanation



                    This is a lambda with parameter x. This works by creating the following sequence:



                    [x - 1, x, x - 2, x + 1, x - 3, x + 2, x - 4, x + 3, ...]


                    This is splicing together the two sequences ]x, -1, -∞] (left-closed, right-open) and [x, ∞] (both open).



                    For x = 80, this looks like:



                    [79, 80, 78, 81, 77, 82, 76, 83, 75, 84, 74, 85, ...]


                    Then, we use f↦s to select all elements from s satisfying f. In this case, we filter out all composite numbers, leaving only the prime ones. For the same x, this becomes:



                    [79, 83, 73, 71, 89, 67, 97, 61, 59, 101, 103, 53, ...]


                    Then, we use (...)@0 to select the first member of this sequence. Since the lower of the two needs to be selected, the sequence which starts with x - 1 is spliced in first.



                    Note: Only one of x and x - 1 can be prime, so it is okay that the spliced sequence starts with x - 1. Though the sequence could be open on both sides ([x,-1,-∞]), this would needlessly include x twice in the sequence. So, for sake of "efficiency", I chose the left-closed version (also because I like to show off Tidy).






                    share|improve this answer









                    $endgroup$





















                      2












                      $begingroup$


                      Python 2, 71 bytes





                      f=lambda n,k=1,p=1:k<n*3and min(k+n-p%k*2*n,f(n,k+1,p*k*k)-n,key=abs)+n


                      Try it online!



                      A recursive function that uses the Wilson's Theorem prime generator. The product p tracks $(k-1)!^2$, and p%k is 1 for primes and 0 for non-primes. To make it easy to compare abs(k-n) for different primes k, we store k-n and compare via abs, adding back n to get the result k.



                      The expression k+n-p%k*2*n is designed to give k-n on primes (where p%k=1), and otherwise a "bad" value of k+n that's always bigger in absolute value and so doesn't affect the minimum, so that non-primes are passed over.






                      share|improve this answer









                      $endgroup$





















                        1












                        $begingroup$


                        C# (Visual C# Interactive Compiler), 112 bytes





                        g=>Enumerable.Range(2,2<<20).Where(x=>Enumerable.Range(1,x).Count(y=>x%y<1)<3).OrderBy(x=>Math.Abs(x-g)).First()


                        Try it online!



                        Left shifts by 20 in submission but 10 in TIO so that TIO terminates for test cases.






                        share|improve this answer









                        $endgroup$





















                          1












                          $begingroup$


                          APL (Dyalog Extended), 20 bytesSBCS





                          ⊢(⊃>/⍤|⍤-⌽⊢)¯4 4⍭3⌈⊢


                          Try it online!



                           the argument



                          3⌈ max of 3 and that



                          ¯4 4⍭ the previous and next primes`



                          ⊢() apply the following infix tacit function to that, with the original argument as left argument:



                           the primes



                           … cyclically rotate them the following number of steps:



                            - the original argument minus the primes

                             then

                            | absolute value of that

                             then

                            >/ Boolean (0/1) whether the left is greater than the right (i.e. 1 if next is closer)



                           pick the first one (i.e. previous if previous is closer and next if next is closer)






                          share|improve this answer









                          $endgroup$





















                            1












                            $begingroup$

                            Swift, 186 bytes



                            func p(a:Int){let b=q(a:a,b:-1),c=q(a:a,b:1);print(a-b<=c-a ? b:c)}
                            func q(a:Int,b:Int)->Int{var k=max(a,2),c=2;while k>c && c != a/2{if k%c==0{k+=b;c=2}else{c=c==2 ? c+1:c+2}};return k}


                            Try it online!






                            share|improve this answer









                            $endgroup$





















                              1












                              $begingroup$


                              Jelly, 14 bytes



                              ÆpæRÆnạÞƲ2>?2Ḣ


                              Try it online!






                              share|improve this answer









                              $endgroup$





















                                1












                                $begingroup$


                                C# (Visual C# Interactive Compiler), 96 bytes





                                n=>{for(int i=0,j;;)if((j=n+i/2*(i++%2*2-1))>1&&Enumerable.Range(2,j-2).All(d=>j%d>0))return j;}


                                Try it online!






                                share|improve this answer









                                $endgroup$





















                                  1












                                  $begingroup$


                                  Zsh, 101 92 91 bytes



                                  -9 by collapsing the body into the head of p's loop, -1 from using i=j instead of i=$1 in main loop.





                                  p(){for ((n=2;n<$1&&$1%n++;)):
                                  (($1==n))&&<<<$1}
                                  j=$1
                                  for ((i=j;;++j&&--i))p $i||p $j&&exit


                                  Try it online!



                                  Try it online!



                                  57 48 bytes to the prime testing function, 43 42 bytes to the main loop (1 byte to the newline between them):



                                  p(){  # prime function: takes one input, outputs via return code
                                  for (( n = 2; n < $1 && $1 % n++; )) # divisibility check in loop header
                                  : # no-op loop body
                                  (( $1 == n )) && # if we looped up to $1:
                                  <<< $1 # echo out $1. Otherwise, this will return false
                                  }


                                  For the last condition, we can't use the shorter (($1-n))||, because we need to return false to the main loop if we didn't find a prime. We print in the function to avoid complexity in the main loop.



                                  j=$1                          # set i = j = $1. Doing one in and one out is smallest
                                  for (( i = j; ; ++j && --i )) # loop indefinitely, increment and decrement
                                  p $i || p $j && exit # if either $i or $j was a prime, exit


                                  Conditionals are left-associative, which we take advantage of here. We do test the starting number twice to make the decrement logic simpler.






                                  share|improve this answer










                                  New contributor




                                  GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.






                                  $endgroup$





















                                    1












                                    $begingroup$

                                    C, 122 bytes



                                    #define r return
                                    p(a,i){if(a<2)r 0;i=1;while(++i<a)if(a%i<1)r 0;r 1;}c(a,b){b=a;while(1){if(p(b))r b;if(p(--a))r a;b++;}}


                                    Use it calling function c() and passing as argument the number, it should return the closest prime.






                                    share|improve this answer









                                    $endgroup$













                                    • $begingroup$
                                      But c() receives two parameters... Also, you can probably shorten the while(1) to for(;;) (untested, since I don't get how to run your code
                                      $endgroup$
                                      – Embodiment of Ignorance
                                      1 hour ago



















                                    1












                                    $begingroup$

                                    Python 2, 93 bytes





                                    lambda n:sorted(range(1,3*n),key=lambda x:abs(x-n)if all(x%k for k in range(2,x))else 2*n)[0]





                                    share|improve this answer











                                    $endgroup$









                                    • 1




                                      $begingroup$
                                      You don't need the f= in the start
                                      $endgroup$
                                      – Embodiment of Ignorance
                                      9 hours ago










                                    • $begingroup$
                                      @EmbodimentofIgnorance Thanks, fixed that along with the range and non-prime penalty criteria that was causing n=1 to fail
                                      $endgroup$
                                      – deustice
                                      9 hours ago






                                    • 1




                                      $begingroup$
                                      The primality check doesn't work for Fermat pseudoprimes such as 341=31*11 which it calls prime.
                                      $endgroup$
                                      – xnor
                                      8 hours ago










                                    • $begingroup$
                                      @xnor Fixed, thanks
                                      $endgroup$
                                      – deustice
                                      7 hours ago



















                                    1












                                    $begingroup$


                                    J, 19 15 bytes



                                    (0{]/:|@-)p:@i.


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$













                                      Your Answer





                                      StackExchange.ifUsing("editor", function () {
                                      return StackExchange.using("mathjaxEditing", function () {
                                      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                                      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                                      });
                                      });
                                      }, "mathjax-editing");

                                      StackExchange.ifUsing("editor", function () {
                                      StackExchange.using("externalEditor", function () {
                                      StackExchange.using("snippets", function () {
                                      StackExchange.snippets.init();
                                      });
                                      });
                                      }, "code-snippets");

                                      StackExchange.ready(function() {
                                      var channelOptions = {
                                      tags: "".split(" "),
                                      id: "200"
                                      };
                                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                                      StackExchange.using("externalEditor", function() {
                                      // Have to fire editor after snippets, if snippets enabled
                                      if (StackExchange.settings.snippets.snippetsEnabled) {
                                      StackExchange.using("snippets", function() {
                                      createEditor();
                                      });
                                      }
                                      else {
                                      createEditor();
                                      }
                                      });

                                      function createEditor() {
                                      StackExchange.prepareEditor({
                                      heartbeatType: 'answer',
                                      autoActivateHeartbeat: false,
                                      convertImagesToLinks: false,
                                      noModals: true,
                                      showLowRepImageUploadWarning: true,
                                      reputationToPostImages: null,
                                      bindNavPrevention: true,
                                      postfix: "",
                                      imageUploader: {
                                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                                      allowUrls: true
                                      },
                                      onDemand: true,
                                      discardSelector: ".discard-answer"
                                      ,immediatelyShowMarkdownHelp:true
                                      });


                                      }
                                      });














                                      draft saved

                                      draft discarded


















                                      StackExchange.ready(
                                      function () {
                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182305%2freturn-the-closest-prime-number%23new-answer', 'question_page');
                                      }
                                      );

                                      Post as a guest















                                      Required, but never shown

























                                      22 Answers
                                      22






                                      active

                                      oldest

                                      votes








                                      22 Answers
                                      22






                                      active

                                      oldest

                                      votes









                                      active

                                      oldest

                                      votes






                                      active

                                      oldest

                                      votes









                                      6












                                      $begingroup$


                                      05AB1E, 5 bytes



                                      Åps.x


                                      Try it online!
                                      or as a Test Suite



                                      Inefficient for big numbers






                                      share|improve this answer









                                      $endgroup$


















                                        6












                                        $begingroup$


                                        05AB1E, 5 bytes



                                        Åps.x


                                        Try it online!
                                        or as a Test Suite



                                        Inefficient for big numbers






                                        share|improve this answer









                                        $endgroup$
















                                          6












                                          6








                                          6





                                          $begingroup$


                                          05AB1E, 5 bytes



                                          Åps.x


                                          Try it online!
                                          or as a Test Suite



                                          Inefficient for big numbers






                                          share|improve this answer









                                          $endgroup$




                                          05AB1E, 5 bytes



                                          Åps.x


                                          Try it online!
                                          or as a Test Suite



                                          Inefficient for big numbers







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered 14 hours ago









                                          EmignaEmigna

                                          47.2k433143




                                          47.2k433143























                                              6












                                              $begingroup$

                                              JavaScript (ES6), 53 bytes





                                              n=>(g=(o,d=N=n+o)=>N%--d?g(o,d):d-1?g(o<0?-o:~o):N)``


                                              Try it online!



                                              Commented



                                              n => (            // n = input
                                              g = ( // g = recursive function taking:
                                              o, // o = offset
                                              d = // d = current divisor, initialized to N
                                              N = n + o // N = input + offset
                                              ) => //
                                              N % --d ? // decrement d; if d is not a divisor of N:
                                              g(o, d) // do recursive calls until it is
                                              : // else:
                                              d - 1 ? // if d is not equal to 1 (either N is composite or N = 1):
                                              g( // do a recursive call with the next offset:
                                              o < 0 ? // if o is negative:
                                              -o // make it positive (e.g. -1 -> +1)
                                              : // else:
                                              ~o // use -(o + 1) (e.g. +1 -> -2)
                                              ) // end of recursive call
                                              : // else (N is prime):
                                              N // stop recursion and return N
                                              )`` // initial call to g with o = [''] (zero-ish)





                                              share|improve this answer











                                              $endgroup$


















                                                6












                                                $begingroup$

                                                JavaScript (ES6), 53 bytes





                                                n=>(g=(o,d=N=n+o)=>N%--d?g(o,d):d-1?g(o<0?-o:~o):N)``


                                                Try it online!



                                                Commented



                                                n => (            // n = input
                                                g = ( // g = recursive function taking:
                                                o, // o = offset
                                                d = // d = current divisor, initialized to N
                                                N = n + o // N = input + offset
                                                ) => //
                                                N % --d ? // decrement d; if d is not a divisor of N:
                                                g(o, d) // do recursive calls until it is
                                                : // else:
                                                d - 1 ? // if d is not equal to 1 (either N is composite or N = 1):
                                                g( // do a recursive call with the next offset:
                                                o < 0 ? // if o is negative:
                                                -o // make it positive (e.g. -1 -> +1)
                                                : // else:
                                                ~o // use -(o + 1) (e.g. +1 -> -2)
                                                ) // end of recursive call
                                                : // else (N is prime):
                                                N // stop recursion and return N
                                                )`` // initial call to g with o = [''] (zero-ish)





                                                share|improve this answer











                                                $endgroup$
















                                                  6












                                                  6








                                                  6





                                                  $begingroup$

                                                  JavaScript (ES6), 53 bytes





                                                  n=>(g=(o,d=N=n+o)=>N%--d?g(o,d):d-1?g(o<0?-o:~o):N)``


                                                  Try it online!



                                                  Commented



                                                  n => (            // n = input
                                                  g = ( // g = recursive function taking:
                                                  o, // o = offset
                                                  d = // d = current divisor, initialized to N
                                                  N = n + o // N = input + offset
                                                  ) => //
                                                  N % --d ? // decrement d; if d is not a divisor of N:
                                                  g(o, d) // do recursive calls until it is
                                                  : // else:
                                                  d - 1 ? // if d is not equal to 1 (either N is composite or N = 1):
                                                  g( // do a recursive call with the next offset:
                                                  o < 0 ? // if o is negative:
                                                  -o // make it positive (e.g. -1 -> +1)
                                                  : // else:
                                                  ~o // use -(o + 1) (e.g. +1 -> -2)
                                                  ) // end of recursive call
                                                  : // else (N is prime):
                                                  N // stop recursion and return N
                                                  )`` // initial call to g with o = [''] (zero-ish)





                                                  share|improve this answer











                                                  $endgroup$



                                                  JavaScript (ES6), 53 bytes





                                                  n=>(g=(o,d=N=n+o)=>N%--d?g(o,d):d-1?g(o<0?-o:~o):N)``


                                                  Try it online!



                                                  Commented



                                                  n => (            // n = input
                                                  g = ( // g = recursive function taking:
                                                  o, // o = offset
                                                  d = // d = current divisor, initialized to N
                                                  N = n + o // N = input + offset
                                                  ) => //
                                                  N % --d ? // decrement d; if d is not a divisor of N:
                                                  g(o, d) // do recursive calls until it is
                                                  : // else:
                                                  d - 1 ? // if d is not equal to 1 (either N is composite or N = 1):
                                                  g( // do a recursive call with the next offset:
                                                  o < 0 ? // if o is negative:
                                                  -o // make it positive (e.g. -1 -> +1)
                                                  : // else:
                                                  ~o // use -(o + 1) (e.g. +1 -> -2)
                                                  ) // end of recursive call
                                                  : // else (N is prime):
                                                  N // stop recursion and return N
                                                  )`` // initial call to g with o = [''] (zero-ish)






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited 13 hours ago

























                                                  answered 14 hours ago









                                                  ArnauldArnauld

                                                  79.9k797330




                                                  79.9k797330























                                                      5












                                                      $begingroup$


                                                      Octave, 40 bytes





                                                      @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))


                                                      Try it online!



                                                      This uses the fact that there is always a prime between n and 2*n (Bertrand–Chebyshev theorem).



                                                      How it works



                                                      @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))

                                                      @(n) % Define anonymous function with input n
                                                      p=primes(2*n) % Vector of primes up to 2*n. Assign to p
                                                      abs(n-( )) % Absolute difference between n and each prime
                                                      [~,k]=min( ) % Index of first minimum (assign to k; not used)
                                                      p( ) % Apply that index to p





                                                      share|improve this answer











                                                      $endgroup$


















                                                        5












                                                        $begingroup$


                                                        Octave, 40 bytes





                                                        @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))


                                                        Try it online!



                                                        This uses the fact that there is always a prime between n and 2*n (Bertrand–Chebyshev theorem).



                                                        How it works



                                                        @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))

                                                        @(n) % Define anonymous function with input n
                                                        p=primes(2*n) % Vector of primes up to 2*n. Assign to p
                                                        abs(n-( )) % Absolute difference between n and each prime
                                                        [~,k]=min( ) % Index of first minimum (assign to k; not used)
                                                        p( ) % Apply that index to p





                                                        share|improve this answer











                                                        $endgroup$
















                                                          5












                                                          5








                                                          5





                                                          $begingroup$


                                                          Octave, 40 bytes





                                                          @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))


                                                          Try it online!



                                                          This uses the fact that there is always a prime between n and 2*n (Bertrand–Chebyshev theorem).



                                                          How it works



                                                          @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))

                                                          @(n) % Define anonymous function with input n
                                                          p=primes(2*n) % Vector of primes up to 2*n. Assign to p
                                                          abs(n-( )) % Absolute difference between n and each prime
                                                          [~,k]=min( ) % Index of first minimum (assign to k; not used)
                                                          p( ) % Apply that index to p





                                                          share|improve this answer











                                                          $endgroup$




                                                          Octave, 40 bytes





                                                          @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))


                                                          Try it online!



                                                          This uses the fact that there is always a prime between n and 2*n (Bertrand–Chebyshev theorem).



                                                          How it works



                                                          @(n)p([~,k]=min(abs(n-(p=primes(2*n)))))

                                                          @(n) % Define anonymous function with input n
                                                          p=primes(2*n) % Vector of primes up to 2*n. Assign to p
                                                          abs(n-( )) % Absolute difference between n and each prime
                                                          [~,k]=min( ) % Index of first minimum (assign to k; not used)
                                                          p( ) % Apply that index to p






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited 7 hours ago

























                                                          answered 11 hours ago









                                                          Luis MendoLuis Mendo

                                                          75.1k888291




                                                          75.1k888291























                                                              3












                                                              $begingroup$

                                                              Pyth, 10 bytes



                                                              haDQfP_TSy


                                                              Try it online here, or verify all the test cases at once here.



                                                              haDQfP_TSyQ   Implicit: Q=eval(input())
                                                              Trailing Q inferred
                                                              yQ 2 * Q
                                                              S Range from 1 to the above
                                                              f Filter keep the elements of the above, as T, where:
                                                              P_T Is T prime?
                                                              D Order the above by...
                                                              a Q ... absolute difference between each element and Q
                                                              This is a stable sort, so smaller primes will be sorted before larger ones if difference is the same
                                                              h Take the first element of the above, implicit print





                                                              share|improve this answer









                                                              $endgroup$


















                                                                3












                                                                $begingroup$

                                                                Pyth, 10 bytes



                                                                haDQfP_TSy


                                                                Try it online here, or verify all the test cases at once here.



                                                                haDQfP_TSyQ   Implicit: Q=eval(input())
                                                                Trailing Q inferred
                                                                yQ 2 * Q
                                                                S Range from 1 to the above
                                                                f Filter keep the elements of the above, as T, where:
                                                                P_T Is T prime?
                                                                D Order the above by...
                                                                a Q ... absolute difference between each element and Q
                                                                This is a stable sort, so smaller primes will be sorted before larger ones if difference is the same
                                                                h Take the first element of the above, implicit print





                                                                share|improve this answer









                                                                $endgroup$
















                                                                  3












                                                                  3








                                                                  3





                                                                  $begingroup$

                                                                  Pyth, 10 bytes



                                                                  haDQfP_TSy


                                                                  Try it online here, or verify all the test cases at once here.



                                                                  haDQfP_TSyQ   Implicit: Q=eval(input())
                                                                  Trailing Q inferred
                                                                  yQ 2 * Q
                                                                  S Range from 1 to the above
                                                                  f Filter keep the elements of the above, as T, where:
                                                                  P_T Is T prime?
                                                                  D Order the above by...
                                                                  a Q ... absolute difference between each element and Q
                                                                  This is a stable sort, so smaller primes will be sorted before larger ones if difference is the same
                                                                  h Take the first element of the above, implicit print





                                                                  share|improve this answer









                                                                  $endgroup$



                                                                  Pyth, 10 bytes



                                                                  haDQfP_TSy


                                                                  Try it online here, or verify all the test cases at once here.



                                                                  haDQfP_TSyQ   Implicit: Q=eval(input())
                                                                  Trailing Q inferred
                                                                  yQ 2 * Q
                                                                  S Range from 1 to the above
                                                                  f Filter keep the elements of the above, as T, where:
                                                                  P_T Is T prime?
                                                                  D Order the above by...
                                                                  a Q ... absolute difference between each element and Q
                                                                  This is a stable sort, so smaller primes will be sorted before larger ones if difference is the same
                                                                  h Take the first element of the above, implicit print






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered 14 hours ago









                                                                  SokSok

                                                                  4,127925




                                                                  4,127925























                                                                      3












                                                                      $begingroup$


                                                                      Japt, 5 bytes



                                                                      _j}cU


                                                                      Try it or run all test cases



                                                                      _j}cU     :Implicit input of integer U
                                                                      _ :Function taking an integer as an argument
                                                                      j : Test if integer is prime
                                                                      } :End function
                                                                      cU :Return the first integer in [U,U-1,U+1,U-2,...] that returns true





                                                                      share|improve this answer











                                                                      $endgroup$


















                                                                        3












                                                                        $begingroup$


                                                                        Japt, 5 bytes



                                                                        _j}cU


                                                                        Try it or run all test cases



                                                                        _j}cU     :Implicit input of integer U
                                                                        _ :Function taking an integer as an argument
                                                                        j : Test if integer is prime
                                                                        } :End function
                                                                        cU :Return the first integer in [U,U-1,U+1,U-2,...] that returns true





                                                                        share|improve this answer











                                                                        $endgroup$
















                                                                          3












                                                                          3








                                                                          3





                                                                          $begingroup$


                                                                          Japt, 5 bytes



                                                                          _j}cU


                                                                          Try it or run all test cases



                                                                          _j}cU     :Implicit input of integer U
                                                                          _ :Function taking an integer as an argument
                                                                          j : Test if integer is prime
                                                                          } :End function
                                                                          cU :Return the first integer in [U,U-1,U+1,U-2,...] that returns true





                                                                          share|improve this answer











                                                                          $endgroup$




                                                                          Japt, 5 bytes



                                                                          _j}cU


                                                                          Try it or run all test cases



                                                                          _j}cU     :Implicit input of integer U
                                                                          _ :Function taking an integer as an argument
                                                                          j : Test if integer is prime
                                                                          } :End function
                                                                          cU :Return the first integer in [U,U-1,U+1,U-2,...] that returns true






                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited 14 hours ago

























                                                                          answered 14 hours ago









                                                                          ShaggyShaggy

                                                                          19k21667




                                                                          19k21667























                                                                              3












                                                                              $begingroup$


                                                                              Gaia, 3 bytes



                                                                              ṅD⌡


                                                                              Try it online!



                                                                              Rather slow for large inputs, but works given enough memory/time.



                                                                              I'm not sure why D⌡ implicitly pushes z again, but it makes this a remarkably short answer!



                                                                              ṅ	| implicit input z: push first z prime numbers, call it P
                                                                              D⌡ | take the absolute difference between P and (implicit) z,
                                                                              | returning the smallest value in P with the minimum absolute difference





                                                                              share|improve this answer











                                                                              $endgroup$


















                                                                                3












                                                                                $begingroup$


                                                                                Gaia, 3 bytes



                                                                                ṅD⌡


                                                                                Try it online!



                                                                                Rather slow for large inputs, but works given enough memory/time.



                                                                                I'm not sure why D⌡ implicitly pushes z again, but it makes this a remarkably short answer!



                                                                                ṅ	| implicit input z: push first z prime numbers, call it P
                                                                                D⌡ | take the absolute difference between P and (implicit) z,
                                                                                | returning the smallest value in P with the minimum absolute difference





                                                                                share|improve this answer











                                                                                $endgroup$
















                                                                                  3












                                                                                  3








                                                                                  3





                                                                                  $begingroup$


                                                                                  Gaia, 3 bytes



                                                                                  ṅD⌡


                                                                                  Try it online!



                                                                                  Rather slow for large inputs, but works given enough memory/time.



                                                                                  I'm not sure why D⌡ implicitly pushes z again, but it makes this a remarkably short answer!



                                                                                  ṅ	| implicit input z: push first z prime numbers, call it P
                                                                                  D⌡ | take the absolute difference between P and (implicit) z,
                                                                                  | returning the smallest value in P with the minimum absolute difference





                                                                                  share|improve this answer











                                                                                  $endgroup$




                                                                                  Gaia, 3 bytes



                                                                                  ṅD⌡


                                                                                  Try it online!



                                                                                  Rather slow for large inputs, but works given enough memory/time.



                                                                                  I'm not sure why D⌡ implicitly pushes z again, but it makes this a remarkably short answer!



                                                                                  ṅ	| implicit input z: push first z prime numbers, call it P
                                                                                  D⌡ | take the absolute difference between P and (implicit) z,
                                                                                  | returning the smallest value in P with the minimum absolute difference






                                                                                  share|improve this answer














                                                                                  share|improve this answer



                                                                                  share|improve this answer








                                                                                  edited 9 hours ago

























                                                                                  answered 9 hours ago









                                                                                  GiuseppeGiuseppe

                                                                                  17.2k31152




                                                                                  17.2k31152























                                                                                      2












                                                                                      $begingroup$


                                                                                      Wolfram Language (Mathematica), 53 bytes



                                                                                      If[PrimeQ[s=#],s,#&@@Nearest[s~NextPrime~{-1, 1},s]]&


                                                                                      Try it online!






                                                                                      share|improve this answer









                                                                                      $endgroup$


















                                                                                        2












                                                                                        $begingroup$


                                                                                        Wolfram Language (Mathematica), 53 bytes



                                                                                        If[PrimeQ[s=#],s,#&@@Nearest[s~NextPrime~{-1, 1},s]]&


                                                                                        Try it online!






                                                                                        share|improve this answer









                                                                                        $endgroup$
















                                                                                          2












                                                                                          2








                                                                                          2





                                                                                          $begingroup$


                                                                                          Wolfram Language (Mathematica), 53 bytes



                                                                                          If[PrimeQ[s=#],s,#&@@Nearest[s~NextPrime~{-1, 1},s]]&


                                                                                          Try it online!






                                                                                          share|improve this answer









                                                                                          $endgroup$




                                                                                          Wolfram Language (Mathematica), 53 bytes



                                                                                          If[PrimeQ[s=#],s,#&@@Nearest[s~NextPrime~{-1, 1},s]]&


                                                                                          Try it online!







                                                                                          share|improve this answer












                                                                                          share|improve this answer



                                                                                          share|improve this answer










                                                                                          answered 14 hours ago









                                                                                          J42161217J42161217

                                                                                          13.6k21252




                                                                                          13.6k21252























                                                                                              2












                                                                                              $begingroup$


                                                                                              Python 2, 96 bytes





                                                                                              l=lambda p:min(filter(lambda p:all(p%n for n in range(2,p)),range(2,p*2)),key=lambda x:abs(x-p))


                                                                                              Try it online!






                                                                                              share|improve this answer









                                                                                              $endgroup$









                                                                                              • 2




                                                                                                $begingroup$
                                                                                                This seems to fail for $n=1$.
                                                                                                $endgroup$
                                                                                                – Arnauld
                                                                                                13 hours ago
















                                                                                              2












                                                                                              $begingroup$


                                                                                              Python 2, 96 bytes





                                                                                              l=lambda p:min(filter(lambda p:all(p%n for n in range(2,p)),range(2,p*2)),key=lambda x:abs(x-p))


                                                                                              Try it online!






                                                                                              share|improve this answer









                                                                                              $endgroup$









                                                                                              • 2




                                                                                                $begingroup$
                                                                                                This seems to fail for $n=1$.
                                                                                                $endgroup$
                                                                                                – Arnauld
                                                                                                13 hours ago














                                                                                              2












                                                                                              2








                                                                                              2





                                                                                              $begingroup$


                                                                                              Python 2, 96 bytes





                                                                                              l=lambda p:min(filter(lambda p:all(p%n for n in range(2,p)),range(2,p*2)),key=lambda x:abs(x-p))


                                                                                              Try it online!






                                                                                              share|improve this answer









                                                                                              $endgroup$




                                                                                              Python 2, 96 bytes





                                                                                              l=lambda p:min(filter(lambda p:all(p%n for n in range(2,p)),range(2,p*2)),key=lambda x:abs(x-p))


                                                                                              Try it online!







                                                                                              share|improve this answer












                                                                                              share|improve this answer



                                                                                              share|improve this answer










                                                                                              answered 13 hours ago









                                                                                              Snaddyvitch DispenserSnaddyvitch Dispenser

                                                                                              1015




                                                                                              1015








                                                                                              • 2




                                                                                                $begingroup$
                                                                                                This seems to fail for $n=1$.
                                                                                                $endgroup$
                                                                                                – Arnauld
                                                                                                13 hours ago














                                                                                              • 2




                                                                                                $begingroup$
                                                                                                This seems to fail for $n=1$.
                                                                                                $endgroup$
                                                                                                – Arnauld
                                                                                                13 hours ago








                                                                                              2




                                                                                              2




                                                                                              $begingroup$
                                                                                              This seems to fail for $n=1$.
                                                                                              $endgroup$
                                                                                              – Arnauld
                                                                                              13 hours ago




                                                                                              $begingroup$
                                                                                              This seems to fail for $n=1$.
                                                                                              $endgroup$
                                                                                              – Arnauld
                                                                                              13 hours ago











                                                                                              2












                                                                                              $begingroup$


                                                                                              VDM-SL, 161 bytes





                                                                                              f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                                                                                              A full program to run might look like this - it's worth noting that the bounds of the set of primes used should probably be changed if you actually want to run this, since it will take a long time to run for 1 million:



                                                                                              functions
                                                                                              f:nat1+>nat1
                                                                                              f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                                                                                              Explanation:



                                                                                              f(i)==                                        /* f is a function which takes a nat1 (natural number not including 0)*/
                                                                                              (lambda p:set of nat1 /* define a lambda which takes a set of nat1*/
                                                                                              &let z in set p be st /* which has an element z in the set such that */
                                                                                              forall m in set p /* for every element in the set*/
                                                                                              &abs(m-i) /* the difference between the element m and the input*/
                                                                                              >=abs(z-i) /* is greater than or equal to the difference between the element z and the input */
                                                                                              in z) /* and return z from the lambda */
                                                                                              ( /* apply this lambda to... */
                                                                                              { /* a set defined by comprehension as.. */
                                                                                              x| /* all elements x such that.. */
                                                                                              x in set{1,...,9**7} /* x is between 1 and 9^7 */
                                                                                              &forall y in set{2,...,1003} /* and for all values between 2 and 1003*/
                                                                                              &y<>x=>x mod y<>0 /* y is not x implies x is not divisible by y*/
                                                                                              }
                                                                                              )





                                                                                              share|improve this answer









                                                                                              $endgroup$


















                                                                                                2












                                                                                                $begingroup$


                                                                                                VDM-SL, 161 bytes





                                                                                                f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                                                                                                A full program to run might look like this - it's worth noting that the bounds of the set of primes used should probably be changed if you actually want to run this, since it will take a long time to run for 1 million:



                                                                                                functions
                                                                                                f:nat1+>nat1
                                                                                                f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                                                                                                Explanation:



                                                                                                f(i)==                                        /* f is a function which takes a nat1 (natural number not including 0)*/
                                                                                                (lambda p:set of nat1 /* define a lambda which takes a set of nat1*/
                                                                                                &let z in set p be st /* which has an element z in the set such that */
                                                                                                forall m in set p /* for every element in the set*/
                                                                                                &abs(m-i) /* the difference between the element m and the input*/
                                                                                                >=abs(z-i) /* is greater than or equal to the difference between the element z and the input */
                                                                                                in z) /* and return z from the lambda */
                                                                                                ( /* apply this lambda to... */
                                                                                                { /* a set defined by comprehension as.. */
                                                                                                x| /* all elements x such that.. */
                                                                                                x in set{1,...,9**7} /* x is between 1 and 9^7 */
                                                                                                &forall y in set{2,...,1003} /* and for all values between 2 and 1003*/
                                                                                                &y<>x=>x mod y<>0 /* y is not x implies x is not divisible by y*/
                                                                                                }
                                                                                                )





                                                                                                share|improve this answer









                                                                                                $endgroup$
















                                                                                                  2












                                                                                                  2








                                                                                                  2





                                                                                                  $begingroup$


                                                                                                  VDM-SL, 161 bytes





                                                                                                  f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                                                                                                  A full program to run might look like this - it's worth noting that the bounds of the set of primes used should probably be changed if you actually want to run this, since it will take a long time to run for 1 million:



                                                                                                  functions
                                                                                                  f:nat1+>nat1
                                                                                                  f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                                                                                                  Explanation:



                                                                                                  f(i)==                                        /* f is a function which takes a nat1 (natural number not including 0)*/
                                                                                                  (lambda p:set of nat1 /* define a lambda which takes a set of nat1*/
                                                                                                  &let z in set p be st /* which has an element z in the set such that */
                                                                                                  forall m in set p /* for every element in the set*/
                                                                                                  &abs(m-i) /* the difference between the element m and the input*/
                                                                                                  >=abs(z-i) /* is greater than or equal to the difference between the element z and the input */
                                                                                                  in z) /* and return z from the lambda */
                                                                                                  ( /* apply this lambda to... */
                                                                                                  { /* a set defined by comprehension as.. */
                                                                                                  x| /* all elements x such that.. */
                                                                                                  x in set{1,...,9**7} /* x is between 1 and 9^7 */
                                                                                                  &forall y in set{2,...,1003} /* and for all values between 2 and 1003*/
                                                                                                  &y<>x=>x mod y<>0 /* y is not x implies x is not divisible by y*/
                                                                                                  }
                                                                                                  )





                                                                                                  share|improve this answer









                                                                                                  $endgroup$




                                                                                                  VDM-SL, 161 bytes





                                                                                                  f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                                                                                                  A full program to run might look like this - it's worth noting that the bounds of the set of primes used should probably be changed if you actually want to run this, since it will take a long time to run for 1 million:



                                                                                                  functions
                                                                                                  f:nat1+>nat1
                                                                                                  f(i)==(lambda p:set of nat1&let z in set p be st forall m in set p&abs(m-i)>=abs(z-i)in z)({x|x in set{1,...,9**7}&forall y in set{2,...,1003}&y<>x=>x mod y<>0})


                                                                                                  Explanation:



                                                                                                  f(i)==                                        /* f is a function which takes a nat1 (natural number not including 0)*/
                                                                                                  (lambda p:set of nat1 /* define a lambda which takes a set of nat1*/
                                                                                                  &let z in set p be st /* which has an element z in the set such that */
                                                                                                  forall m in set p /* for every element in the set*/
                                                                                                  &abs(m-i) /* the difference between the element m and the input*/
                                                                                                  >=abs(z-i) /* is greater than or equal to the difference between the element z and the input */
                                                                                                  in z) /* and return z from the lambda */
                                                                                                  ( /* apply this lambda to... */
                                                                                                  { /* a set defined by comprehension as.. */
                                                                                                  x| /* all elements x such that.. */
                                                                                                  x in set{1,...,9**7} /* x is between 1 and 9^7 */
                                                                                                  &forall y in set{2,...,1003} /* and for all values between 2 and 1003*/
                                                                                                  &y<>x=>x mod y<>0 /* y is not x implies x is not divisible by y*/
                                                                                                  }
                                                                                                  )






                                                                                                  share|improve this answer












                                                                                                  share|improve this answer



                                                                                                  share|improve this answer










                                                                                                  answered 13 hours ago









                                                                                                  Expired DataExpired Data

                                                                                                  3686




                                                                                                  3686























                                                                                                      2












                                                                                                      $begingroup$

                                                                                                      APL(NARS), 38 chars, 76 bytes



                                                                                                      {⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}


                                                                                                      0π is the test for prime, ¯1π the prev prime, 1π is the next prime; test:



                                                                                                        f←{⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}
                                                                                                      f¨80 100 5 9 532 1
                                                                                                      79 101 5 7 523 2





                                                                                                      share|improve this answer









                                                                                                      $endgroup$


















                                                                                                        2












                                                                                                        $begingroup$

                                                                                                        APL(NARS), 38 chars, 76 bytes



                                                                                                        {⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}


                                                                                                        0π is the test for prime, ¯1π the prev prime, 1π is the next prime; test:



                                                                                                          f←{⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}
                                                                                                        f¨80 100 5 9 532 1
                                                                                                        79 101 5 7 523 2





                                                                                                        share|improve this answer









                                                                                                        $endgroup$
















                                                                                                          2












                                                                                                          2








                                                                                                          2





                                                                                                          $begingroup$

                                                                                                          APL(NARS), 38 chars, 76 bytes



                                                                                                          {⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}


                                                                                                          0π is the test for prime, ¯1π the prev prime, 1π is the next prime; test:



                                                                                                            f←{⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}
                                                                                                          f¨80 100 5 9 532 1
                                                                                                          79 101 5 7 523 2





                                                                                                          share|improve this answer









                                                                                                          $endgroup$



                                                                                                          APL(NARS), 38 chars, 76 bytes



                                                                                                          {⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}


                                                                                                          0π is the test for prime, ¯1π the prev prime, 1π is the next prime; test:



                                                                                                            f←{⍵≤1:2⋄0π⍵:⍵⋄d←1π⍵⋄(d-⍵)≥⍵-s←¯1π⍵:s⋄d}
                                                                                                          f¨80 100 5 9 532 1
                                                                                                          79 101 5 7 523 2






                                                                                                          share|improve this answer












                                                                                                          share|improve this answer



                                                                                                          share|improve this answer










                                                                                                          answered 12 hours ago









                                                                                                          RosLuPRosLuP

                                                                                                          2,296514




                                                                                                          2,296514























                                                                                                              2












                                                                                                              $begingroup$


                                                                                                              Jelly, 9 7 bytes



                                                                                                              ḤÆRạÞµḢ


                                                                                                              Try it online!



                                                                                                              Slow for larger input, but works ok for the requested range. Thanks to @EriktheOutgolfer for saving 2 bytes!






                                                                                                              share|improve this answer











                                                                                                              $endgroup$













                                                                                                              • $begingroup$
                                                                                                                Hey, that's clever! Save two by substituting _A¥ with (absolute difference). Oh, and can really be .
                                                                                                                $endgroup$
                                                                                                                – Erik the Outgolfer
                                                                                                                11 hours ago












                                                                                                              • $begingroup$
                                                                                                                @EriktheOutgolfer thanks. Surely using won’t always work? It means that only primes up to n+1 will be found, while the closest might be n+2.
                                                                                                                $endgroup$
                                                                                                                – Nick Kennedy
                                                                                                                7 hours ago










                                                                                                              • $begingroup$
                                                                                                                Hm, that's a concern.
                                                                                                                $endgroup$
                                                                                                                – Erik the Outgolfer
                                                                                                                7 hours ago


















                                                                                                              2












                                                                                                              $begingroup$


                                                                                                              Jelly, 9 7 bytes



                                                                                                              ḤÆRạÞµḢ


                                                                                                              Try it online!



                                                                                                              Slow for larger input, but works ok for the requested range. Thanks to @EriktheOutgolfer for saving 2 bytes!






                                                                                                              share|improve this answer











                                                                                                              $endgroup$













                                                                                                              • $begingroup$
                                                                                                                Hey, that's clever! Save two by substituting _A¥ with (absolute difference). Oh, and can really be .
                                                                                                                $endgroup$
                                                                                                                – Erik the Outgolfer
                                                                                                                11 hours ago












                                                                                                              • $begingroup$
                                                                                                                @EriktheOutgolfer thanks. Surely using won’t always work? It means that only primes up to n+1 will be found, while the closest might be n+2.
                                                                                                                $endgroup$
                                                                                                                – Nick Kennedy
                                                                                                                7 hours ago










                                                                                                              • $begingroup$
                                                                                                                Hm, that's a concern.
                                                                                                                $endgroup$
                                                                                                                – Erik the Outgolfer
                                                                                                                7 hours ago
















                                                                                                              2












                                                                                                              2








                                                                                                              2





                                                                                                              $begingroup$


                                                                                                              Jelly, 9 7 bytes



                                                                                                              ḤÆRạÞµḢ


                                                                                                              Try it online!



                                                                                                              Slow for larger input, but works ok for the requested range. Thanks to @EriktheOutgolfer for saving 2 bytes!






                                                                                                              share|improve this answer











                                                                                                              $endgroup$




                                                                                                              Jelly, 9 7 bytes



                                                                                                              ḤÆRạÞµḢ


                                                                                                              Try it online!



                                                                                                              Slow for larger input, but works ok for the requested range. Thanks to @EriktheOutgolfer for saving 2 bytes!







                                                                                                              share|improve this answer














                                                                                                              share|improve this answer



                                                                                                              share|improve this answer








                                                                                                              edited 11 hours ago

























                                                                                                              answered 11 hours ago









                                                                                                              Nick KennedyNick Kennedy

                                                                                                              1,04648




                                                                                                              1,04648












                                                                                                              • $begingroup$
                                                                                                                Hey, that's clever! Save two by substituting _A¥ with (absolute difference). Oh, and can really be .
                                                                                                                $endgroup$
                                                                                                                – Erik the Outgolfer
                                                                                                                11 hours ago












                                                                                                              • $begingroup$
                                                                                                                @EriktheOutgolfer thanks. Surely using won’t always work? It means that only primes up to n+1 will be found, while the closest might be n+2.
                                                                                                                $endgroup$
                                                                                                                – Nick Kennedy
                                                                                                                7 hours ago










                                                                                                              • $begingroup$
                                                                                                                Hm, that's a concern.
                                                                                                                $endgroup$
                                                                                                                – Erik the Outgolfer
                                                                                                                7 hours ago




















                                                                                                              • $begingroup$
                                                                                                                Hey, that's clever! Save two by substituting _A¥ with (absolute difference). Oh, and can really be .
                                                                                                                $endgroup$
                                                                                                                – Erik the Outgolfer
                                                                                                                11 hours ago












                                                                                                              • $begingroup$
                                                                                                                @EriktheOutgolfer thanks. Surely using won’t always work? It means that only primes up to n+1 will be found, while the closest might be n+2.
                                                                                                                $endgroup$
                                                                                                                – Nick Kennedy
                                                                                                                7 hours ago










                                                                                                              • $begingroup$
                                                                                                                Hm, that's a concern.
                                                                                                                $endgroup$
                                                                                                                – Erik the Outgolfer
                                                                                                                7 hours ago


















                                                                                                              $begingroup$
                                                                                                              Hey, that's clever! Save two by substituting _A¥ with (absolute difference). Oh, and can really be .
                                                                                                              $endgroup$
                                                                                                              – Erik the Outgolfer
                                                                                                              11 hours ago






                                                                                                              $begingroup$
                                                                                                              Hey, that's clever! Save two by substituting _A¥ with (absolute difference). Oh, and can really be .
                                                                                                              $endgroup$
                                                                                                              – Erik the Outgolfer
                                                                                                              11 hours ago














                                                                                                              $begingroup$
                                                                                                              @EriktheOutgolfer thanks. Surely using won’t always work? It means that only primes up to n+1 will be found, while the closest might be n+2.
                                                                                                              $endgroup$
                                                                                                              – Nick Kennedy
                                                                                                              7 hours ago




                                                                                                              $begingroup$
                                                                                                              @EriktheOutgolfer thanks. Surely using won’t always work? It means that only primes up to n+1 will be found, while the closest might be n+2.
                                                                                                              $endgroup$
                                                                                                              – Nick Kennedy
                                                                                                              7 hours ago












                                                                                                              $begingroup$
                                                                                                              Hm, that's a concern.
                                                                                                              $endgroup$
                                                                                                              – Erik the Outgolfer
                                                                                                              7 hours ago






                                                                                                              $begingroup$
                                                                                                              Hm, that's a concern.
                                                                                                              $endgroup$
                                                                                                              – Erik the Outgolfer
                                                                                                              7 hours ago













                                                                                                              2












                                                                                                              $begingroup$


                                                                                                              Tidy, 43 bytes



                                                                                                              {x:(prime↦splice(]x,-1,-∞],[x,∞]))@0}


                                                                                                              Try it online!



                                                                                                              Explanation



                                                                                                              This is a lambda with parameter x. This works by creating the following sequence:



                                                                                                              [x - 1, x, x - 2, x + 1, x - 3, x + 2, x - 4, x + 3, ...]


                                                                                                              This is splicing together the two sequences ]x, -1, -∞] (left-closed, right-open) and [x, ∞] (both open).



                                                                                                              For x = 80, this looks like:



                                                                                                              [79, 80, 78, 81, 77, 82, 76, 83, 75, 84, 74, 85, ...]


                                                                                                              Then, we use f↦s to select all elements from s satisfying f. In this case, we filter out all composite numbers, leaving only the prime ones. For the same x, this becomes:



                                                                                                              [79, 83, 73, 71, 89, 67, 97, 61, 59, 101, 103, 53, ...]


                                                                                                              Then, we use (...)@0 to select the first member of this sequence. Since the lower of the two needs to be selected, the sequence which starts with x - 1 is spliced in first.



                                                                                                              Note: Only one of x and x - 1 can be prime, so it is okay that the spliced sequence starts with x - 1. Though the sequence could be open on both sides ([x,-1,-∞]), this would needlessly include x twice in the sequence. So, for sake of "efficiency", I chose the left-closed version (also because I like to show off Tidy).






                                                                                                              share|improve this answer









                                                                                                              $endgroup$


















                                                                                                                2












                                                                                                                $begingroup$


                                                                                                                Tidy, 43 bytes



                                                                                                                {x:(prime↦splice(]x,-1,-∞],[x,∞]))@0}


                                                                                                                Try it online!



                                                                                                                Explanation



                                                                                                                This is a lambda with parameter x. This works by creating the following sequence:



                                                                                                                [x - 1, x, x - 2, x + 1, x - 3, x + 2, x - 4, x + 3, ...]


                                                                                                                This is splicing together the two sequences ]x, -1, -∞] (left-closed, right-open) and [x, ∞] (both open).



                                                                                                                For x = 80, this looks like:



                                                                                                                [79, 80, 78, 81, 77, 82, 76, 83, 75, 84, 74, 85, ...]


                                                                                                                Then, we use f↦s to select all elements from s satisfying f. In this case, we filter out all composite numbers, leaving only the prime ones. For the same x, this becomes:



                                                                                                                [79, 83, 73, 71, 89, 67, 97, 61, 59, 101, 103, 53, ...]


                                                                                                                Then, we use (...)@0 to select the first member of this sequence. Since the lower of the two needs to be selected, the sequence which starts with x - 1 is spliced in first.



                                                                                                                Note: Only one of x and x - 1 can be prime, so it is okay that the spliced sequence starts with x - 1. Though the sequence could be open on both sides ([x,-1,-∞]), this would needlessly include x twice in the sequence. So, for sake of "efficiency", I chose the left-closed version (also because I like to show off Tidy).






                                                                                                                share|improve this answer









                                                                                                                $endgroup$
















                                                                                                                  2












                                                                                                                  2








                                                                                                                  2





                                                                                                                  $begingroup$


                                                                                                                  Tidy, 43 bytes



                                                                                                                  {x:(prime↦splice(]x,-1,-∞],[x,∞]))@0}


                                                                                                                  Try it online!



                                                                                                                  Explanation



                                                                                                                  This is a lambda with parameter x. This works by creating the following sequence:



                                                                                                                  [x - 1, x, x - 2, x + 1, x - 3, x + 2, x - 4, x + 3, ...]


                                                                                                                  This is splicing together the two sequences ]x, -1, -∞] (left-closed, right-open) and [x, ∞] (both open).



                                                                                                                  For x = 80, this looks like:



                                                                                                                  [79, 80, 78, 81, 77, 82, 76, 83, 75, 84, 74, 85, ...]


                                                                                                                  Then, we use f↦s to select all elements from s satisfying f. In this case, we filter out all composite numbers, leaving only the prime ones. For the same x, this becomes:



                                                                                                                  [79, 83, 73, 71, 89, 67, 97, 61, 59, 101, 103, 53, ...]


                                                                                                                  Then, we use (...)@0 to select the first member of this sequence. Since the lower of the two needs to be selected, the sequence which starts with x - 1 is spliced in first.



                                                                                                                  Note: Only one of x and x - 1 can be prime, so it is okay that the spliced sequence starts with x - 1. Though the sequence could be open on both sides ([x,-1,-∞]), this would needlessly include x twice in the sequence. So, for sake of "efficiency", I chose the left-closed version (also because I like to show off Tidy).






                                                                                                                  share|improve this answer









                                                                                                                  $endgroup$




                                                                                                                  Tidy, 43 bytes



                                                                                                                  {x:(prime↦splice(]x,-1,-∞],[x,∞]))@0}


                                                                                                                  Try it online!



                                                                                                                  Explanation



                                                                                                                  This is a lambda with parameter x. This works by creating the following sequence:



                                                                                                                  [x - 1, x, x - 2, x + 1, x - 3, x + 2, x - 4, x + 3, ...]


                                                                                                                  This is splicing together the two sequences ]x, -1, -∞] (left-closed, right-open) and [x, ∞] (both open).



                                                                                                                  For x = 80, this looks like:



                                                                                                                  [79, 80, 78, 81, 77, 82, 76, 83, 75, 84, 74, 85, ...]


                                                                                                                  Then, we use f↦s to select all elements from s satisfying f. In this case, we filter out all composite numbers, leaving only the prime ones. For the same x, this becomes:



                                                                                                                  [79, 83, 73, 71, 89, 67, 97, 61, 59, 101, 103, 53, ...]


                                                                                                                  Then, we use (...)@0 to select the first member of this sequence. Since the lower of the two needs to be selected, the sequence which starts with x - 1 is spliced in first.



                                                                                                                  Note: Only one of x and x - 1 can be prime, so it is okay that the spliced sequence starts with x - 1. Though the sequence could be open on both sides ([x,-1,-∞]), this would needlessly include x twice in the sequence. So, for sake of "efficiency", I chose the left-closed version (also because I like to show off Tidy).







                                                                                                                  share|improve this answer












                                                                                                                  share|improve this answer



                                                                                                                  share|improve this answer










                                                                                                                  answered 8 hours ago









                                                                                                                  Conor O'BrienConor O'Brien

                                                                                                                  30.5k264162




                                                                                                                  30.5k264162























                                                                                                                      2












                                                                                                                      $begingroup$


                                                                                                                      Python 2, 71 bytes





                                                                                                                      f=lambda n,k=1,p=1:k<n*3and min(k+n-p%k*2*n,f(n,k+1,p*k*k)-n,key=abs)+n


                                                                                                                      Try it online!



                                                                                                                      A recursive function that uses the Wilson's Theorem prime generator. The product p tracks $(k-1)!^2$, and p%k is 1 for primes and 0 for non-primes. To make it easy to compare abs(k-n) for different primes k, we store k-n and compare via abs, adding back n to get the result k.



                                                                                                                      The expression k+n-p%k*2*n is designed to give k-n on primes (where p%k=1), and otherwise a "bad" value of k+n that's always bigger in absolute value and so doesn't affect the minimum, so that non-primes are passed over.






                                                                                                                      share|improve this answer









                                                                                                                      $endgroup$


















                                                                                                                        2












                                                                                                                        $begingroup$


                                                                                                                        Python 2, 71 bytes





                                                                                                                        f=lambda n,k=1,p=1:k<n*3and min(k+n-p%k*2*n,f(n,k+1,p*k*k)-n,key=abs)+n


                                                                                                                        Try it online!



                                                                                                                        A recursive function that uses the Wilson's Theorem prime generator. The product p tracks $(k-1)!^2$, and p%k is 1 for primes and 0 for non-primes. To make it easy to compare abs(k-n) for different primes k, we store k-n and compare via abs, adding back n to get the result k.



                                                                                                                        The expression k+n-p%k*2*n is designed to give k-n on primes (where p%k=1), and otherwise a "bad" value of k+n that's always bigger in absolute value and so doesn't affect the minimum, so that non-primes are passed over.






                                                                                                                        share|improve this answer









                                                                                                                        $endgroup$
















                                                                                                                          2












                                                                                                                          2








                                                                                                                          2





                                                                                                                          $begingroup$


                                                                                                                          Python 2, 71 bytes





                                                                                                                          f=lambda n,k=1,p=1:k<n*3and min(k+n-p%k*2*n,f(n,k+1,p*k*k)-n,key=abs)+n


                                                                                                                          Try it online!



                                                                                                                          A recursive function that uses the Wilson's Theorem prime generator. The product p tracks $(k-1)!^2$, and p%k is 1 for primes and 0 for non-primes. To make it easy to compare abs(k-n) for different primes k, we store k-n and compare via abs, adding back n to get the result k.



                                                                                                                          The expression k+n-p%k*2*n is designed to give k-n on primes (where p%k=1), and otherwise a "bad" value of k+n that's always bigger in absolute value and so doesn't affect the minimum, so that non-primes are passed over.






                                                                                                                          share|improve this answer









                                                                                                                          $endgroup$




                                                                                                                          Python 2, 71 bytes





                                                                                                                          f=lambda n,k=1,p=1:k<n*3and min(k+n-p%k*2*n,f(n,k+1,p*k*k)-n,key=abs)+n


                                                                                                                          Try it online!



                                                                                                                          A recursive function that uses the Wilson's Theorem prime generator. The product p tracks $(k-1)!^2$, and p%k is 1 for primes and 0 for non-primes. To make it easy to compare abs(k-n) for different primes k, we store k-n and compare via abs, adding back n to get the result k.



                                                                                                                          The expression k+n-p%k*2*n is designed to give k-n on primes (where p%k=1), and otherwise a "bad" value of k+n that's always bigger in absolute value and so doesn't affect the minimum, so that non-primes are passed over.







                                                                                                                          share|improve this answer












                                                                                                                          share|improve this answer



                                                                                                                          share|improve this answer










                                                                                                                          answered 6 hours ago









                                                                                                                          xnorxnor

                                                                                                                          93.1k18190448




                                                                                                                          93.1k18190448























                                                                                                                              1












                                                                                                                              $begingroup$


                                                                                                                              C# (Visual C# Interactive Compiler), 112 bytes





                                                                                                                              g=>Enumerable.Range(2,2<<20).Where(x=>Enumerable.Range(1,x).Count(y=>x%y<1)<3).OrderBy(x=>Math.Abs(x-g)).First()


                                                                                                                              Try it online!



                                                                                                                              Left shifts by 20 in submission but 10 in TIO so that TIO terminates for test cases.






                                                                                                                              share|improve this answer









                                                                                                                              $endgroup$


















                                                                                                                                1












                                                                                                                                $begingroup$


                                                                                                                                C# (Visual C# Interactive Compiler), 112 bytes





                                                                                                                                g=>Enumerable.Range(2,2<<20).Where(x=>Enumerable.Range(1,x).Count(y=>x%y<1)<3).OrderBy(x=>Math.Abs(x-g)).First()


                                                                                                                                Try it online!



                                                                                                                                Left shifts by 20 in submission but 10 in TIO so that TIO terminates for test cases.






                                                                                                                                share|improve this answer









                                                                                                                                $endgroup$
















                                                                                                                                  1












                                                                                                                                  1








                                                                                                                                  1





                                                                                                                                  $begingroup$


                                                                                                                                  C# (Visual C# Interactive Compiler), 112 bytes





                                                                                                                                  g=>Enumerable.Range(2,2<<20).Where(x=>Enumerable.Range(1,x).Count(y=>x%y<1)<3).OrderBy(x=>Math.Abs(x-g)).First()


                                                                                                                                  Try it online!



                                                                                                                                  Left shifts by 20 in submission but 10 in TIO so that TIO terminates for test cases.






                                                                                                                                  share|improve this answer









                                                                                                                                  $endgroup$




                                                                                                                                  C# (Visual C# Interactive Compiler), 112 bytes





                                                                                                                                  g=>Enumerable.Range(2,2<<20).Where(x=>Enumerable.Range(1,x).Count(y=>x%y<1)<3).OrderBy(x=>Math.Abs(x-g)).First()


                                                                                                                                  Try it online!



                                                                                                                                  Left shifts by 20 in submission but 10 in TIO so that TIO terminates for test cases.







                                                                                                                                  share|improve this answer












                                                                                                                                  share|improve this answer



                                                                                                                                  share|improve this answer










                                                                                                                                  answered 13 hours ago









                                                                                                                                  Expired DataExpired Data

                                                                                                                                  3686




                                                                                                                                  3686























                                                                                                                                      1












                                                                                                                                      $begingroup$


                                                                                                                                      APL (Dyalog Extended), 20 bytesSBCS





                                                                                                                                      ⊢(⊃>/⍤|⍤-⌽⊢)¯4 4⍭3⌈⊢


                                                                                                                                      Try it online!



                                                                                                                                       the argument



                                                                                                                                      3⌈ max of 3 and that



                                                                                                                                      ¯4 4⍭ the previous and next primes`



                                                                                                                                      ⊢() apply the following infix tacit function to that, with the original argument as left argument:



                                                                                                                                       the primes



                                                                                                                                       … cyclically rotate them the following number of steps:



                                                                                                                                        - the original argument minus the primes

                                                                                                                                         then

                                                                                                                                        | absolute value of that

                                                                                                                                         then

                                                                                                                                        >/ Boolean (0/1) whether the left is greater than the right (i.e. 1 if next is closer)



                                                                                                                                       pick the first one (i.e. previous if previous is closer and next if next is closer)






                                                                                                                                      share|improve this answer









                                                                                                                                      $endgroup$


















                                                                                                                                        1












                                                                                                                                        $begingroup$


                                                                                                                                        APL (Dyalog Extended), 20 bytesSBCS





                                                                                                                                        ⊢(⊃>/⍤|⍤-⌽⊢)¯4 4⍭3⌈⊢


                                                                                                                                        Try it online!



                                                                                                                                         the argument



                                                                                                                                        3⌈ max of 3 and that



                                                                                                                                        ¯4 4⍭ the previous and next primes`



                                                                                                                                        ⊢() apply the following infix tacit function to that, with the original argument as left argument:



                                                                                                                                         the primes



                                                                                                                                         … cyclically rotate them the following number of steps:



                                                                                                                                          - the original argument minus the primes

                                                                                                                                           then

                                                                                                                                          | absolute value of that

                                                                                                                                           then

                                                                                                                                          >/ Boolean (0/1) whether the left is greater than the right (i.e. 1 if next is closer)



                                                                                                                                         pick the first one (i.e. previous if previous is closer and next if next is closer)






                                                                                                                                        share|improve this answer









                                                                                                                                        $endgroup$
















                                                                                                                                          1












                                                                                                                                          1








                                                                                                                                          1





                                                                                                                                          $begingroup$


                                                                                                                                          APL (Dyalog Extended), 20 bytesSBCS





                                                                                                                                          ⊢(⊃>/⍤|⍤-⌽⊢)¯4 4⍭3⌈⊢


                                                                                                                                          Try it online!



                                                                                                                                           the argument



                                                                                                                                          3⌈ max of 3 and that



                                                                                                                                          ¯4 4⍭ the previous and next primes`



                                                                                                                                          ⊢() apply the following infix tacit function to that, with the original argument as left argument:



                                                                                                                                           the primes



                                                                                                                                           … cyclically rotate them the following number of steps:



                                                                                                                                            - the original argument minus the primes

                                                                                                                                             then

                                                                                                                                            | absolute value of that

                                                                                                                                             then

                                                                                                                                            >/ Boolean (0/1) whether the left is greater than the right (i.e. 1 if next is closer)



                                                                                                                                           pick the first one (i.e. previous if previous is closer and next if next is closer)






                                                                                                                                          share|improve this answer









                                                                                                                                          $endgroup$




                                                                                                                                          APL (Dyalog Extended), 20 bytesSBCS





                                                                                                                                          ⊢(⊃>/⍤|⍤-⌽⊢)¯4 4⍭3⌈⊢


                                                                                                                                          Try it online!



                                                                                                                                           the argument



                                                                                                                                          3⌈ max of 3 and that



                                                                                                                                          ¯4 4⍭ the previous and next primes`



                                                                                                                                          ⊢() apply the following infix tacit function to that, with the original argument as left argument:



                                                                                                                                           the primes



                                                                                                                                           … cyclically rotate them the following number of steps:



                                                                                                                                            - the original argument minus the primes

                                                                                                                                             then

                                                                                                                                            | absolute value of that

                                                                                                                                             then

                                                                                                                                            >/ Boolean (0/1) whether the left is greater than the right (i.e. 1 if next is closer)



                                                                                                                                           pick the first one (i.e. previous if previous is closer and next if next is closer)







                                                                                                                                          share|improve this answer












                                                                                                                                          share|improve this answer



                                                                                                                                          share|improve this answer










                                                                                                                                          answered 12 hours ago









                                                                                                                                          AdámAdám

                                                                                                                                          28.6k276207




                                                                                                                                          28.6k276207























                                                                                                                                              1












                                                                                                                                              $begingroup$

                                                                                                                                              Swift, 186 bytes



                                                                                                                                              func p(a:Int){let b=q(a:a,b:-1),c=q(a:a,b:1);print(a-b<=c-a ? b:c)}
                                                                                                                                              func q(a:Int,b:Int)->Int{var k=max(a,2),c=2;while k>c && c != a/2{if k%c==0{k+=b;c=2}else{c=c==2 ? c+1:c+2}};return k}


                                                                                                                                              Try it online!






                                                                                                                                              share|improve this answer









                                                                                                                                              $endgroup$


















                                                                                                                                                1












                                                                                                                                                $begingroup$

                                                                                                                                                Swift, 186 bytes



                                                                                                                                                func p(a:Int){let b=q(a:a,b:-1),c=q(a:a,b:1);print(a-b<=c-a ? b:c)}
                                                                                                                                                func q(a:Int,b:Int)->Int{var k=max(a,2),c=2;while k>c && c != a/2{if k%c==0{k+=b;c=2}else{c=c==2 ? c+1:c+2}};return k}


                                                                                                                                                Try it online!






                                                                                                                                                share|improve this answer









                                                                                                                                                $endgroup$
















                                                                                                                                                  1












                                                                                                                                                  1








                                                                                                                                                  1





                                                                                                                                                  $begingroup$

                                                                                                                                                  Swift, 186 bytes



                                                                                                                                                  func p(a:Int){let b=q(a:a,b:-1),c=q(a:a,b:1);print(a-b<=c-a ? b:c)}
                                                                                                                                                  func q(a:Int,b:Int)->Int{var k=max(a,2),c=2;while k>c && c != a/2{if k%c==0{k+=b;c=2}else{c=c==2 ? c+1:c+2}};return k}


                                                                                                                                                  Try it online!






                                                                                                                                                  share|improve this answer









                                                                                                                                                  $endgroup$



                                                                                                                                                  Swift, 186 bytes



                                                                                                                                                  func p(a:Int){let b=q(a:a,b:-1),c=q(a:a,b:1);print(a-b<=c-a ? b:c)}
                                                                                                                                                  func q(a:Int,b:Int)->Int{var k=max(a,2),c=2;while k>c && c != a/2{if k%c==0{k+=b;c=2}else{c=c==2 ? c+1:c+2}};return k}


                                                                                                                                                  Try it online!







                                                                                                                                                  share|improve this answer












                                                                                                                                                  share|improve this answer



                                                                                                                                                  share|improve this answer










                                                                                                                                                  answered 11 hours ago









                                                                                                                                                  onnowebonnoweb

                                                                                                                                                  1613




                                                                                                                                                  1613























                                                                                                                                                      1












                                                                                                                                                      $begingroup$


                                                                                                                                                      Jelly, 14 bytes



                                                                                                                                                      ÆpæRÆnạÞƲ2>?2Ḣ


                                                                                                                                                      Try it online!






                                                                                                                                                      share|improve this answer









                                                                                                                                                      $endgroup$


















                                                                                                                                                        1












                                                                                                                                                        $begingroup$


                                                                                                                                                        Jelly, 14 bytes



                                                                                                                                                        ÆpæRÆnạÞƲ2>?2Ḣ


                                                                                                                                                        Try it online!






                                                                                                                                                        share|improve this answer









                                                                                                                                                        $endgroup$
















                                                                                                                                                          1












                                                                                                                                                          1








                                                                                                                                                          1





                                                                                                                                                          $begingroup$


                                                                                                                                                          Jelly, 14 bytes



                                                                                                                                                          ÆpæRÆnạÞƲ2>?2Ḣ


                                                                                                                                                          Try it online!






                                                                                                                                                          share|improve this answer









                                                                                                                                                          $endgroup$




                                                                                                                                                          Jelly, 14 bytes



                                                                                                                                                          ÆpæRÆnạÞƲ2>?2Ḣ


                                                                                                                                                          Try it online!







                                                                                                                                                          share|improve this answer












                                                                                                                                                          share|improve this answer



                                                                                                                                                          share|improve this answer










                                                                                                                                                          answered 11 hours ago









                                                                                                                                                          Erik the OutgolferErik the Outgolfer

                                                                                                                                                          32.9k429106




                                                                                                                                                          32.9k429106























                                                                                                                                                              1












                                                                                                                                                              $begingroup$


                                                                                                                                                              C# (Visual C# Interactive Compiler), 96 bytes





                                                                                                                                                              n=>{for(int i=0,j;;)if((j=n+i/2*(i++%2*2-1))>1&&Enumerable.Range(2,j-2).All(d=>j%d>0))return j;}


                                                                                                                                                              Try it online!






                                                                                                                                                              share|improve this answer









                                                                                                                                                              $endgroup$


















                                                                                                                                                                1












                                                                                                                                                                $begingroup$


                                                                                                                                                                C# (Visual C# Interactive Compiler), 96 bytes





                                                                                                                                                                n=>{for(int i=0,j;;)if((j=n+i/2*(i++%2*2-1))>1&&Enumerable.Range(2,j-2).All(d=>j%d>0))return j;}


                                                                                                                                                                Try it online!






                                                                                                                                                                share|improve this answer









                                                                                                                                                                $endgroup$
















                                                                                                                                                                  1












                                                                                                                                                                  1








                                                                                                                                                                  1





                                                                                                                                                                  $begingroup$


                                                                                                                                                                  C# (Visual C# Interactive Compiler), 96 bytes





                                                                                                                                                                  n=>{for(int i=0,j;;)if((j=n+i/2*(i++%2*2-1))>1&&Enumerable.Range(2,j-2).All(d=>j%d>0))return j;}


                                                                                                                                                                  Try it online!






                                                                                                                                                                  share|improve this answer









                                                                                                                                                                  $endgroup$




                                                                                                                                                                  C# (Visual C# Interactive Compiler), 96 bytes





                                                                                                                                                                  n=>{for(int i=0,j;;)if((j=n+i/2*(i++%2*2-1))>1&&Enumerable.Range(2,j-2).All(d=>j%d>0))return j;}


                                                                                                                                                                  Try it online!







                                                                                                                                                                  share|improve this answer












                                                                                                                                                                  share|improve this answer



                                                                                                                                                                  share|improve this answer










                                                                                                                                                                  answered 11 hours ago









                                                                                                                                                                  Embodiment of IgnoranceEmbodiment of Ignorance

                                                                                                                                                                  2,218126




                                                                                                                                                                  2,218126























                                                                                                                                                                      1












                                                                                                                                                                      $begingroup$


                                                                                                                                                                      Zsh, 101 92 91 bytes



                                                                                                                                                                      -9 by collapsing the body into the head of p's loop, -1 from using i=j instead of i=$1 in main loop.





                                                                                                                                                                      p(){for ((n=2;n<$1&&$1%n++;)):
                                                                                                                                                                      (($1==n))&&<<<$1}
                                                                                                                                                                      j=$1
                                                                                                                                                                      for ((i=j;;++j&&--i))p $i||p $j&&exit


                                                                                                                                                                      Try it online!



                                                                                                                                                                      Try it online!



                                                                                                                                                                      57 48 bytes to the prime testing function, 43 42 bytes to the main loop (1 byte to the newline between them):



                                                                                                                                                                      p(){  # prime function: takes one input, outputs via return code
                                                                                                                                                                      for (( n = 2; n < $1 && $1 % n++; )) # divisibility check in loop header
                                                                                                                                                                      : # no-op loop body
                                                                                                                                                                      (( $1 == n )) && # if we looped up to $1:
                                                                                                                                                                      <<< $1 # echo out $1. Otherwise, this will return false
                                                                                                                                                                      }


                                                                                                                                                                      For the last condition, we can't use the shorter (($1-n))||, because we need to return false to the main loop if we didn't find a prime. We print in the function to avoid complexity in the main loop.



                                                                                                                                                                      j=$1                          # set i = j = $1. Doing one in and one out is smallest
                                                                                                                                                                      for (( i = j; ; ++j && --i )) # loop indefinitely, increment and decrement
                                                                                                                                                                      p $i || p $j && exit # if either $i or $j was a prime, exit


                                                                                                                                                                      Conditionals are left-associative, which we take advantage of here. We do test the starting number twice to make the decrement logic simpler.






                                                                                                                                                                      share|improve this answer










                                                                                                                                                                      New contributor




                                                                                                                                                                      GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                                                                      Check out our Code of Conduct.






                                                                                                                                                                      $endgroup$


















                                                                                                                                                                        1












                                                                                                                                                                        $begingroup$


                                                                                                                                                                        Zsh, 101 92 91 bytes



                                                                                                                                                                        -9 by collapsing the body into the head of p's loop, -1 from using i=j instead of i=$1 in main loop.





                                                                                                                                                                        p(){for ((n=2;n<$1&&$1%n++;)):
                                                                                                                                                                        (($1==n))&&<<<$1}
                                                                                                                                                                        j=$1
                                                                                                                                                                        for ((i=j;;++j&&--i))p $i||p $j&&exit


                                                                                                                                                                        Try it online!



                                                                                                                                                                        Try it online!



                                                                                                                                                                        57 48 bytes to the prime testing function, 43 42 bytes to the main loop (1 byte to the newline between them):



                                                                                                                                                                        p(){  # prime function: takes one input, outputs via return code
                                                                                                                                                                        for (( n = 2; n < $1 && $1 % n++; )) # divisibility check in loop header
                                                                                                                                                                        : # no-op loop body
                                                                                                                                                                        (( $1 == n )) && # if we looped up to $1:
                                                                                                                                                                        <<< $1 # echo out $1. Otherwise, this will return false
                                                                                                                                                                        }


                                                                                                                                                                        For the last condition, we can't use the shorter (($1-n))||, because we need to return false to the main loop if we didn't find a prime. We print in the function to avoid complexity in the main loop.



                                                                                                                                                                        j=$1                          # set i = j = $1. Doing one in and one out is smallest
                                                                                                                                                                        for (( i = j; ; ++j && --i )) # loop indefinitely, increment and decrement
                                                                                                                                                                        p $i || p $j && exit # if either $i or $j was a prime, exit


                                                                                                                                                                        Conditionals are left-associative, which we take advantage of here. We do test the starting number twice to make the decrement logic simpler.






                                                                                                                                                                        share|improve this answer










                                                                                                                                                                        New contributor




                                                                                                                                                                        GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                                                                        Check out our Code of Conduct.






                                                                                                                                                                        $endgroup$
















                                                                                                                                                                          1












                                                                                                                                                                          1








                                                                                                                                                                          1





                                                                                                                                                                          $begingroup$


                                                                                                                                                                          Zsh, 101 92 91 bytes



                                                                                                                                                                          -9 by collapsing the body into the head of p's loop, -1 from using i=j instead of i=$1 in main loop.





                                                                                                                                                                          p(){for ((n=2;n<$1&&$1%n++;)):
                                                                                                                                                                          (($1==n))&&<<<$1}
                                                                                                                                                                          j=$1
                                                                                                                                                                          for ((i=j;;++j&&--i))p $i||p $j&&exit


                                                                                                                                                                          Try it online!



                                                                                                                                                                          Try it online!



                                                                                                                                                                          57 48 bytes to the prime testing function, 43 42 bytes to the main loop (1 byte to the newline between them):



                                                                                                                                                                          p(){  # prime function: takes one input, outputs via return code
                                                                                                                                                                          for (( n = 2; n < $1 && $1 % n++; )) # divisibility check in loop header
                                                                                                                                                                          : # no-op loop body
                                                                                                                                                                          (( $1 == n )) && # if we looped up to $1:
                                                                                                                                                                          <<< $1 # echo out $1. Otherwise, this will return false
                                                                                                                                                                          }


                                                                                                                                                                          For the last condition, we can't use the shorter (($1-n))||, because we need to return false to the main loop if we didn't find a prime. We print in the function to avoid complexity in the main loop.



                                                                                                                                                                          j=$1                          # set i = j = $1. Doing one in and one out is smallest
                                                                                                                                                                          for (( i = j; ; ++j && --i )) # loop indefinitely, increment and decrement
                                                                                                                                                                          p $i || p $j && exit # if either $i or $j was a prime, exit


                                                                                                                                                                          Conditionals are left-associative, which we take advantage of here. We do test the starting number twice to make the decrement logic simpler.






                                                                                                                                                                          share|improve this answer










                                                                                                                                                                          New contributor




                                                                                                                                                                          GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                                                                          Check out our Code of Conduct.






                                                                                                                                                                          $endgroup$




                                                                                                                                                                          Zsh, 101 92 91 bytes



                                                                                                                                                                          -9 by collapsing the body into the head of p's loop, -1 from using i=j instead of i=$1 in main loop.





                                                                                                                                                                          p(){for ((n=2;n<$1&&$1%n++;)):
                                                                                                                                                                          (($1==n))&&<<<$1}
                                                                                                                                                                          j=$1
                                                                                                                                                                          for ((i=j;;++j&&--i))p $i||p $j&&exit


                                                                                                                                                                          Try it online!



                                                                                                                                                                          Try it online!



                                                                                                                                                                          57 48 bytes to the prime testing function, 43 42 bytes to the main loop (1 byte to the newline between them):



                                                                                                                                                                          p(){  # prime function: takes one input, outputs via return code
                                                                                                                                                                          for (( n = 2; n < $1 && $1 % n++; )) # divisibility check in loop header
                                                                                                                                                                          : # no-op loop body
                                                                                                                                                                          (( $1 == n )) && # if we looped up to $1:
                                                                                                                                                                          <<< $1 # echo out $1. Otherwise, this will return false
                                                                                                                                                                          }


                                                                                                                                                                          For the last condition, we can't use the shorter (($1-n))||, because we need to return false to the main loop if we didn't find a prime. We print in the function to avoid complexity in the main loop.



                                                                                                                                                                          j=$1                          # set i = j = $1. Doing one in and one out is smallest
                                                                                                                                                                          for (( i = j; ; ++j && --i )) # loop indefinitely, increment and decrement
                                                                                                                                                                          p $i || p $j && exit # if either $i or $j was a prime, exit


                                                                                                                                                                          Conditionals are left-associative, which we take advantage of here. We do test the starting number twice to make the decrement logic simpler.







                                                                                                                                                                          share|improve this answer










                                                                                                                                                                          New contributor




                                                                                                                                                                          GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                                                                          Check out our Code of Conduct.









                                                                                                                                                                          share|improve this answer



                                                                                                                                                                          share|improve this answer








                                                                                                                                                                          edited 10 hours ago





















                                                                                                                                                                          New contributor




                                                                                                                                                                          GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                                                                          Check out our Code of Conduct.









                                                                                                                                                                          answered 10 hours ago









                                                                                                                                                                          GammaFunctionGammaFunction

                                                                                                                                                                          1716




                                                                                                                                                                          1716




                                                                                                                                                                          New contributor




                                                                                                                                                                          GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                                                                          Check out our Code of Conduct.





                                                                                                                                                                          New contributor





                                                                                                                                                                          GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                                                                          Check out our Code of Conduct.






                                                                                                                                                                          GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                                                                                                          Check out our Code of Conduct.























                                                                                                                                                                              1












                                                                                                                                                                              $begingroup$

                                                                                                                                                                              C, 122 bytes



                                                                                                                                                                              #define r return
                                                                                                                                                                              p(a,i){if(a<2)r 0;i=1;while(++i<a)if(a%i<1)r 0;r 1;}c(a,b){b=a;while(1){if(p(b))r b;if(p(--a))r a;b++;}}


                                                                                                                                                                              Use it calling function c() and passing as argument the number, it should return the closest prime.






                                                                                                                                                                              share|improve this answer









                                                                                                                                                                              $endgroup$













                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                But c() receives two parameters... Also, you can probably shorten the while(1) to for(;;) (untested, since I don't get how to run your code
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – Embodiment of Ignorance
                                                                                                                                                                                1 hour ago
















                                                                                                                                                                              1












                                                                                                                                                                              $begingroup$

                                                                                                                                                                              C, 122 bytes



                                                                                                                                                                              #define r return
                                                                                                                                                                              p(a,i){if(a<2)r 0;i=1;while(++i<a)if(a%i<1)r 0;r 1;}c(a,b){b=a;while(1){if(p(b))r b;if(p(--a))r a;b++;}}


                                                                                                                                                                              Use it calling function c() and passing as argument the number, it should return the closest prime.






                                                                                                                                                                              share|improve this answer









                                                                                                                                                                              $endgroup$













                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                But c() receives two parameters... Also, you can probably shorten the while(1) to for(;;) (untested, since I don't get how to run your code
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – Embodiment of Ignorance
                                                                                                                                                                                1 hour ago














                                                                                                                                                                              1












                                                                                                                                                                              1








                                                                                                                                                                              1





                                                                                                                                                                              $begingroup$

                                                                                                                                                                              C, 122 bytes



                                                                                                                                                                              #define r return
                                                                                                                                                                              p(a,i){if(a<2)r 0;i=1;while(++i<a)if(a%i<1)r 0;r 1;}c(a,b){b=a;while(1){if(p(b))r b;if(p(--a))r a;b++;}}


                                                                                                                                                                              Use it calling function c() and passing as argument the number, it should return the closest prime.






                                                                                                                                                                              share|improve this answer









                                                                                                                                                                              $endgroup$



                                                                                                                                                                              C, 122 bytes



                                                                                                                                                                              #define r return
                                                                                                                                                                              p(a,i){if(a<2)r 0;i=1;while(++i<a)if(a%i<1)r 0;r 1;}c(a,b){b=a;while(1){if(p(b))r b;if(p(--a))r a;b++;}}


                                                                                                                                                                              Use it calling function c() and passing as argument the number, it should return the closest prime.







                                                                                                                                                                              share|improve this answer












                                                                                                                                                                              share|improve this answer



                                                                                                                                                                              share|improve this answer










                                                                                                                                                                              answered 9 hours ago









                                                                                                                                                                              Lince AssassinoLince Assassino

                                                                                                                                                                              914




                                                                                                                                                                              914












                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                But c() receives two parameters... Also, you can probably shorten the while(1) to for(;;) (untested, since I don't get how to run your code
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – Embodiment of Ignorance
                                                                                                                                                                                1 hour ago


















                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                But c() receives two parameters... Also, you can probably shorten the while(1) to for(;;) (untested, since I don't get how to run your code
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – Embodiment of Ignorance
                                                                                                                                                                                1 hour ago
















                                                                                                                                                                              $begingroup$
                                                                                                                                                                              But c() receives two parameters... Also, you can probably shorten the while(1) to for(;;) (untested, since I don't get how to run your code
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – Embodiment of Ignorance
                                                                                                                                                                              1 hour ago




                                                                                                                                                                              $begingroup$
                                                                                                                                                                              But c() receives two parameters... Also, you can probably shorten the while(1) to for(;;) (untested, since I don't get how to run your code
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – Embodiment of Ignorance
                                                                                                                                                                              1 hour ago











                                                                                                                                                                              1












                                                                                                                                                                              $begingroup$

                                                                                                                                                                              Python 2, 93 bytes





                                                                                                                                                                              lambda n:sorted(range(1,3*n),key=lambda x:abs(x-n)if all(x%k for k in range(2,x))else 2*n)[0]





                                                                                                                                                                              share|improve this answer











                                                                                                                                                                              $endgroup$









                                                                                                                                                                              • 1




                                                                                                                                                                                $begingroup$
                                                                                                                                                                                You don't need the f= in the start
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – Embodiment of Ignorance
                                                                                                                                                                                9 hours ago










                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                @EmbodimentofIgnorance Thanks, fixed that along with the range and non-prime penalty criteria that was causing n=1 to fail
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – deustice
                                                                                                                                                                                9 hours ago






                                                                                                                                                                              • 1




                                                                                                                                                                                $begingroup$
                                                                                                                                                                                The primality check doesn't work for Fermat pseudoprimes such as 341=31*11 which it calls prime.
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – xnor
                                                                                                                                                                                8 hours ago










                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                @xnor Fixed, thanks
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – deustice
                                                                                                                                                                                7 hours ago
















                                                                                                                                                                              1












                                                                                                                                                                              $begingroup$

                                                                                                                                                                              Python 2, 93 bytes





                                                                                                                                                                              lambda n:sorted(range(1,3*n),key=lambda x:abs(x-n)if all(x%k for k in range(2,x))else 2*n)[0]





                                                                                                                                                                              share|improve this answer











                                                                                                                                                                              $endgroup$









                                                                                                                                                                              • 1




                                                                                                                                                                                $begingroup$
                                                                                                                                                                                You don't need the f= in the start
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – Embodiment of Ignorance
                                                                                                                                                                                9 hours ago










                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                @EmbodimentofIgnorance Thanks, fixed that along with the range and non-prime penalty criteria that was causing n=1 to fail
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – deustice
                                                                                                                                                                                9 hours ago






                                                                                                                                                                              • 1




                                                                                                                                                                                $begingroup$
                                                                                                                                                                                The primality check doesn't work for Fermat pseudoprimes such as 341=31*11 which it calls prime.
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – xnor
                                                                                                                                                                                8 hours ago










                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                @xnor Fixed, thanks
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – deustice
                                                                                                                                                                                7 hours ago














                                                                                                                                                                              1












                                                                                                                                                                              1








                                                                                                                                                                              1





                                                                                                                                                                              $begingroup$

                                                                                                                                                                              Python 2, 93 bytes





                                                                                                                                                                              lambda n:sorted(range(1,3*n),key=lambda x:abs(x-n)if all(x%k for k in range(2,x))else 2*n)[0]





                                                                                                                                                                              share|improve this answer











                                                                                                                                                                              $endgroup$



                                                                                                                                                                              Python 2, 93 bytes





                                                                                                                                                                              lambda n:sorted(range(1,3*n),key=lambda x:abs(x-n)if all(x%k for k in range(2,x))else 2*n)[0]






                                                                                                                                                                              share|improve this answer














                                                                                                                                                                              share|improve this answer



                                                                                                                                                                              share|improve this answer








                                                                                                                                                                              edited 7 hours ago

























                                                                                                                                                                              answered 10 hours ago









                                                                                                                                                                              deusticedeustice

                                                                                                                                                                              614




                                                                                                                                                                              614








                                                                                                                                                                              • 1




                                                                                                                                                                                $begingroup$
                                                                                                                                                                                You don't need the f= in the start
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – Embodiment of Ignorance
                                                                                                                                                                                9 hours ago










                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                @EmbodimentofIgnorance Thanks, fixed that along with the range and non-prime penalty criteria that was causing n=1 to fail
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – deustice
                                                                                                                                                                                9 hours ago






                                                                                                                                                                              • 1




                                                                                                                                                                                $begingroup$
                                                                                                                                                                                The primality check doesn't work for Fermat pseudoprimes such as 341=31*11 which it calls prime.
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – xnor
                                                                                                                                                                                8 hours ago










                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                @xnor Fixed, thanks
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – deustice
                                                                                                                                                                                7 hours ago














                                                                                                                                                                              • 1




                                                                                                                                                                                $begingroup$
                                                                                                                                                                                You don't need the f= in the start
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – Embodiment of Ignorance
                                                                                                                                                                                9 hours ago










                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                @EmbodimentofIgnorance Thanks, fixed that along with the range and non-prime penalty criteria that was causing n=1 to fail
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – deustice
                                                                                                                                                                                9 hours ago






                                                                                                                                                                              • 1




                                                                                                                                                                                $begingroup$
                                                                                                                                                                                The primality check doesn't work for Fermat pseudoprimes such as 341=31*11 which it calls prime.
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – xnor
                                                                                                                                                                                8 hours ago










                                                                                                                                                                              • $begingroup$
                                                                                                                                                                                @xnor Fixed, thanks
                                                                                                                                                                                $endgroup$
                                                                                                                                                                                – deustice
                                                                                                                                                                                7 hours ago








                                                                                                                                                                              1




                                                                                                                                                                              1




                                                                                                                                                                              $begingroup$
                                                                                                                                                                              You don't need the f= in the start
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – Embodiment of Ignorance
                                                                                                                                                                              9 hours ago




                                                                                                                                                                              $begingroup$
                                                                                                                                                                              You don't need the f= in the start
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – Embodiment of Ignorance
                                                                                                                                                                              9 hours ago












                                                                                                                                                                              $begingroup$
                                                                                                                                                                              @EmbodimentofIgnorance Thanks, fixed that along with the range and non-prime penalty criteria that was causing n=1 to fail
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – deustice
                                                                                                                                                                              9 hours ago




                                                                                                                                                                              $begingroup$
                                                                                                                                                                              @EmbodimentofIgnorance Thanks, fixed that along with the range and non-prime penalty criteria that was causing n=1 to fail
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – deustice
                                                                                                                                                                              9 hours ago




                                                                                                                                                                              1




                                                                                                                                                                              1




                                                                                                                                                                              $begingroup$
                                                                                                                                                                              The primality check doesn't work for Fermat pseudoprimes such as 341=31*11 which it calls prime.
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – xnor
                                                                                                                                                                              8 hours ago




                                                                                                                                                                              $begingroup$
                                                                                                                                                                              The primality check doesn't work for Fermat pseudoprimes such as 341=31*11 which it calls prime.
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – xnor
                                                                                                                                                                              8 hours ago












                                                                                                                                                                              $begingroup$
                                                                                                                                                                              @xnor Fixed, thanks
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – deustice
                                                                                                                                                                              7 hours ago




                                                                                                                                                                              $begingroup$
                                                                                                                                                                              @xnor Fixed, thanks
                                                                                                                                                                              $endgroup$
                                                                                                                                                                              – deustice
                                                                                                                                                                              7 hours ago











                                                                                                                                                                              1












                                                                                                                                                                              $begingroup$


                                                                                                                                                                              J, 19 15 bytes



                                                                                                                                                                              (0{]/:|@-)p:@i.


                                                                                                                                                                              Try it online!






                                                                                                                                                                              share|improve this answer











                                                                                                                                                                              $endgroup$


















                                                                                                                                                                                1












                                                                                                                                                                                $begingroup$


                                                                                                                                                                                J, 19 15 bytes



                                                                                                                                                                                (0{]/:|@-)p:@i.


                                                                                                                                                                                Try it online!






                                                                                                                                                                                share|improve this answer











                                                                                                                                                                                $endgroup$
















                                                                                                                                                                                  1












                                                                                                                                                                                  1








                                                                                                                                                                                  1





                                                                                                                                                                                  $begingroup$


                                                                                                                                                                                  J, 19 15 bytes



                                                                                                                                                                                  (0{]/:|@-)p:@i.


                                                                                                                                                                                  Try it online!






                                                                                                                                                                                  share|improve this answer











                                                                                                                                                                                  $endgroup$




                                                                                                                                                                                  J, 19 15 bytes



                                                                                                                                                                                  (0{]/:|@-)p:@i.


                                                                                                                                                                                  Try it online!







                                                                                                                                                                                  share|improve this answer














                                                                                                                                                                                  share|improve this answer



                                                                                                                                                                                  share|improve this answer








                                                                                                                                                                                  edited 7 hours ago

























                                                                                                                                                                                  answered 9 hours ago









                                                                                                                                                                                  Galen IvanovGalen Ivanov

                                                                                                                                                                                  7,31211034




                                                                                                                                                                                  7,31211034






























                                                                                                                                                                                      draft saved

                                                                                                                                                                                      draft discarded




















































                                                                                                                                                                                      If this is an answer to a challenge…




                                                                                                                                                                                      • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                                                                                                                                                      • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                                                                                                                                                        Explanations of your answer make it more interesting to read and are very much encouraged.


                                                                                                                                                                                      • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                                                                                                                                                                      More generally…




                                                                                                                                                                                      • …Please make sure to answer the question and provide sufficient detail.


                                                                                                                                                                                      • …Avoid asking for help, clarification or responding to other answers (use comments instead).





                                                                                                                                                                                      draft saved


                                                                                                                                                                                      draft discarded














                                                                                                                                                                                      StackExchange.ready(
                                                                                                                                                                                      function () {
                                                                                                                                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182305%2freturn-the-closest-prime-number%23new-answer', 'question_page');
                                                                                                                                                                                      }
                                                                                                                                                                                      );

                                                                                                                                                                                      Post as a guest















                                                                                                                                                                                      Required, but never shown





















































                                                                                                                                                                                      Required, but never shown














                                                                                                                                                                                      Required, but never shown












                                                                                                                                                                                      Required, but never shown







                                                                                                                                                                                      Required, but never shown

































                                                                                                                                                                                      Required, but never shown














                                                                                                                                                                                      Required, but never shown












                                                                                                                                                                                      Required, but never shown







                                                                                                                                                                                      Required, but never shown







                                                                                                                                                                                      Popular posts from this blog

                                                                                                                                                                                      Contact image not getting when fetch all contact list from iPhone by CNContact

                                                                                                                                                                                      count number of partitions of a set with n elements into k subsets

                                                                                                                                                                                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks