Skip to content

Instantly share code, notes, and snippets.

@gushwell
Last active April 14, 2018 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gushwell/d34899bc6a17d525ce537f7af619bfc1 to your computer and use it in GitHub Desktop.
Save gushwell/d34899bc6a17d525ce537f7af619bfc1 to your computer and use it in GitHub Desktop.
C#で階乗の計算(再帰処理) ref: https://qiita.com/gushwell/items/51abfaa8bd05a38a5dd0
public static long Factorial(int n) {
if (n == 0)
return 1L;
return n * Factorial(n - 1);
}
static long Factorial(int n) =>
n == 0 ? 1L : n * Factorial(n - 1);
n! = n * (n-1) * (n-2) * ... * 3 * 2 * 1 (n > 0 のとき)
= 1 (n = 0 のとき)
(n-1) * (n-2) * ... * 3 * 2 * 1
n! = n * (n-1)!
n! = n * (n-1)! (n > 0 のとき)
= 1 (n = 0 のとき)
public static long Factorial(int n) {
checked {
if (n == 0)
return 1L;
return n * Factorial(n - 1);
}
}
static long Factorial(int n) =>
checked (n == 0 ? 1L : n * Factorial(n - 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment