Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // https://blog.ruangguru.com/aturan-perkalian-dan-faktorial-dalam-teori-peluang
- function factorial($n) {
- echo "$n! = ";
- $f = 1;
- for($i=1; $i<=$n; $i++) {
- $f = $f * $i;
- echo "$i" . ( ($i<$n)? "." : "" );
- }
- echo "= $f <br/>";
- }
- // Test
- //2! = 2.1 = 2
- factorial(2);
- //3! = 3.2.1 = 6
- factorial(3);
- //4! = 4.3.2.1 = 24
- factorial(4);
- //5! = 5.4.3.2.1 = 120
- factorial(5);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement