Archive

Posts Tagged ‘calculate’

PHP Function To Work Out Factorial Numbers

October 8th, 2008 No comments

Factorial of a number is defined as the product of the number and all of the numbers small than it. So if you take the number 4 the factorial of that number is 24 or 1 x 2 x 3 x 4.

Factorials are useful for a number of applications, for example when working out how many times a set of objects can be combined in different ways.

Use the following PHP function to work out the factorial of any given number. The first thing it does it make sure that the number is greater than 1 because the factorial of the number 1 is 1.

function factorial($number){
 $result = 1;
 if($number > 1){
  for($i = 2; $i >= $number; $i++){
   $result *= $i;
  }
 }
 return $result;
}

The function works by using the *= operator, which does a calculation between the left and right sides (in this case multiplication) and stores the result on the left hand side. This operator is much like the += operator, and works in the same way.

An alternative of this is the GMP function gmp_fact(). GMP is a set of maths functions that can be used to do some special things like add very large numbers together. It has been part of the PHP core since version 4.0.4.

Beware that factorial numbers can get very large, very quickly. For example, the factorial of the number 50 is 30,414,093,201,713,375,576,366,966,406,747,986,832,057,064,836,514,787,179,557,289,984.

Categories: PHP Tags: , , , , ,

Get Fibonacci Numbers Using PHP

October 7th, 2008 No comments

Fibonacci numbers not only have a few uses, but are also quite a nice little number sequence in themselves.

The sequence starts at 0, the next number is 1, and every number after that is the sum of the last two numbers. So the third number is 1, and the second number is 2.

To create this number sequence in PHP we need to create the first two items in the array. As we know that these are 0 and 1 we can create the array like this.

$fibarray = array(0,1);

To create the third number we just add the first two numbers together.

$fibarray[2] = $fibarray[0]+$fibarray[1];

This can be continued for as much as we want if we add it to a loop.

for($i=0;$i<=10;++$i){
 $fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];
}

This will create an array with the following values.


Array
(
 [0] => 0
 [1] => 1
 [2] => 1
 [3] => 2
 [4] => 3
 [5] => 5
 [6] => 8
 [7] => 13
 [8] => 21
 [9] => 34
 [10] => 55
)

Following on from this we can get a Fibonacci sequence to any position we want by using the following function.

function fibonacciSequence($pos){
 $fibarray = array(0,1);
 for($i=0;$i<=$pos;++$i){
  $fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];
 }
 return $fibarray;
}

We can also create a very similar function that will only return a number at a particular position.

function fibonacciSequence($pos){
 $fibarray = array(0,1);
 for($i=0;$i<=$pos;++$i){
  $fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];
 }
 return $fibarray[$pos];
}

For example, if we pass this function the number 56, the result would be 225,851,433,717.

Categories: PHP Tags: , , , , ,

Get Percentage Of A Number With PHP

June 4th, 2008 1 comment

Use the following function to find the percentage value of one number to another. If the $total parameter is zero then zero is returned as this would otherwise result in a division by zero error.

function get_percentage($total, $number){
  if($total>0){
   return round($number / ($total / 100),2);
  }else{
    return 0;
  }
}

Here are some examples of the function in action.

echo get_percentage(100,50).'%'; // 50%
echo get_percentage(100,10).'%'; // 10%
echo get_percentage(100,100).'%'; // 100%
echo get_percentage(400,3).'%'; // 0.75%
echo get_percentage(1234,4321).'%'; // 350.16%

Categories: PHP Tags: , , ,