C programming
factorial calculation
large numbers
algorithm
computational mathematics

Calculating factorial of large numbers in C

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The factorial of a number is a fundamental concept in mathematics, represented by the product of all positive integers up to a given number, denoted as n!. While it is straightforward to compute the factorial of small numbers using basic multiplication, calculating the factorial of large numbers in C can introduce significant challenges due to the limitations of standard data types and precision errors. This article explores methods and techniques to compute the factorial of large numbers efficiently in C.

Mathematical Background

Factorial, denoted as n!, is defined for a non-negative integer n as:

n!=n×(n1)×(n2)××1n! = n \times (n-1) \times (n-2) \times \ldots \times 1

Special cases include:

  • 0!=10! = 1
  • 1!=11! = 1

For small values of n, computing n! is feasible with ordinary arithmetic operations. However, as n increases, the value of n! grows exponentially, quickly surpassing the range of standard integer data types like int and long in C, which can manage only a limited range of values.

Challenges in Computing Factorials

Limitations of Standard Data Types

  • Integer Overflow: Standard data types (int, long, unsigned long) can store values only up to their maximum limits. For instance, on a typical 32-bit system, an unsigned int can store values up to 2^32−1, which is insufficient for computing large factorials because 20! alone equals 2,432,902,008,176,640,000.
  • Precision: Standard floating-point types (float, double) lack the precision necessary for large integers due to their inherent limitations in representing significant digits accurately.

Performance Considerations

Factorial calculations involve a large number of multiplications, increasing the computational complexity and time, especially for large n.

Techniques for Calculating Large Factorials

Using Arrays for Arbitrary-Precision Arithmetic

One efficient approach to manage the computation of large factorials in C is to use arrays to store individual digits of the number. This method involves performing arithmetic operations manually, digit by digit, as demonstrated below.

Example Implementation

  • res[] is used to store digits of the factorial result.
  • multiply() handles the digit-wise multiplication of res[] with an integer, updating the size of the result.
  • The final factorial is printed by parsing the digits from the array.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.