Square Root Distance from IntegersCalculate the square root only using ++Sorted Lexical Partition of a...

Why does 0.-5 evaluate to -5?

Does the ditching switch allow an A320 to float indefinitely?

Where do we learn that students are like offspring?

Has any human ever had the choice to leave Earth permanently?

What senses are available to a corpse subjected to a Speak with Dead spell?

Integration of two exponential multiplied by each other

Cat is tipping over bed-side lamps during the night

Will rerolling initiative each round stop meta-gaming about initiative?

Memory usage: #define vs. static const for uint8_t

Why is the "Domain users" group missing from this PowerShell AD query?

How do you get out of your own psychology to write characters?

How would an AI self awareness kill switch work?

Book where a space ship journeys to the center of the galaxy to find all the stars had gone supernova

The No-Straight Maze

A fantasy book with seven white haired women on the cover

What makes papers publishable in top-tier journals?

Custom shape shows unwanted extra line

How much mayhem could I cause as a fish?

Why avoid shared user accounts?

Why zero tolerance on nudity in space?

Stuck on a Geometry Puzzle

Possible issue with my W4 and tax return

Why did Luke use his left hand to shoot?

Can we "borrow" our answers to populate our own websites?



Square Root Distance from Integers


Calculate the square root only using ++Sorted Lexical Partition of a NumberReverse and squareThe fastest square root calculatorRobbers - square times square rootCops - square times square rootFermat's factorization helperMiller-Rabin Strong PseudoprimesExact change in fewest bills and coinsApproximate My Squares













8












$begingroup$


Given a decimal number k, find the smallest integer n such that the square root of n is within k of an integer. However, the distance should be nonzero - n cannot be a perfect square.



Given k, a decimal number or a fraction (whichever is easier for you), such that 0 < k < 1, output the smallest positive integer n such that the difference between the square root of n and the closest integer to the square root of n is less than or equal to k but nonzero.



If i is the closest integer to the square root of n, you are looking for the first n where 0 < |i - sqrt(n)| <= k.



Rules




  • You cannot use a language's insufficient implementation of non-integer numbers to trivialize the problem.

  • Otherwise, you can assume that k will not cause problems with, for example, floating point rounding.


Test Cases



.9         > 2
.5 > 2
.4 > 3
.3 > 3
.25 > 5
.2 > 8
.1 > 26
.05 > 101
.03 > 288
.01 > 2501
.005 > 10001
.003 > 27888
.001 > 250001
.0005 > 1000001
.0003 > 2778888
.0001 > 25000001
.0314159 > 255
.00314159 > 25599
.000314159 > 2534463


Comma separated test case inputs:



0.9, 0.5, 0.4, 0.3, 0.25, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.0314159, 0.00314159, 0.000314159


This is code-golf, so shortest answer in bytes wins.










share|improve this question











$endgroup$

















    8












    $begingroup$


    Given a decimal number k, find the smallest integer n such that the square root of n is within k of an integer. However, the distance should be nonzero - n cannot be a perfect square.



    Given k, a decimal number or a fraction (whichever is easier for you), such that 0 < k < 1, output the smallest positive integer n such that the difference between the square root of n and the closest integer to the square root of n is less than or equal to k but nonzero.



    If i is the closest integer to the square root of n, you are looking for the first n where 0 < |i - sqrt(n)| <= k.



    Rules




    • You cannot use a language's insufficient implementation of non-integer numbers to trivialize the problem.

    • Otherwise, you can assume that k will not cause problems with, for example, floating point rounding.


    Test Cases



    .9         > 2
    .5 > 2
    .4 > 3
    .3 > 3
    .25 > 5
    .2 > 8
    .1 > 26
    .05 > 101
    .03 > 288
    .01 > 2501
    .005 > 10001
    .003 > 27888
    .001 > 250001
    .0005 > 1000001
    .0003 > 2778888
    .0001 > 25000001
    .0314159 > 255
    .00314159 > 25599
    .000314159 > 2534463


    Comma separated test case inputs:



    0.9, 0.5, 0.4, 0.3, 0.25, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.0314159, 0.00314159, 0.000314159


    This is code-golf, so shortest answer in bytes wins.










    share|improve this question











    $endgroup$















      8












      8








      8





      $begingroup$


      Given a decimal number k, find the smallest integer n such that the square root of n is within k of an integer. However, the distance should be nonzero - n cannot be a perfect square.



      Given k, a decimal number or a fraction (whichever is easier for you), such that 0 < k < 1, output the smallest positive integer n such that the difference between the square root of n and the closest integer to the square root of n is less than or equal to k but nonzero.



      If i is the closest integer to the square root of n, you are looking for the first n where 0 < |i - sqrt(n)| <= k.



      Rules




      • You cannot use a language's insufficient implementation of non-integer numbers to trivialize the problem.

      • Otherwise, you can assume that k will not cause problems with, for example, floating point rounding.


      Test Cases



      .9         > 2
      .5 > 2
      .4 > 3
      .3 > 3
      .25 > 5
      .2 > 8
      .1 > 26
      .05 > 101
      .03 > 288
      .01 > 2501
      .005 > 10001
      .003 > 27888
      .001 > 250001
      .0005 > 1000001
      .0003 > 2778888
      .0001 > 25000001
      .0314159 > 255
      .00314159 > 25599
      .000314159 > 2534463


      Comma separated test case inputs:



      0.9, 0.5, 0.4, 0.3, 0.25, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.0314159, 0.00314159, 0.000314159


      This is code-golf, so shortest answer in bytes wins.










      share|improve this question











      $endgroup$




      Given a decimal number k, find the smallest integer n such that the square root of n is within k of an integer. However, the distance should be nonzero - n cannot be a perfect square.



      Given k, a decimal number or a fraction (whichever is easier for you), such that 0 < k < 1, output the smallest positive integer n such that the difference between the square root of n and the closest integer to the square root of n is less than or equal to k but nonzero.



      If i is the closest integer to the square root of n, you are looking for the first n where 0 < |i - sqrt(n)| <= k.



      Rules




      • You cannot use a language's insufficient implementation of non-integer numbers to trivialize the problem.

      • Otherwise, you can assume that k will not cause problems with, for example, floating point rounding.


      Test Cases



      .9         > 2
      .5 > 2
      .4 > 3
      .3 > 3
      .25 > 5
      .2 > 8
      .1 > 26
      .05 > 101
      .03 > 288
      .01 > 2501
      .005 > 10001
      .003 > 27888
      .001 > 250001
      .0005 > 1000001
      .0003 > 2778888
      .0001 > 25000001
      .0314159 > 255
      .00314159 > 25599
      .000314159 > 2534463


      Comma separated test case inputs:



      0.9, 0.5, 0.4, 0.3, 0.25, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.0314159, 0.00314159, 0.000314159


      This is code-golf, so shortest answer in bytes wins.







      code-golf number integer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago







      Stephen

















      asked 4 hours ago









      StephenStephen

      7,39823395




      7,39823395






















          6 Answers
          6






          active

          oldest

          votes


















          6












          $begingroup$


          Wolfram Language (Mathematica), 34 bytes



          Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&


          Try it online!



          Explanation



          The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.






          share|improve this answer











          $endgroup$





















            3












            $begingroup$

            JavaScript (ES7),  51  50 bytes





            f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n


            Try it online!



            (fails for the test cases that require too much recursion)





            Non-recursive version,  57  56 bytes





            k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}


            Try it online!



            Or for 55 bytes:



            k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)


            Try it online!



            (but this one is significantly slower)






            share|improve this answer











            $endgroup$





















              3












              $begingroup$


              J, 39 29 bytes



              [:<./_1 1++:*:@>.@%~1+(,-)@*:


              NB. This shorter version simply uses @alephalpha's formula.



              Try it online!



              39 bytes, original, brute force



              2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]


              Try it online!



              Handles all test cases






              share|improve this answer











              $endgroup$





















                2












                $begingroup$


                Japt, 18 bytes



                _¬%1©U>½-Z¬u1 a½}a


                Try it online!






                share|improve this answer











                $endgroup$













                • $begingroup$
                  Might be shorter using Arnauld's solution
                  $endgroup$
                  – ASCII-only
                  3 hours ago



















                2












                $begingroup$


                Python, 42 bytes





                lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)


                Try it online!



                Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k.



                Python 3.8 can save a byte with an inline assignment.



                Python 3.8, 41 bytes





                lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)


                Try it online!



                These beat my recursive solution:



                50 bytes





                f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)


                Try it online!






                share|improve this answer











                $endgroup$





















                  1












                  $begingroup$


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





                  k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n++))-p)>k|p%1==0;);return n-1;}


                  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%2f180412%2fsquare-root-distance-from-integers%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    6












                    $begingroup$


                    Wolfram Language (Mathematica), 34 bytes



                    Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&


                    Try it online!



                    Explanation



                    The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.






                    share|improve this answer











                    $endgroup$


















                      6












                      $begingroup$


                      Wolfram Language (Mathematica), 34 bytes



                      Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&


                      Try it online!



                      Explanation



                      The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.






                      share|improve this answer











                      $endgroup$
















                        6












                        6








                        6





                        $begingroup$


                        Wolfram Language (Mathematica), 34 bytes



                        Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&


                        Try it online!



                        Explanation



                        The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.






                        share|improve this answer











                        $endgroup$




                        Wolfram Language (Mathematica), 34 bytes



                        Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&


                        Try it online!



                        Explanation



                        The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 3 hours ago

























                        answered 3 hours ago









                        alephalphaalephalpha

                        21.4k32991




                        21.4k32991























                            3












                            $begingroup$

                            JavaScript (ES7),  51  50 bytes





                            f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n


                            Try it online!



                            (fails for the test cases that require too much recursion)





                            Non-recursive version,  57  56 bytes





                            k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}


                            Try it online!



                            Or for 55 bytes:



                            k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)


                            Try it online!



                            (but this one is significantly slower)






                            share|improve this answer











                            $endgroup$


















                              3












                              $begingroup$

                              JavaScript (ES7),  51  50 bytes





                              f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n


                              Try it online!



                              (fails for the test cases that require too much recursion)





                              Non-recursive version,  57  56 bytes





                              k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}


                              Try it online!



                              Or for 55 bytes:



                              k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)


                              Try it online!



                              (but this one is significantly slower)






                              share|improve this answer











                              $endgroup$
















                                3












                                3








                                3





                                $begingroup$

                                JavaScript (ES7),  51  50 bytes





                                f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n


                                Try it online!



                                (fails for the test cases that require too much recursion)





                                Non-recursive version,  57  56 bytes





                                k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}


                                Try it online!



                                Or for 55 bytes:



                                k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)


                                Try it online!



                                (but this one is significantly slower)






                                share|improve this answer











                                $endgroup$



                                JavaScript (ES7),  51  50 bytes





                                f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n


                                Try it online!



                                (fails for the test cases that require too much recursion)





                                Non-recursive version,  57  56 bytes





                                k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}


                                Try it online!



                                Or for 55 bytes:



                                k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)


                                Try it online!



                                (but this one is significantly slower)







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 3 hours ago

























                                answered 4 hours ago









                                ArnauldArnauld

                                76.8k693322




                                76.8k693322























                                    3












                                    $begingroup$


                                    J, 39 29 bytes



                                    [:<./_1 1++:*:@>.@%~1+(,-)@*:


                                    NB. This shorter version simply uses @alephalpha's formula.



                                    Try it online!



                                    39 bytes, original, brute force



                                    2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]


                                    Try it online!



                                    Handles all test cases






                                    share|improve this answer











                                    $endgroup$


















                                      3












                                      $begingroup$


                                      J, 39 29 bytes



                                      [:<./_1 1++:*:@>.@%~1+(,-)@*:


                                      NB. This shorter version simply uses @alephalpha's formula.



                                      Try it online!



                                      39 bytes, original, brute force



                                      2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]


                                      Try it online!



                                      Handles all test cases






                                      share|improve this answer











                                      $endgroup$
















                                        3












                                        3








                                        3





                                        $begingroup$


                                        J, 39 29 bytes



                                        [:<./_1 1++:*:@>.@%~1+(,-)@*:


                                        NB. This shorter version simply uses @alephalpha's formula.



                                        Try it online!



                                        39 bytes, original, brute force



                                        2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]


                                        Try it online!



                                        Handles all test cases






                                        share|improve this answer











                                        $endgroup$




                                        J, 39 29 bytes



                                        [:<./_1 1++:*:@>.@%~1+(,-)@*:


                                        NB. This shorter version simply uses @alephalpha's formula.



                                        Try it online!



                                        39 bytes, original, brute force



                                        2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]


                                        Try it online!



                                        Handles all test cases







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited 1 hour ago

























                                        answered 3 hours ago









                                        JonahJonah

                                        2,361916




                                        2,361916























                                            2












                                            $begingroup$


                                            Japt, 18 bytes



                                            _¬%1©U>½-Z¬u1 a½}a


                                            Try it online!






                                            share|improve this answer











                                            $endgroup$













                                            • $begingroup$
                                              Might be shorter using Arnauld's solution
                                              $endgroup$
                                              – ASCII-only
                                              3 hours ago
















                                            2












                                            $begingroup$


                                            Japt, 18 bytes



                                            _¬%1©U>½-Z¬u1 a½}a


                                            Try it online!






                                            share|improve this answer











                                            $endgroup$













                                            • $begingroup$
                                              Might be shorter using Arnauld's solution
                                              $endgroup$
                                              – ASCII-only
                                              3 hours ago














                                            2












                                            2








                                            2





                                            $begingroup$


                                            Japt, 18 bytes



                                            _¬%1©U>½-Z¬u1 a½}a


                                            Try it online!






                                            share|improve this answer











                                            $endgroup$




                                            Japt, 18 bytes



                                            _¬%1©U>½-Z¬u1 a½}a


                                            Try it online!







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited 3 hours ago

























                                            answered 3 hours ago









                                            ASCII-onlyASCII-only

                                            3,3821236




                                            3,3821236












                                            • $begingroup$
                                              Might be shorter using Arnauld's solution
                                              $endgroup$
                                              – ASCII-only
                                              3 hours ago


















                                            • $begingroup$
                                              Might be shorter using Arnauld's solution
                                              $endgroup$
                                              – ASCII-only
                                              3 hours ago
















                                            $begingroup$
                                            Might be shorter using Arnauld's solution
                                            $endgroup$
                                            – ASCII-only
                                            3 hours ago




                                            $begingroup$
                                            Might be shorter using Arnauld's solution
                                            $endgroup$
                                            – ASCII-only
                                            3 hours ago











                                            2












                                            $begingroup$


                                            Python, 42 bytes





                                            lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)


                                            Try it online!



                                            Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k.



                                            Python 3.8 can save a byte with an inline assignment.



                                            Python 3.8, 41 bytes





                                            lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)


                                            Try it online!



                                            These beat my recursive solution:



                                            50 bytes





                                            f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)


                                            Try it online!






                                            share|improve this answer











                                            $endgroup$


















                                              2












                                              $begingroup$


                                              Python, 42 bytes





                                              lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)


                                              Try it online!



                                              Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k.



                                              Python 3.8 can save a byte with an inline assignment.



                                              Python 3.8, 41 bytes





                                              lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)


                                              Try it online!



                                              These beat my recursive solution:



                                              50 bytes





                                              f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)


                                              Try it online!






                                              share|improve this answer











                                              $endgroup$
















                                                2












                                                2








                                                2





                                                $begingroup$


                                                Python, 42 bytes





                                                lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)


                                                Try it online!



                                                Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k.



                                                Python 3.8 can save a byte with an inline assignment.



                                                Python 3.8, 41 bytes





                                                lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)


                                                Try it online!



                                                These beat my recursive solution:



                                                50 bytes





                                                f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)


                                                Try it online!






                                                share|improve this answer











                                                $endgroup$




                                                Python, 42 bytes





                                                lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)


                                                Try it online!



                                                Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k.



                                                Python 3.8 can save a byte with an inline assignment.



                                                Python 3.8, 41 bytes





                                                lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)


                                                Try it online!



                                                These beat my recursive solution:



                                                50 bytes





                                                f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)


                                                Try it online!







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited 1 hour ago

























                                                answered 2 hours ago









                                                xnorxnor

                                                91.2k18186442




                                                91.2k18186442























                                                    1












                                                    $begingroup$


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





                                                    k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n++))-p)>k|p%1==0;);return n-1;}


                                                    Try it online!






                                                    share|improve this answer









                                                    $endgroup$


















                                                      1












                                                      $begingroup$


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





                                                      k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n++))-p)>k|p%1==0;);return n-1;}


                                                      Try it online!






                                                      share|improve this answer









                                                      $endgroup$
















                                                        1












                                                        1








                                                        1





                                                        $begingroup$


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





                                                        k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n++))-p)>k|p%1==0;);return n-1;}


                                                        Try it online!






                                                        share|improve this answer









                                                        $endgroup$




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





                                                        k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n++))-p)>k|p%1==0;);return n-1;}


                                                        Try it online!







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered 2 hours ago









                                                        Embodiment of IgnoranceEmbodiment of Ignorance

                                                        1,170119




                                                        1,170119






























                                                            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%2f180412%2fsquare-root-distance-from-integers%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

                                                            Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...

                                                            Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

                                                            VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...