Calculate factorial

Views: 11 Last modified: August 03rd, 2011 Comments: 0

This function will return the factorial of $int (is the product of all positive integers less than or equal to n).

<?php
function factorial( $int )
{
	// For as long $i is more or equal to 2
	for ( $i = $int; $i >= 2; $i-- ):
		// If $i is equal to $int
		if ( $i == $int ):
			$result = $int;
			continue;
		endif;
		// Result
		$result *= $i;
	endfor;

	//  Return with number format
	return number_format( $result );
}

// Factorial
echo factorial( 20 );
?>

This will output:

2,432,902,008,176,640,000
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {5+7} =  
    Anything to add ?

        You must be logged in to post a comment.