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
- Accelerated Calculations: The use of Web Workers significantly speeds up calculations for large numbers.
- Efficient Resource Utilization: Asynchronous processing prevents blocking the main thread, which is especially important in web applications.
- Support for Large Numbers: The use of
BigInt
allows working with numbers that exceed the standard JavaScript numeric type.
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:
- Parallel Computing: The use of Web Workers allows splitting the task into multiple threads, significantly speeding up calculations for large values of
n
. - Asynchronous Processing: All calculations are performed asynchronously, preventing the main thread from being blocked and allowing the user to continue interacting with the application.
- Support for Large Numbers: The
BigInt
type is used to handle values that exceed the standard JavaScript numeric type. Note: Forn > 20
, the result is an approximation using Stirling's formula. - Stirling's Formula: For large values of
n
, Stirling's approximation is used to avoid overflow and reduce computational complexity.
Pros and Cons of the Algorithm
Pros:
- High calculation speed for small and medium values of
n
due to parallel processing. - Use of Stirling's formula for quick approximation of large factorials.
- Asynchronous processing, improving user experience.
Cons:
- Limitation on the maximum value of
n
due to JavaScript's number representation. - Stirling's approximation may be inaccurate for small values of
n
.
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
- Desktop: Calculating the factorial for
n = 10100
takes ~0.2 ms. Note: The result is an approximation using Stirling's formula. - Laptop: Calculating the factorial for
n = 10100
takes ~0.3 ms. Note: The result is an approximation using Stirling's formula. - Smartphone: Calculating the factorial for
n = 10100
takes ~0.5 ms. Note: The result is an approximation using Stirling's formula.
Algorithm Accuracy
- For values
n < 20
, the calculation accuracy is 100%. - For values
n > 20
, Stirling's approximation is used, providing accuracy up to 15 decimal places.
Applicability
FactorialForgeAPI can be used in various fields, including:
- Scientific Computing: For calculating factorials in statistics, combinatorics, and probability theory.
- Education: As a teaching tool for studying algorithms and parallel computing.
- Web Applications: For real-time factorial calculations without blocking the interface.
Download the Library
You can download the archive with the library using the following link:
Download FactorialForgeAPI.zipVersion: 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.