Large Factorials Algorithm

The Large Factorials Algorithm is an essential computational method used to calculate the factorial of a large number, which is often not possible using conventional programming techniques due to limitations in data types and memory constraints. Factorial of a number (n!) is the product of all positive integers less than or equal to n, and it usually grows at an incredibly fast rate, making it difficult to compute for large values of n. To overcome these challenges, the Large Factorials Algorithm employs techniques such as prime factorization, array manipulation, and modular arithmetic to efficiently compute and represent large factorials. One of the popular approaches to calculating large factorials is the use of the multiplicative method, which utilizes an array to store the intermediate results of the factorial calculation. The algorithm starts by initializing the array with the value of 1 and then iteratively multiplies each element of the array with the current number (i) in the range from 2 to n. After each multiplication step, the array elements are updated to hold the new intermediate results, and any carry values are propagated to higher indices in the array. This process continues until all values from 2 to n have been multiplied, and the final factorial value is obtained by reading the array elements in reverse order. The algorithm's efficiency comes from its ability to handle large numbers by breaking them down into smaller, manageable parts, allowing it to compute factorials for large values of n that would otherwise be impossible using traditional techniques.
#include <stdio.h>

int main(){

	int a[16500], T;
	long long int i, j;

	printf("Enter number of test cases : ");
	scanf("%d", &T);

	while(T--){
		for(i=0; i<16500; i++){
			a[i]=0;
		}

		a[1]=1;
		int N, carry=0, count=0;
		printf("Enter a number : ");
		scanf("%d", &N);

		for(i=1; i<=N; i++){
			carry=0;
			for(j=0; j<16500; j++){
				a[j]=a[j]*i+carry;
				carry=a[j]/10;
				a[j]=a[j]%10;
			}
		}

		for(i=0; i<16500; i++){
			if(a[i]!=0){
				count=i;
			}
		}

		for(i=count; i>0; i--){
			printf("%d", a[i]);
		}
		printf("\n");

	}

	return 0;
}

LANGUAGE:

DARK MODE: