Click here to Skip to main content
14,607,970 members

1000 Factorial

Rate this:
4.54 (29 votes)
Please Sign up or sign in to vote.
4.54 (29 votes)
27 Jul 2009CPOL
Optimum algorithm for calculating factorial of large number
Sample Image - maximum width is 600 pixels


Factorial or the product of positive integer numbers is one of the most usable mathematical functions (or processes). A lot of mathematical calculations need to have exact result of great number's factorial, such as 1000! to find the final response with high accuracy. But the problem is that no programming language has a variable or mechanism to store such a big figure. All of the programming languages and calculators estimate the result and then store it as scientific number. For example, calculator of Windows XP displays 1000! like this:

4.02387260077093773543702433923 e+2567 

So I decided to invent an algorithm for solving this problem.


You know to calculate factorial of a number, you should multiply all numbers from 1 to itself, for example:

1000! = 1*2*3*……*998*999*1000 

There is no variable in any programming language that can store the result of this multiplication exactly; except storing in scientific notation. Because of sequential multiplication's huge result, finding a new strategy or mechanism is necessary.

In my recommended algorithm, the number is not looked at as a number, instead it is treated as sequential digits where each one has its own numerical value. In fact, an array of digits is available. Each time, the second number is multiplied by each digit of the first number (index of array) and then added with carry number up from the previous multiplication.
This process is shown, for example 12!:

12! = 11! * 12     11! = 39916800     12! = 479001600 

Sample Image - maximum width is 600 pixels

  • If the result is less than 10, the result will be placed in the cell of array. 
  • If it is equal to or greater than 10, it will be divided by 10 and then the remainder will be placed in the array's cell and quotient placed in variable that will be added with next multiplication's response.
    Note: Notice that all the numbers are stored from end of array, the same as normal calculation (real calculation).

Programming Code


int numArr[3000];			
int total,rem=0,count;	
register int i;

In the first line, an array is defined such that its size depends on factorial size. "rem" is defined to store remainder of division.

At the end , an integer variable is defined and named "i" that plays the role of loop counter and array's index and due to much access, it's defined as register.

The register type modifier tells the compiler to store the variable being declared in a CPU register (if possible), to optimize access.

Note: Modern compilers have a good optimizer that decides which variables should be stored in registers. They make their own register choices when global register-allocation optimization is on.

The Main Part of the Code

i=2999;				//start from end of array.

According to the algorithm that was explained before, there is a loop counting 2 to 1000 and each time value of 'count' is multiplied by array's cell and added with 'rem' which contains carry from previous multiplication.

Finally, the result is stored in 'total' and then placed in the array's cell.

Another Algorithm

At first, I found another algorithm for this problem. It's based on simulating multiplication by successive additions. For example 20= 4*5 also 20= (5+5+5+5).
So putting numbers in an array and then adding it with itself 'X' times.
X for 1000! is:

X= ∑ n
n=1,2,3, … ,999,1000

Note: This algorithm is not as optimum as the first one that I explained. Also it's too dull and needs several big arrays. The second ZIP file belong to this algorithm.

Points of Interest

I would like to point out that the algorithm has high computing speed, so it turns the program to quite an optimum state.

One of the most important features of this algorithm is using just one array, while other algorithms need more memory (using several arrays to simulate multiplication by sequential sums).

Furthermore, less coding helps to understand the program easily and you can rewrite it in any programming language.

One of the reasons for high execution speed is using register variable as loop's counter. Loop's counter is a variable with most access to it.


As I mentioned in the introduction, factorial operation is used in a lot of mathematical calculations, especially, the computations that relate to statistics that need exact result of factorial.

So this program can be part of any program that needs factorial method, such as statistical software.


I'd like to thank Mr.Fermisk Naserzade for introducing me to this problem and Mr. Iraj Safa for helping me to edit this article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mohammad Shafieenia
Iran (Islamic Republic of) Iran (Islamic Republic of)
Mohammad Shafienia is student of MS of IT(Information Security) in Tehran University who has been coding since 1999. He was born in Tehran, the capital of Iran.
He has started programming by BASIC, but now he is .Net developer. AI and Low-Level programming are his favorite issues. He is good at team-working and participates in research projects.
Mohammad loves sport, specially volleyball and camping. He is such outgoing person and crazy about nature.
One of his wishes is to have laboratory same as Bell Lab, How ambitious.

To contact Mohammad, email him at shafienia[AT]gmail[DOT]com

Comments and Discussions

Generalcongratulations Pin
Andy Allinger15-Dec-13 9:53
MemberAndy Allinger15-Dec-13 9:53 
QuestionThank you Pin
Amin Ghadirian26-Jun-13 8:34
MemberAmin Ghadirian26-Jun-13 8:34 
Suggestiongood work! Pin
kspliusa28-Mar-12 20:40
Memberkspliusa28-Mar-12 20:40 
QuestionAlso excellent Pin
jrobb22914-Feb-12 11:02
Memberjrobb22914-Feb-12 11:02 
AnswerRe: Also excellent Pin
Mohammad Shafieenia27-Oct-12 1:02
MemberMohammad Shafieenia27-Oct-12 1:02 
Generalexcellent sir!!! Pin
kushal seth18-May-10 5:17
Memberkushal seth18-May-10 5:17 
GeneralRe: excellent sir!!! Pin
Mohammad Shafieenia27-Oct-12 0:59
MemberMohammad Shafieenia27-Oct-12 0:59 
GeneralNeed a break after if statement Pin
Pourya Shirazian4-Aug-09 6:48
MemberPourya Shirazian4-Aug-09 6:48 
GeneralRe: Need a break after if statement Pin
Mohammad Shafieenia5-Aug-09 7:45
MemberMohammad Shafieenia5-Aug-09 7:45 
Generalvery good! Pin
alejandro29A4-Aug-09 4:48
Memberalejandro29A4-Aug-09 4:48 
GeneralRe: very good! Pin
Mohammad Shafieenia4-Aug-09 9:22
MemberMohammad Shafieenia4-Aug-09 9:22 
GeneralExcellent Pin
Anil Srivastava29-Jul-09 23:54
MemberAnil Srivastava29-Jul-09 23:54 
GeneralRe: Excellent Pin
Mohammad Shafieenia30-Jul-09 0:35
MemberMohammad Shafieenia30-Jul-09 0:35 
GeneralWhy don't you try to implement a more generalized solution Pin
Zimmermann Stephan27-Jul-09 21:40
MemberZimmermann Stephan27-Jul-09 21:40 
Questionwhy? Pin
MSHADroo27-Jul-09 11:19
MemberMSHADroo27-Jul-09 11:19 
AnswerRe: why? Pin
MSHADroo2-Aug-09 22:35
MemberMSHADroo2-Aug-09 22:35 
Generalcongratulation Pin
MSHADroo27-Jul-09 10:33
MemberMSHADroo27-Jul-09 10:33 
GeneralRe: congratulation Pin
Mohammad Shafieenia27-Jul-09 20:37
MemberMohammad Shafieenia27-Jul-09 20:37 
GeneralEfficiency improvement Pin
supercat927-Jul-09 6:18
Membersupercat927-Jul-09 6:18 
GeneralRe: Efficiency improvement Pin
Mohammad Shafieenia27-Jul-09 7:01
MemberMohammad Shafieenia27-Jul-09 7:01 
GeneralRe: Efficiency improvement Pin
supercat927-Jul-09 18:57
Membersupercat927-Jul-09 18:57 
GeneralRe: Efficiency improvement [modified] Pin
brianma4-Aug-09 23:30
professionalbrianma4-Aug-09 23:30 
GeneralRe: Efficiency improvement Pin
supercat95-Aug-09 4:28
Membersupercat95-Aug-09 4:28 
GeneralRe: Efficiency improvement Pin
Amin Ghadirian26-Jun-13 10:23
MemberAmin Ghadirian26-Jun-13 10:23 
General"Only" up to 1142 Pin
mluri26-Jul-09 21:14
Membermluri26-Jul-09 21:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 26 Jul 2009

Tagged as


27 bookmarked