変更
Haskell
fact :: Int -> Int fact 0 = 1 fact n = n * fact (n - 1) main :: IO () main = do print (fact 10) -- > 3628800
Python
def fact(n): if n == 0: return 1 return n * fact(n - 1) print(fact(10)) #-> 3628800
Ruby
def fact n if n == 0 1 else n * fact(n - 1) end end puts fact 10 #-> 3628800
Java
public class Fact { static int fact(int n) { if (n == 0) { return 1; } else { return n * fact(n - 1); } } public static void main(String[] args) { System.out.println(fact(10)); //-> 3628800 } }
Javascript
function fact(n) { if (n === 0) { return 1; } else { return n * fact(n - 1); } } console.log(fact(10)); //-> 3628800
Emacs Lisp
(defun fact (n) (cond ((= n 0) 1) (t (* n (fact (- n 1)))) )) (fact 10) ;;-> 3628800
Common Lisp
(defun fact(n) (if (<= n 1) 1 (* n (fact (- n 1))))) (print (fact 10)) ;;-> 3628800
Go
package main import ("fmt") func fact(n int) int { if n == 0 { return 1 } return n * fact(n - 1) } func main() { fmt.Println(fact(10)) //-> 3628800 }
PHP
<?php function fact($n) { if ($n == 0) { return 1; } return $n * fact($n - 1); } echo fact(10); //-> 3628800