Below is the php code to print the factorial of a number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $save_factorial = array (); function factorial( $n ) { global $save_factorial ; if ( $n ==1|| $n ==0) return 1; if (isset( $save_factorial [ $n ])) return $save_factorial [ $n ]; $save_factorial [ $n ]= bcmul ( $n ,factorial( $n -1)); return $save_factorial [ $n ]; } $t =trim( fgets (STDIN)); for ( $k =1; $k <= $t ; $k ++) { $i =trim( fgets (STDIN)); echo factorial( $i ). "\n" ; } |
No comments:
Post a Comment