Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Calculating Factorial (Recursively & Iteratively) (http://www.go4expert.com/articles/calculating-factorial-recursively-t1757/)

pradeep 30Oct2006 18:10

Calculating Factorial (Recursively & Iteratively)
 
Calculating the factorial of a number is a basic excercise while learning to program in C, many of us have done it iteratively, it can also be done recursively. I am posting both iterative and recursive versions below.

Code: C

/* Recursive Version */
 unsigned int recursive_factorial(int n)
  {
      return n>=1 ? n * recr_factorial(n-1) : 1;
  }
   
 /* Iterative Version */
  unsigned int iter_factorial(int n)
  {
      int f = 1;
      int i;
      for(i = 1; i <= n; i++)
     {
          f *= i;
      }
      return f;
  }


bothie 24Nov2006 14:22

Re: Calculating Factorial (Recursively & Iteratively)
 
loved your functions they are just readable

deepak.mobisy 24Oct2007 22:25

Re: Calculating Factorial (Recursively & Iteratively)
 
Thanks for this kind of efoorts.

oleber 25Oct2007 12:54

Re: Calculating Factorial (Recursively & Iteratively)
 
Good work ;) but one small comment :p:

Why having int and unsigned int?

Just for coherence, and avoiding compilers work:

Code:

/* Recursive Version */
unsigned int recursive_factorial(unsigned int n)
{
    return n >= 1 ? n * recr_factorial(n-1) : 1;
}

Code:

/* Iterative Version */
unsigned int iter_factorial(unsigned int n)
{
    unsigned int f = 1;
    for(unsigned int i = 1; i <= n; i++)
    {
        f *= i;
    }
    return f;
}



shabbir 25Oct2007 17:21

Re: Calculating Factorial (Recursively & Iteratively)
 
Offtopic comment:
Oleber, I would suggest using the code block rather than manually coloring the code blocks. - http://www.go4expert.com/misc.php?do=bbcode#code

pr1nc3k1d 7Dec2007 03:52

Re: Calculating Factorial (Recursively & Iteratively)
 
Everything looks good and nice but what's if you want to calculate " 1000! " or the factorial of greater values ? :) The " unsigned int " type can memorize a value between 0 and +4,294,967,295 but " 1000! " is more much greater than the dimension of " long double " which is the greatest data type in C/C++. I think this is a good question. :) I'm waiting suggestions and ideas.

shabbir 7Dec2007 09:07

Re: Calculating Factorial (Recursively & Iteratively)
 
The greatest has also the limitation for large numbers and I think you are with the greatest "long double"

pr1nc3k1d 7Dec2007 14:28

Re: Calculating Factorial (Recursively & Iteratively)
 
Yes, it has, but i'm thinking on an algorithm which not calculates the result,but it generates it into a vector or a list. For example you can get a number with more than 1000 digits as result But if I put every digit of the number into a list I could view it and print it on the screen or into a file. So I think it could be a possible solution for this problem because there is no other data types which could memorize such a number. Opinions ?

pr1nc3k1d 8Dec2007 05:45

Re: Calculating Factorial (Recursively & Iteratively)
 
I made it but I don't really like it cuz` it's slow. It took about 6-7 minutes waiting for the result of " 1000! ".

Code:

#include <iostream.h>
#include <conio.h>
#include <time.h>
void main ()
{
        long int v[259000],i,n;
  double start;
  clrscr();
  v[0]=1;
  for(i=1;i<259000;i++) v[i]=0;
  cout<<"Enter the number: "; cin>>n;
  if(n==0 || n==1 ) cout<<n<<"!="<<"1";
  else
  {
      start = clock ();
          long int c=0;
          for(i=1;i<=n;i++)
      {
              long int j;
                    for(j=0;j<=c;j++)
          v[j]*=i;
        for(j=0;j<=c;j++)
        {
                if(v[j]>=10)
            {
                    v[j+1]=v[j+1]+v[j]/10;
              v[j]=v[j]%10;
              int k1=258999,cont=0;
              while(v[k1]==0) { cont++; k1--; }
              c=258999-cont;
            }
        }
        cout<<i<<"!=";
        for(j=c;j>=0;j--)
                        cout<<v[j];
        cout<<endl;
          }
  start=clock()-start; 
  cout<<"It took "<<start<<" seconds to complete. ";
  }
  getch();
}


pradeep 8Dec2007 10:17

Re: Calculating Factorial (Recursively & Iteratively)
 
Goodness gracious! 6-7 mins?? Try to rethink your logic!


All times are GMT +5.5. The time now is 01:21.