[RESOLVED] factorial
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] factorial

  1. #1
    Join Date
    Oct 2010
    Posts
    98

    [RESOLVED] factorial

    hi all,

    Please tell me the logic by which i can calculate the factorial of a number, my own logic has failed.
    please help...


    many thanks!!

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: factorial

    Show us your code and we'll tell you what you did wrong. Factorial is very easy, and I have a really good method for it, but this sounds like homework.
    Last edited by ninja9578; October 23rd, 2010 at 10:13 AM.

  3. #3
    Join Date
    Oct 2010
    Posts
    98

    Re: factorial

    ok, i've made a function:

    void func(int num)
    {
    int result;
    for(int i=1; i<=num; i++)
    {
    result=result*i;
    }
    cout<<"answer is:"<<result;
    }

    and i've called it in main.

    void main()
    {
    int a;
    cin>>a;
    func(a);
    }


    Please tell me what i'm doing wrong??

    many thanks!!!

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    Re: factorial

    You did not initialise the result variable.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: factorial

    You're also missing a few special cases, you also probably don't want to use an int as you'll run out of room REALLY fast. Here is my function:

    Just change number_t to whatever type you want, and adjust MAX_FACTORIAL to your liking, or leave it. Computers have memory to burn, and even though some computers and compilers will run out of range long before 1755, none will run beyond it.
    Code:
    typedef long double number_t;  //replace with whatever you want
    #define MAX_FACTORIAL 1755  //Use getMax to figure out what to set this to, 1755 is for 128-bit long doubles
    number_t factorial(int n){  
        //Static Lookup tables and checking for static constants
        static number_t preCalculated[MAX_FACTORIAL] = {0};  //Set everything to 0, since it is an impossible result
        #ifndef INFINITY
    	   const static number_t INFINITY = (number_t)(1.0f/0.0f);
        #endif
        #ifndef NAN
    	   const static number_t NAN = (number_t)sqrt(-1);
        #endif
        
        //Check special cases
        if (n > MAX_FACTORIAL - 1){  //Know it's going to overflow, so don't bother calculating
    	   return INFINITY;
        } else if (n < 0){  //Factorial of negative numbers in undefined
    	   return NAN;
        } else if (n < 2){  //0! and 1! both resolve to 1
    	   return 1;
        } else if (preCalculated[n] != 0){  //already solved for this value, why solve again?
    	   return preCalculated[n];
        }
        
        //Need to calculate it
        number_t result = (number_t)n;
        for (unsigned int value = (unsigned int)n - 1; value > 1; --value){
    	   result *= (number_t)value;
        }
        
        //store the result for later and return
        preCalculated[n] = result;
        return result;
    }
    Code:
    //Use this to determine what to define MAX_FACTORIAL to be, then you can remove it
    unsigned int getMax(void){
        for (unsigned int n = 2; n < 5000; ++n){
    	   number_t result = (number_t)n;
    	   for (unsigned int value = n - 1; value > 1; --value){
    		  result *= (number_t)value;
    	   }  
    	   
    	   if (!isfinite(result)) return n;
        }
        return 5000;
    }
    Last edited by ninja9578; October 24th, 2010 at 08:32 AM.

  6. #6
    Join Date
    Oct 2010
    Posts
    98

    Re: factorial

    Thanks both of you..

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: factorial

    ninja's code is far more than you need for a typical homework assignment, FYI.

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Talking Re: factorial

    Hi ninja9578, I thought we were through with that since http://www.codeguru.com/forum/showthread.php?t=503561...

    Quote Originally Posted by ninja9578 View Post
    Code:
        #ifndef INFINITY
    	   const static number_t INFINITY = (number_t)(1.0f/0.0f);
        #endif
        #ifndef NAN
    	   const static number_t NAN = (number_t)sqrt(-1);
        #endif
    VC++ 2010 didn't even compile your INFINITY: error C2124 - guess what the message text was...

    The NAN was rejected with a complaint about an ambiguous overload resolution. After changing the numeric literal to -1.0 it compiled and cout gave me -1.#IND as the output for that. I didn't try that however with <complex> included and thus don't know if that would have made any difference, as we most likely all know that sqrt(-1) is i from the math point of view.

    The processor manuals for both the 486 and the Pentium 4 I consulted about that weren't really clear about what the FPU would make out of this input.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center