Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: basic recursion problem - code needs to be modified

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default basic recursion problem - code needs to be modified

    I need help fixing this! I just keep getting a million errors urgh

    /* The formula for computing the number of ways of choosing r different things from a set of n things is the
    following: C(n, r) = n! / (r! * (n-r)!)
    Write a recursive program (name the class that contains the main method Activity2) that executes C(n,
    r) three times and prints the results. Your execution should use the (n, r) pairs as follows: (2, 4), (5, 3), and
    (24, 12). Hard-code these values and calls to the recursive method into your program. (Do not prompt the
    user to enter them.)
    Note that you will need to devise and program a recursive method that calculates the factorial of a value. In
    the event any of the pairs perform a calculation that throws any exception, catch the exception, print a
    suitable informative message and continue processing. */
     
    import java.math.BigInteger;
     
    public class Activity2 {
     
    public static void main(String[] args) {
     
    try
    {
    int n=2; 
    int r=4; 
    System.out.print("C("+n+","+r+"): "); 
    int fn=factorial(n); 
    int fr=factorial(r);
    int fnr=factorial(n-r);  
    int ncr=fn/(fr*fnr); 
    System.out.println(ncr); 
    } 
    catch (Exception e) {}
    } 
    static int factorial(int n){ 
    	if (n==0)
    		return 1;
    	else
    		return(n*factorial(n-1));
    } 
     
    }


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: basic recursion problem - code needs to be modified

    Please post the the first errors that appear

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: basic recursion problem - code needs to be modified

    I think i found what is the problem.You are likely to get a stackOverFlowError.Why?
    Because of a call of factorial with a negative number.Here is the call
    int fnr=factorial(n-r);//n-r=4-2=-2
    So go to function factorial and see what happens
    static int factorial(int n){ 
    	if (n==0)//that's when the recursion is going to stop
    		return 1;
    	else
    		return(n*factorial(n-1));
    }
    n must reach value 0 in order the recursion to stop.However when n is negative,then the recursion is going to be made by n decremented by one.So in your case,you have -2 then you call factorial with n-1=-2-1=-3 and so on.So n never reaches value zero.As a result the recursion is infinite <-that's bad

    As you know factorial for negative ints can not stand logic.If you want to expand to negative(non-int) numbers there is the γάμα (gamma) function.
    See at this link for more:
    http://answers.yahoo.com/question/in...5190020AAZs7E5
    Last edited by Samaras; August 6th, 2012 at 08:33 AM.

Similar Threads

  1. [SOLVED] Using recursion issues for basic program
    By ash12 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 5th, 2012, 10:40 PM
  2. [SOLVED] Basic Code Problem
    By msinc210 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 3rd, 2012, 04:08 PM
  3. recursion code
    By Java_boy in forum Algorithms & Recursion
    Replies: 8
    Last Post: May 27th, 2012, 11:12 AM
  4. [SOLVED] Very basic code problem
    By frederick213 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 12:33 PM
  5. recursion problem, please help
    By nil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2010, 12:59 PM