[Algorithm] Math - Factorial

수학 비트조작

Posted by lim.Chuck on September 18, 2024

Factorial?

팩토리얼(Factorial)은 음이 아닌 정수 n의 팩토리얼을 n!으로 표기하며, 이는 n보다 작거나 같은 모든 양의 정수의 곱입니다. 수학적으로는 다음과 같이 정의됩니다:

  • n! = n × (n - 1) × (n - 2) × … × 1
  • 특수한 경우: 0! = 1으로 정의됩니다.

예시:

  • 3! = 3 × 2 × 1 = 6
  • 5! = 5 × 4 × 3 × 2 × 1 = 120

팩토리얼의 사용 사례

팩토리얼은 다양한 분야에서 사용됩니다:

  1. 조합론(Combinatorics): 순열과 조합을 계산할 때 사용.
  2. 확률(Probability): 확률 이론과 이항 분포, 포아송 분포 등에서 사용.
  3. 수학(Mathematics): 미적분학, 대수학, 급수 전개(Taylor 급수 등)에서 자주 사용.
  4. 컴퓨터 과학(Computer Science): 재귀, 동적 프로그래밍 같은 알고리즘과 퍼즐에서 활용.

JavaScript로 팩토리얼 구현

JavaScript에서 팩토리얼 함수를 반복(iterative) 방식과 재귀(recursive) 방식으로 구현할 수 있습니다.

반복적 접근

1
2
3
4
5
6
7
8
9
10
function factorialIterative(n) {
  let result = 1;
  for (let i = 2; i <= n; i++) {
    result *= i;
  }
  return result;
}

console.log(factorialIterative(5)); // 120 출력
console.log(factorialIterative(0)); // 1 출력

재귀적 접근

1
2
3
4
5
6
7
8
9
function factorialRecursive(n) {
  if (n === 0) {
    return 1;
  }
  return n * factorialRecursive(n - 1);
}

console.log(factorialRecursive(5)); // 120 출력
console.log(factorialRecursive(0)); // 1 출력

Rust로 팩토리얼 구현

Rust에서도 반복 방식과 재귀 방식으로 팩토리얼을 구현할 수 있습니다.

반복적 접근

1
2
3
4
5
6
7
8
9
10
11
12
fn factorial_iterative(n: u32) -> u32 {
    let mut result = 1;
    for i in 2..=n {
        result *= i;
    }
    result
}

fn main() {
    println!("{}", factorial_iterative(5)); // 120 출력
    println!("{}", factorial_iterative(0)); // 1 출력
}

재귀적 접근

1
2
3
4
5
6
7
8
9
10
11
fn factorial_recursive(n: u32) -> u32 {
    if n == 0 {
        return 1;
    }
    n * factorial_recursive(n - 1)
}

fn main() {
    println!("{}", factorial_recursive(5)); // 120 출력
    println!("{}", factorial_recursive(0)); // 1 출력
}

요약:

팩토리얼은 조합론, 확률, 다양한 알고리즘 등 많은 분야에서 사용되는 단순하지만 강력한 수학적 개념입니다. JavaScript와 Rust 모두 반복적 또는 재귀적인 방법을 통해 효율적으로 팩토리얼을 계산할 수 있습니다.

Factorial?

The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers less than or equal to n. Mathematically, it is defined as:

  • n! = n × (n - 1) × (n - 2) × … × 1
  • Special case: 0! = 1 by definition.

For example:

  • 3! = 3 × 2 × 1 = 6
  • 5! = 5 × 4 × 3 × 2 × 1 = 120

Where is Factorial Used?

Factorials are used in various fields:

  1. Combinatorics: To calculate permutations and combinations.
  2. Probability: Used in probability theory and distributions like the binomial and Poisson distributions.
  3. Mathematics: Factorials are often used in calculus, algebra, and series expansions (e.g., Taylor series).
  4. Computer Science: Algorithms and puzzles like recursion and dynamic programming often use factorials.

Example of Factorial in JavaScript

Here’s how you can implement the factorial function both iteratively and recursively in JavaScript.

Iterative Approach

1
2
3
4
5
6
7
8
9
10
function factorialIterative(n) {
  let result = 1;
  for (let i = 2; i <= n; i++) {
    result *= i;
  }
  return result;
}

console.log(factorialIterative(5)); // Outputs 120
console.log(factorialIterative(0)); // Outputs 1

Recursive Approach

1
2
3
4
5
6
7
8
9
function factorialRecursive(n) {
  if (n === 0) {
    return 1;
  }
  return n * factorialRecursive(n - 1);
}

console.log(factorialRecursive(5)); // Outputs 120
console.log(factorialRecursive(0)); // Outputs 1

Example of Factorial in Rust

Similarly, here’s how you can implement factorial in Rust using both iterative and recursive approaches.

Iterative Approach

1
2
3
4
5
6
7
8
9
10
11
12
fn factorial_iterative(n: u32) -> u32 {
    let mut result = 1;
    for i in 2..=n {
        result *= i;
    }
    result
}

fn main() {
    println!("{}", factorial_iterative(5)); // Outputs 120
    println!("{}", factorial_iterative(0)); // Outputs 1
}

Recursive Approach

1
2
3
4
5
6
7
8
9
10
11
fn factorial_recursive(n: u32) -> u32 {
    if n == 0 {
        return 1;
    }
    n * factorial_recursive(n - 1)
}

fn main() {
    println!("{}", factorial_recursive(5)); // Outputs 120
    println!("{}", factorial_recursive(0)); // Outputs 1
}

Summary:

Factorial is a simple yet powerful mathematical concept used in many areas such as combinatorics, probability, and various algorithms. Both JavaScript and Rust provide the ability to compute factorials efficiently using iterative or recursive methods.