5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

js 階乗の求め方

Last updated at Posted at 2016-12-12

##階乗の求め方

script.js
function factorialize(k) {
// write your code.
}
factorialize(5) // 120

出力結果 例

script.js
factorialize(5) // 120
factorialize(10) // 3628800
factorialize(20) // 2432902008176640000
factorialize(0) // 1

##試したコード

script.js
function factorialize(k) {
  var j = 1;
  for(var i = 1; i <= k; i++){
    j *= i;
  }
  return j;
}
function factorialize(num) {
  if (num === 0) { return 1; }
  return num * factorialize(num-1);
}

factorialize(5);

##考え方
5の階乗の求め方は、
5! = 1 * 2 * 3 * 4 * 5 = 120
1 から n までのすべての整数の積が求める値。
n までのすべての整数をfor文の i で表す。
k は n , j は 1を表す。

もっと簡潔に美しくかけるよ!という方、コメントお待ちしております。

##参考リンク
Arithmetic Operators

5
4
5

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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?