 # A fast Computation of N! ( Factorial) instead of recursion... Email
 Submitted on: 1/4/2015 6:49:00 AM By: michele berardi (from psc cd) Level: Advanced User Rating:   By 3 Users Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++ Views: 9441

Quickly you can obtain your factorial number, and of course this code explain the theory you can optimize more and more this.

code:
Can't Copy and Paste this?
 ``` //************************************** // Name: A fast Computation of N! ( Factorial) instead of recursion... // Description:Quickly you can obtain your factorial number, and of course this code explain the theory you can optimize more and more this. // By: michele berardi (from psc cd) //************************************** /* Quick Factorial Computation (you can use any types instead of long!) (C) 2002 Berardi Michele http://web.tiscali.it/mberardi E-Mail(s): 03473192000@vizzavi.it mfxaub@tin.it */ #include void main() { long N , b , c , p ; // use int for fast calculation and small range of calculation.. // use double for factor bigger than 12! Restart: printf("Enter N!: "); scanf("%d", &N); if(N < 0) { printf("\t\n R.estart, I.nvalid P.arameter! (N > -1)\n"); goto Restart; } else c = N - 1; p = 1; while(c>0) { p = 0; b = c; while(b>0) { if ( b & 1 ) { p += N; // p = p + N; } // if you would like to use double choose the alternative forms instead shifts // the code is fast even! // you can use the same tips on double or 64 bit int etc.... but you must... ;-) b >>=1; // b/=2; (b = b / 2;) ( b >> 1; a.s.r. is more efficent for int or long..!) N <<=1; // N += N; N = N + N; N = N * 2; (N <<=1; a.s.l. is more efficent for int or long..!) } // end of: while(b>0) N = p; c--; // c = c - 1; } // end of: while(c > 0) printf("[%d] is the factorial! \n", p); } //p.s. //using recursion is much slower! //for example: // //int fact(int); //void main() //{ // int N; // printf("Enter the number: "); // do // { scanf("%d",&N);} // while(N<0); // printf("\n %d", fact(N)); // } // int fact(int N) // { // int f; // if(N==0)f=1; // else f=N*(fact(N-1)); // return f; // } ```

Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)