LoginSignup
2
0

More than 3 years have passed since last update.

PHPで階乗を計算する関数

Last updated at Posted at 2020-03-20

備忘録。PHPで階乗を計算するプログラム。

再帰関数を使わずに実装。

sample.php

echo factorial_of($num);

function factorial_of($num){

    $num2 = $num-1;

    while($num2>0){
        $num *= $num2;

        if($num2>=1){
            $num2--;
        }
    }

    return $num;
}

再帰関数で実装。

sample2.php
echo factorial_of($num); 

function factorial_of($num){

    if($num>0) {
        return $num *= factorial_of($num-1);
    }

    return 1;
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0