Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

computing square root by hand -- no sqrt (golf, beginner)

by boo_radley (Parson)
on May 09, 2002 at 18:05 UTC ( [id://165450]=perlmeditation: print w/replies, xml ) Need Help??

I ran across this Dr. Math faq and thought it would make a nifty intro to perl golf( in that the algorithm is pretty simple).

your script should take 2 inputs on the command line, the first being the number we're looking for the root of, and a first 'best guess' Again, don't use sqrt in your code. Stop guessing when you have a reasonable idea of what the answer is, and print it out.

Your code isn't responsible for error checking or verifying that the output's correct (verify it with a calculator if you like).
here's an outline of the algorithm described in the first section of the above url:

First, start by guessing a square root value. It helps if
your guess is a good one but it will work even if it is a
terrible guess. We will guess that 2 is the square root of 12.
In step two, we divide 12 by our guess of 2 and we get 6.
In step three, we average 6 and 2: (6+2)/2 = 4
Now we repeat step two with the new guess of 4. So 12/4 = 3
Now average 4 and 3: (4+3)/2 = 3.5
Repeat step two: 12/3.5 = 3.43
Average: (3.5 + 3.43)/2 = 3.465

some sample input/output :
C:\>sqrt 701 25 26.4764404223228 C:\>sqrt 12 2 3.46428571428571 C:\>sqrt 120 10 10.9544511505092
All of these have have a precision of 3 digits.

my answer, at 66 characters, below :
($n,$_)=@ARGV;while(1){$_=($_+$n/$_)/2;($l^$_)||print&&exit;$l=$_}

Replies are listed 'Best First'.
Re: computing square root by hand -- no sqrt (golf, beginner)
by japhy (Canon) on May 09, 2002 at 18:17 UTC
    Golfing to 59:
    ($n,$_)=@ARGV;{$l^($_=($_+$n/$_)/2)||exit print;$l=$_;redo}

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;


      a reinterpretation of japhy's solution gives 54:
      $_=pop;$l^$_?$l=$_:die"$_\n"while$_=($_+$ARGV[0]/$_)/2
      jynx

      update: this works at 53:

      $_=pop;$l^$_?$l=$_:die"$_\n"while($_+=$ARGV[0]/$_)/=2
      update2: using the comma instead of quotes for die it's 52:
      $_=pop;$l^$_?$l=$_:die$_,$/while($_+=$ARGV[0]/$_)/=2
Re: computing square root by hand -- no sqrt (golf, beginner)
by Matts (Deacon) on May 09, 2002 at 19:49 UTC

      print $ARGV[0]**.5

      This is the best answer I've seen in this thread :) It's clear, short and very precise...
      How about some golfing while you're at it?

      die+shift()**.5,$/
      I know that this is 18 strokes like yours, but this one prints a newline ;)

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      


        Since you're only using one argument you can call it with one argument and shorten it to 16:
        die pop()**.5,$/
        but that's bending the rules...
        jynx
      How's 17? The space is unnecessary.
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
Re: computing square root by hand -- no sqrt (golf, beginner)
by jynx (Priest) on May 09, 2002 at 21:01 UTC

    While playing around, i thought i would (jokingly) try to golf Matt's solution and found something interesting. Using subtraction instead of bit-xor is more accurate. So, cleaning up my current mess of solutions and using subtraction for accuracy, it's now 49 chars (52 to print a carriage return):
    #23456789_123456789_123456789_123456789_123456789_123456789_ $_=pop;$l=$_,($_+=$ARGV[0]/$_)/=2while$l-$_;print
    jynx

    update:an approximation method:

    # 44 #23456789_123456789_123456789_123456789_123456789_ $a=pop;($a+=$ARGV[0]/$a)/=2for-9..99;print$a
Re: computing square root by hand -- no sqrt (golf, beginner)
by particle (Vicar) on May 09, 2002 at 19:51 UTC
    mine at 57:

    #0 1 2 3 4 5 6 #123456789012345678901234567890123456789012345678901234567890 $_=pop,$n=pop;{$l^($_=($_+$n/$_)/2)||die$_,$/;$l=$_;redo}

    ~Particle *accelerates*

(MeowChow) Re: computing square root by hand -- no sqrt (golf, beginner)
by MeowChow (Vicar) on May 10, 2002 at 10:16 UTC
    38 chars, note that the second argument is entirely superfluous:
      
    ($_+=$ARGV[0]/$_)/=2for($_=1)x99;print
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: computing square root by hand -- no sqrt (golf, beginner)
by cLive ;-) (Prior) on May 10, 2002 at 09:45 UTC
Re: computing square root by hand -- no sqrt (golf, beginner)
by pepik_knize (Scribe) on May 13, 2002 at 18:22 UTC
    44 using both arguments, but no \n. Add 3 for -l to get it.
    0 1 2 3 4 12345678901234567890123456789012345678901234 $b=pop;$b=($b+$ARGV[0]/$b)/2for 0..9;print$b

    Pepik

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://165450]
Approved by ichimunki
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-18 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found