<?php
//for precision number
//number_format(45.234729492, 3) = 45.235;
//number_format(45.234729492, 2) = 45.23;
//number_format(405.234729492, 2) = 405.23;
function mySqrt($number){
$first_range = 1;
$last_range = $number;
$middle_point = 0;
$sq_middle_point = 0;
while(true){
$middle_point = number_format(($first_range+$last_range) / 2, 3);
$sq_middle_point = number_format($middle_point ** 2, 3);
if($sq_middle_point == $number){
break;
}
if($sq_middle_point > $number){
$last_range = $middle_point;
}else{
$first_range = $middle_point;
}
}
return $middle_point;
}
echo mySqrt(5);
//for precision number
//number_format(45.234729492, 3) = 45.235;
//number_format(45.234729492, 2) = 45.23;
//number_format(405.234729492, 2) = 405.23;
function mySqrt($number){
$first_range = 1;
$last_range = $number;
$middle_point = 0;
$sq_middle_point = 0;
while(true){
$middle_point = number_format(($first_range+$last_range) / 2, 3);
$sq_middle_point = number_format($middle_point ** 2, 3);
if($sq_middle_point == $number){
break;
}
if($sq_middle_point > $number){
$last_range = $middle_point;
}else{
$first_range = $middle_point;
}
}
return $middle_point;
}
echo mySqrt(5);
No comments:
Post a Comment