Thread: recursive factorial function

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    recursive factorial function

    I am having trouble getting started writing a recursive function to calculate a factorial, like this:

    n!/r!*(n-r)!

    Where n and r and numbers entered by the user and the ! represents the factorial process. It needs to be a single function.

    Any ideas on getting started?
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You could try naming the successive calls to the function different names to keep things straight in your mind, and then at the end change them all to the same name.
    Last edited by 7stud; 05-07-2003 at 02:16 PM.

  3. #3
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    Here is what I have so far:

    Code:
     #include <iostream>
    
     unsigned long factorial(unsigned long n, unsigned long r);	//	function prototype
    
     int main()
     {
    	 unsigned long answer, things, sets;
    	 cout << "Please enter the number of items: " << endl;
    	 cin >> things;
    	 cout << "Please enter the number of sets: " << endl;
    	 cin >> sets;
    	 answer = factorial (things, sets);
    	 cout << "The answer is: " << answer << endl;
    
    	return 0;
    
    }
    
    
    unsigned long factorial(unsigned long n, unsigned long r)	//	recursive function
    {
    	unsigned long a, b;
    
    	while (n > 1)
    	{
    		a = n * factorial((n -1), r);
    	}
    
    	while (r >1)
    	{
    		b = r * factorial((r-1), n);
    	}
    
    	if (r <= 1 && n <= 1)
    		return  a/( b *(a-b));
    }
    This will compile, but not much else.
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why does it "have" to be one function. It certainly won't work the way you've done it, I'm afraid.

    Something like this might be better:
    ans = factorial(n) / factorial(r) * (n - r)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    I agree that would be a simpler and more direct solution. How would I get both my variables into the function in the first place?
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    Have you tried looking on the Internet? Also, how about some CS texts or algorithm books. This should be simple.
    Mr. C: Author and Instructor

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A correct recursive factorial function would be
    Code:
    unsigned int fac(unsigned int i)
    {
      return (i <= 1) ? 1 : (i * fac(i-1));
    }
    Or iterative:
    Code:
    unsigned int fac(unsigned int i)
    {
      int ret = 1;
      for(;i>1;--i)
        ret *= i;
      return ret;
    }
    Iterative is better of course.

    To calculate this
    n!/r!*(n-r)!
    you would write
    Code:
    unsigned int res = fac(n)/fac(r)*fac(n-r);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM