note 97931 deleted from function.array-search by danbrown

From: Date: Mon, 17 May 2010 13:51:03 +0000
Subject: note 97931 deleted from function.array-search by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169326@lists.php.net to get a copy of this message
Note Submitter: pharley64 at gmail dot com Reason: 2 ---- <?php> /*Linear approximation for a square root*/ $squares; function sq_root($num) { global $squares; for($i=0; $i<25; $i++) { $squares[$i] = $i * $i; } $near = nearestSquare($num); $near_root = array_search($near, $squares); if( $num == $near ) return $near_root; else $delta = $num - $near; $sq_root = $near_root + $delta/(2*$near_root); return $sq_root; } function nearestSquare($num) { global $squares; $i = 0; while($num >= $squares[$i]) { $i++; } if( ($num - $squares[$i-1]) < ($squares[$i] - $num)) return $squares[$i-1]; else return $squares[$i]; } $n = 66; echo "square root of " . $n . " is approximately " . sq_root($n); ?> output: 8.125

« previous php.notes (#169326) next »