FactorialForgeAPI.js

Demo

Enter a number to calculate its factorial. Note: For large values of n, the result will be an approximation using Stirling's formula.

Relevance of the Problem

Factorials are widely used in mathematics, statistics, combinatorics, probability theory, and other fields. However, calculating factorials for large numbers is a computationally intensive task, especially with limited resources. FactorialForgeAPI solves this problem by providing an efficient tool for calculating factorials using parallel computing and mathematical approximations.

Problems Solved by the Product

API Description

FactorialForgeAPI provides a powerful and efficient interface for calculating factorials using modern parallel computing methods and mathematical approximations. It delivers exact results for small values of n and highly accurate approximations for large values of n using Stirling's formula. Key methods:

compute(n)

Calculates the factorial of the number n using parallel computing via Web Workers. This method is optimized for working with large numbers and uses an algorithm that splits the task into multiple threads to speed up calculations. Note: For n > 20, the result is an approximation using Stirling's formula.

approximation(n)

Returns an approximate value of the factorial using Stirling's formula. This method is particularly useful for large values of n, where exact calculation becomes computationally intensive.

Using the API

To use the API, include the FactorialForgeAPI.js library in your project. After that, you can call the compute and approximation methods, passing them a non-negative integer.

Example usage:

const result = await FactorialForgeAPI.compute(5); // 120
const approx = await FactorialForgeAPI.approximation(5); // 118.019
            

Note that the compute method can handle values up to 170, after which the result may be inaccurate due to JavaScript's limitations on number representation.

Mathematical Description

The factorial of a number n (denoted as n!) is the product of all positive integers from 1 to n. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120.

For large values of n, Stirling's approximation is used:

log(n!) ~ n * log(n) - n + 0.5 * log(2 * pi * n)
            

This formula allows for a quick estimation of the factorial value for large n, which is especially useful in statistics, combinatorics, and other fields where factorials are frequently encountered.

Factorial Calculation Algorithm

To calculate the factorial, a parallel algorithm is used that splits the task into multiple threads using Web Workers. Each thread calculates the product of numbers in its range, after which the results are combined. This significantly speeds up calculations for large values of n.

Technical Implementation Features

FactorialForgeAPI is implemented using modern JavaScript technologies and Web Workers to enhance performance. The library is optimized for working with large numbers and includes:

Pros and Cons of the Algorithm

Pros:

Cons:

Testing and Statistics

To evaluate the performance and accuracy of FactorialForgeAPI, tests were conducted on various devices and under different conditions. Below are the test results:

Performance

Algorithm Accuracy

Applicability

FactorialForgeAPI can be used in various fields, including:

Download the Library

You can download the archive with the library using the following link:

Download FactorialForgeAPI.zip

Version: 1.0.0

Release Date: March 20, 2025

File Size: 9.53 KB

This is a free, open-source product. You are free to use, modify, and distribute it under the terms of the license specified in the LICENSE.rst file.