Jump to content

Brute force algorithm for square roots with tatics


cwarn23

Recommended Posts

Hi, I have just created an algorithm and could this be the greatest algorithm invented for calculating square roots? Perhaps the answer is yes with 100% accuracy unlike some algorithms that round the last digit up. Below is the algorithm I have created and I used php. Also this algorithm can easily be imported into any language with any bitrate and is very scalable unlike some of the standard built in square root algorithms for some interpreters (all of the ones tested so far). Now below is the code with it in both the form of a class and the form of a function.

 

You may notice that the function name is funny but it is tatically named that way so that the string length of the function name is the same as the sqrt() function so you don't get qwerky results when comparing the two.

<?php
class sqrt {
    public $value;
    function __construct($in) {
    $tmp = (int) $in;
    $tmpp = ($tmp/2);
    for ($i=1;($i*$i)<=$tmp;$i*=2) {}
    $i/=2;
    for (;($i*$i)<=$tmp;$i++) {}
    $i--;
    $k=$i;
    $i++;
    if (($i*$i)>$tmp || ($i*$i)<$tmp) {
        for ($j=1;($i*$i)>=$tmp;$j/=2,$i=($j+$k)) {}
        
        $v=strlen((string)$j);
        $m=1/pow(10,$v);
        for (;($i*$i)<=$tmp;$j+=$m,$i=($j+$k)) {}
        $j-=($m);
        $i=$j+$k;
        
        if (($i*$i)>$tmp || ($i*$i)<$tmp) {
        //$w = (strlen((string)pow(2,32))+1);
        //$w = same number as below
        $w = 11; //32 bit
        $q=$m;
        for ($n=$v+1;$n<$w;$n++) {
            $m=1/pow(10,$n);
            $p=pow(10,(($n-$v)+1));
            for ($o=0;$o<$p && ($i*$i)<=$tmp;$j+=$m,$i=($j+$k),$o++) {}
            $j-=$m;
            $i=($j+$k);
            }
        }
    }
    echo $i;
    $this->value = $i;
    return $this->value;
    }
    
}
    function mysq($in) {
        $tmp = (int) $in;
        $tmpp = ($tmp/2);
        for ($i=1;($i*$i)<=$tmp;$i*=2) {}
        $i/=2;
        for (;($i*$i)<=$tmp;$i++) {}
        $i--;
        $k=$i;
        $i++;
        if (($i*$i)>$tmp || ($i*$i)<$tmp) {
            for ($j=1;($i*$i)>=$tmp;$j/=2,$i=($j+$k)) {}
            
            $v=strlen((string)$j);
            $m=1/pow(10,$v);
            for (;($i*$i)<=$tmp;$j+=$m,$i=($j+$k)) {}
            $j-=($m);
            $i=$j+$k;
            
            if (($i*$i)>$tmp || ($i*$i)<$tmp) {
            //$w = (strlen((string)pow(2,32))+1);
            //$w = same number as below
            $w = 11; //32 bit
            $q=$m;
            for ($n=$v+1;$n<$w;$n++) {
                $m=1/pow(10,$n);
                $p=pow(10,(($n-$v)+1));
                for ($o=0;$o<$p && ($i*$i)<=$tmp;$j+=$m,$i=($j+$k),$o++) {}
                $j-=$m;
                $i=($j+$k);
                }
            }
        }
        return $i;
    }
    
$s = microtime(true);
mysq(128);
$e = microtime(true);

$a=($e-$s);
unset($e,$s);

sleep(1);

$s = microtime(true);
sqrt(128);
$e = microtime(true);
echo 'mysq time &#160;&#160;='.$a.'<br>';
echo 'mysq result='.mysq(128).'<br>';
echo 'sqrt time&#160;&#160;='.($e-$s).'<br>';
echo 'sqrt result='.sqrt(128).'<br>';
echo 'The mysq function is '.bcdiv(substr(($e-$s),0,11),$a,0).' times faster for this calculation.<br>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.