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 ='.$a.'<br>';
echo 'mysq result='.mysq(128).'<br>';
echo 'sqrt time ='.($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>';