728x90

재귀의 가장 기본이라고 할수있는

팩토리얼 문제를 풀어보았다.

 

팩토리얼이란

주어진 수 까지의 차례곱을 의미한다.

 

예) 5! = 1 * 2 * 3 * 4 * 5 = 120

 

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin").toString().split(" ");
const value = parseInt(input[0]);

function factorial(n, sum) {
  if (n === 0) {
    console.log(sum);
  } else {
    const multi = sum * n;
    const m = n - 1;
    factorial(m, multi);
  }
}

factorial(value, 1);

 

 

https://github.com/jaekwangLee/algorithm

 

GitHub - jaekwangLee/algorithm

Contribute to jaekwangLee/algorithm development by creating an account on GitHub.

github.com

 

728x90
반응형