Factorial Implementations — Tutorial

Factorial Implementations — Advanced Tutorial

Two ready-to-build C++ examples included in this folder: a simple recursive example using native types and an iterative big-integer example using a digit vector.

Files

  • recursive_simple.cpp — Recursive version using unsigned long long. Short and didactic; overflows for n > 20.
  • bigint_iterative.cpp — Iterative big-integer version using a vector of base-10 digits. Computes exact factorials for much larger n without external libraries.
  • factorial_comparison.md — Comparison notes and guidance for publication.

How to build

GCC / Clang

g++ -std=c++17 -O2 recursive_simple.cpp -o recursive_simple
./recursive_simple

g++ -std=c++17 -O2 bigint_iterative.cpp -o bigint_iterative
./bigint_iterative

MSVC (Developer Command Prompt)

cl /EHsc recursive_simple.cpp
recursive_simple.exe

cl /EHsc bigint_iterative.cpp
bigint_iterative.exe
Note: If you open the Visual Studio solution included in this folder, ensure only one file with main() is built per project, or keep these examples in separate projects to avoid multiple-definition linker errors.

Quick comparison

  • Recursive (native): minimal code; limited by 64-bit overflow and recursion depth.
  • Big-int (digit vector): exact results for large n; simple implementation but not optimized for thousands of digits — prefer boost::multiprecision::cpp_int or GMP for production.

Suggested tutorial flow

  1. Introduce factorial mathematically.
  2. Show recursive native implementation and explain overflow and stack-depth limits.
  3. Show iterative native implementation and discuss limits.
  4. Present big-integer approach (this repo) and explain complexity.
  5. Demonstrate using boost::multiprecision::cpp_int and compare performance.
  6. Optional: link to advanced algorithms (prime-swing, binary splitting) for very large factorials.

Ready to publish. If you want, I can also generate an HTML page that embeds full source code blocks, or produce a single combined ZIP containing both examples and the tutorial HTML.

When the topic is Function, there are some misunderstandings about what concerns parameter and argument of a function. To create a function in C/C++ , we need to define a return type(void, int, char, float, etc), a name and at least, one variable as the formal parameter.

An exemple in C:

#include<stdio.h>

/*Here, “power” is the name of function, in which represents a mathematical function for any given number (base) raised to some defined number(exponent). Or, any number multiplied by itself for an specific number of times(so called the exponent). */

int power(int base, int n) /* base and n are the formal parameters of the function named power*/

{int p=1;for(int i=1; i<=n; ++i)

p*=base;

printf(“POWER FUNCTION:%d To The Power of %d is: %dn”, base,n,p);

}

int main() { 

power(3,4); /* in main function, we call the function power with two arguments: the int variables ‘base’ and ‘n’, represented by the values of 3 and 4) */

}

Parameters vs Arguments in C/C++

Shopping Cart
Optimized by Optimole
Scroll to Top