in reply to Square Root algorithm

Here's mine, did someone say GOLF!

cheers

tachyon

print sqroot(2,100); # long version sub sqroot { $number = shift; $iterate = shift; $guess = $number; for (0..$iterate) { $guess = (($number/$guess) + $guess)/2 ; } return $guess; } # golf at 44 strokes sub sqroot { ($n,$i)=@_;$g=$n;$g=($n/$g+$g)/2for 0..$i;$g }

Replies are listed 'Best First'.
Re: Re: Square Root algorithm
by jynx (Priest) on Dec 03, 2001 at 01:03 UTC

    well,

    if we're golfing this, you can reduce that line a bit:

    sub sqroot { #23456789_123456789_123456789_123456 $g++;$g=($_[0]/$g+$g)/2for-1..pop;$g }
    At 29 characters. It seems to work correctly on perl5.6

    jynx

    update: d'oh, i was erroneously fooled by my browser when entering the snippet, it's actually 36 characters...