|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
There are times you have to get a root of a number into very precise decimal. bcpow() does not work like pow(), it can't accept decimal numbers. Which means it can't get a root of a number. Here is a function that can do that. This system is optimized, but it's still slow when taking in a large number.
Here is the function
|
function bcgetscale(){
return strlen(bcadd(1,0))-2;
}
//Version 0.2 of BCRoot
//By Chao XU
//From www.webdevlogs.com
//Change Log: It uses a lot bcsqrt() to make the speed fast
//fix a small decimal bug, where the last decimal could be wrong
function bcroot($a, $n, $scale='default'){
$default = bcgetscale();//Get the scale
if($scale == 'default'){
$scale = $default;//use default scale
}
if($n & ($n-1)){//check if $n is the power of 2, return 0 if is
//decbin is the reason this function can't have number
//larger than 2^31
$bin = decbin($n);
$i = strlen($bin)-1;
$pow = 0;
while($i){
if($bin[$i]==='0'){
++$pow; –$i;
}else{
break;
}
}
$n = bcdiv($n, bcpow(2, $pow),0);
//now use Newton’s method to find the number
bcscale($scale+15);
$x = 1;
$k = 0;
$limit = ceil(log($scale+15)/log(2))+1;
while($k<$limit){
$t1 = bcdiv(1,$n);
$t21 = bcmul(bcsub($n,1),$x);
$t22 = bcdiv($a,bcpow($x, $n-1));
$t2 = bcadd($t21,$t22);
$x = bcmul($t1, $t2);
++$k;
}
$i = 0;
while($i < $pow){
$x = bcsqrt($x,$scale+3);
++$i;
}
bcscale($default);
return bcadd($x,0,$scale);
}else{
//here use many bcsqrt, because this is FAST
$i = 0;
$pow = log($n)/log(2);
while($i < $pow){
$a = bcsqrt($a,$scale+3);
++$i;
}
return bcadd($a,0,$scale);
}
}
?> | |
Here is how to use it:
| <?php
$the_number = 3;
$the_nth_root = 12;
$the_precision = 30;
bcroot($the_number, $the_nth_root, $the_precision);
//returns 1.095872691135244380160019128072.
//Which is the 12th root of 3
?> | | |
|
| Show the steps for converting a number from a given base to base 10. Shows the steps involved in converting a number from a given base to base 10. Categories : PHP, Math., Algorithms | | | How to judge if an integer is odd or is even in Php3? Categories : Math., PHP, Algorithms | | | Arbitrary Precision Math using BCMATH routines Categories : PHP, Math., BC math | | | Fast PI calculator. Can easily find the 1000th decimal place of pi in 5 seconds. Categories : PHP, BC math, Algorithms | | | Library of math functions to expand the functionality of PHP3. Version 1.2.1 fixes a major problem with the gcd function.
Categories : Algorithms, PHP, Math. | | | Prime Spiral is a image plotted with all the primes in a number spiral.
Categories : Algorithms, Graphics, GD image library, Math. | | | Prime number finder (Sieve of Erastothenes) Categories : PHP, Algorithms, Math. | | | Simple histogram class with a constructor and printing
methods (updated 1999/03/26) Categories : Algorithms, Math. | | | Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | Reverse a given number Categories : PHP, Beginner Guides, Algorithms, Math. | | | Calculate the great circle distance between two latitude/longitudes
Categories : Algorithms, PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | You have a set of many resources , and You need to know wich of these resources are available for a given use during a given period but MySql does not allow SUB-Selection :(
here is the right way to do that in a single query
Categories : MySQL, General SQL, Databases, Algorithms | | | Easy to use random number function that seeds with uniqid and allows a max value Categories : Math., PHP | | | SHA: Implementation of the Secure Hash Algorithm in pure PHP. This is a secure one-way function that can be used to perform challenge
response login algorithms over an insecure connection. Categories : Algorithms, PHP, Security | |
|
|