Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C

1000 Factorial

Rate me:
Please Sign up or sign in to vote.
4.54/5 (29 votes)
27 Jul 2009CPOL4 min read 138.4K   2.9K   27  
Optimum algorithm for calculating factorial of large number
  • fact.zip
    • fact
      • FACT.BAK
      • FACT.CPP
      • FACT.EXE
  • fact2.zip
    • fact2
      • FACT2.BAK
      • FACT2.CPP
      • FACT2.EXE
//////////////////////////////////////////////////////////////////////////////
// Author : Mohammad Shafieenia
// Email : shafienia@gmail.com
//
// CopyRight License : CPOL
// For more information refer to : www.codeproject.com
//////////////////////////////////////////////////////////////////////////////

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

void main()
{
clrscr();
int s[3000];
int a[3000];
int j,r,k;
register int i;
for(i=0;i<3000;i++)
 {
  s[i]=0;
  a[i]=0;
 }
a[2999]=1;
s[2999]=1;

for(k=1;k<1000;k++)
{
  for(j=0;j<k;j++)
   {
    for(i=2999;i>=0;i--)
     {
      r=s[i]+a[i];
      if(r>9)
       {
	s[i-1]+=r/10;
	s[i]=r%10;
       }
      else
       s[i]=r;
     }
   }
for(i=0;i<3000;i++)
 a[i]=s[i];
}
k=0;
for(i=0;i<3000;i++)
if (s[i]!=0 | k==1)
{
 cout<<s[i];
 k=1;
}
getch();
}



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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
www.iranexam.net

Comments and Discussions