Using Recursion to Calculate (n + r)/(n * r)

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    Recursion
Click For Summary

Discussion Overview

The discussion revolves around the use of recursion to calculate the expression (n + r)/(n * r), with a focus on implementing this in code. Participants explore the correct mathematical formulation for combinations and the challenges of achieving accurate integer results in programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares a code snippet intended to calculate a recursive function but reports incorrect results.
  • Another participant points out that the formula used in the code is incorrect and provides the correct formula for combinations, $$\frac {(n + r)!}{n! \cdot r!}$$.
  • Some participants note that the code does not handle integer division correctly, leading to results of 0 for certain inputs.
  • There is a discussion about the need for the function to return integer values only, as it is meant to calculate combinations.
  • One participant suggests that the recursive function could be structured to reduce calculations by calling itself with (n, r-1) instead.
  • Another participant mentions that the order of operations in the code affects the results due to integer division.
  • There is a question about whether the function is still recursive if it does not call itself continuously.
  • Participants express differing results when testing the function with specific values for n and r, indicating confusion over the implementation.

Areas of Agreement / Disagreement

Participants generally disagree on the correctness of the formula used in the initial code. There is no consensus on the implementation details, and multiple competing views on how to structure the recursive function remain unresolved.

Contextual Notes

Some participants highlight limitations in the original code, such as incorrect handling of integer division and the need for parentheses in mathematical expressions. The discussion also reflects varying interpretations of the parameters n and r in the context of combinations.

Who May Find This Useful

Readers interested in programming recursive functions, combinatorial mathematics, or debugging code related to mathematical calculations may find this discussion relevant.

anonim
Messages
39
Reaction score
2
Homework Statement
Calculate math formula
Relevant Equations
(n+r)!/(n!.r!)
I wirte code but it does not work. I entered n=5 and r=2, result is 0. And I want to calculate this recursively.
Code:
#include<stdio.h>
int func(int n, int r);
int main(){
    int n,r;
    int result=0;
    printf("Enter the number:\n");
    scanf("%d",&n);
    printf("Enter the 2.number:\n");
    scanf("%d",&r);
  
   result=func(n,r);
   printf("%d",result);
  
    return(0);
}
int func(int n, int r){
    if(r==1){
        return(n);
    }
   if(n>0 && r>0){
       return ((n+r) / (n*r))* func(n-1,r-1);
   }}
 
Physics news on Phys.org
anonim said:
C:
    return ((n+r) / (n*r))* func(n-1,r-1);
For one thing this isn't the right formula.

$$\frac {(n + r)!}{n! \cdot r!} = \frac{n + r}{n \cdot r} \frac{(n + r - 1)!}{(n - 1)!(r - 1)!}$$

Your code might have other problems, but this is the first thing that jumped out at me.
 
  • Like
Likes   Reactions: berkeman
Mark44 said:
For one thing this isn't the right formula.

$$\frac {(n + r)!}{n! \cdot r!} = \frac{n + r}{n \cdot r} \frac{(n + r - 1)!}{(n - 1)!(r - 1)!}$$

Your code might have other problems, but this is the first thing that jumped out at me.
Yes, you are right. It also doesn't return the true result of the division. For example if 5/6 it returns 0. But I have to write int function.
 
anonim said:
Yes, you are right. It also doesn't return the true result of the division. For example if 5/6 it returns 0. But I have to write int function.
It shouldn't return a fraction such as 5/6. The result should only be an integer. What you're trying to calculate is the number of combinations of ##n + r## things taken ##r## at a time. This is defined as
$$\frac{(n + r)!}{n! \cdot r!}$$.

For example, the number of combinations of 5 things taken 2 at a time is ##\frac{5!}{(5 - 2)! \cdot 2!} = \frac{5!}{3! \cdot 2!} = 10##. These combinations always come out with integer values of at least 1.
 
Mark44 said:
It shouldn't return a fraction such as 5/6. The result should only be an integer. What you're trying to calculate is the number of combinations of ##n + r## things taken ##r## at a time. This is defined as
$$\frac{(n + r)!}{n! \cdot r!}$$.

For example, the number of combinations of 5 things taken 2 at a time is ##\frac{5!}{(5 - 2)! \cdot 2!} = \frac{5!}{3! \cdot 2!} = 10##. These combinations always come out with integer values of at least 1.
Okay if n=5 and r=5--> 10!/5!*5! is not equal 5!/0!*5!
 
anonim said:
Okay if n=5 and r=5--> 10!/5!*5! is not equal 5!/0!*5!
First off, please use parentheses when they are needed.
10!/5!*5! means ##\frac {10!}{5!} \cdot 5!##, which I don't think is what you meant.
If you don't use LaTeX, write it as 10!/(5! * 5!).
Second, ##\frac {10!}{5! 5!} = \binom {10} 5##, which is the number of combinations of 10 things taken 5 at a time, and is equal to 36. This is sometimes written as ##_{10}C^5##.
 
anonym said:
C:
int func(int n, int r){
    if(r==1){
        return(n);
    }
   if(n>0 && r>0){
       return ((n+r) / (n*r))* func(n-1,r-1);
   }
}
I was able to write some code that seems to work, but I changed the meaning of the two parameters. In your function you are using n and r to mean the number of combinations of (n + r) things taken n at a time, and this is defined as
$$\frac{(n + r)!}{n! \cdot r!}$$
By symmetry, this is exactly the same as (n + r) things taken r at a time.

In my code, I'm looking that the number of combinations of N things taken k and a time, or
$$\frac {N!}{(N - k)! \cdot k!}$$
My N is your n + r, and my N - k is your r.

One thing I found is that if you do the division too soon, the integer division produces incorrect results. To counter this, I added an extra line.

In your code, which I believe uses an incorrect formula, the change would look like this:
C:
int func(int n, int r){
    int result;
    if(r==1){
        return(n);
    }
   if(n>0 && r>0){
       result = func(n - 1, r - 1);
       return result * (n + r) / (n * r);
   }
}
If you save the division for last, you won't have the problem of your code evaluating something like 5/6, resulting in a value of 0.

Again, though, your formula is wrong and still needs to be fixed.
 
Mark44 said:
Again, though, your formula is wrong and still needs to be fixed.
Yes. Compare the numerators wit both (n,r) and (n-1, r-1)

It might be easier to calculate func(n, r) from func (n, r-1). Less calculations for each step, and the same number of steps.
 
Mark44 said:
I was able to write some code that seems to work, but I changed the meaning of the two parameters. In your function you are using n and r to mean the number of combinations of (n + r) things taken n at a time, and this is defined as
$$\frac{(n + r)!}{n! \cdot r!}$$
By symmetry, this is exactly the same as (n + r) things taken r at a time.

In my code, I'm looking that the number of combinations of N things taken k and a time, or
$$\frac {N!}{(N - k)! \cdot k!}$$
My N is your n + r, and my N - k is your r.

One thing I found is that if you do the division too soon, the integer division produces incorrect results. To counter this, I added an extra line.

In your code, which I believe uses an incorrect formula, the change would look like this:
C:
int func(int n, int r){
    int result;
    if(r==1){
        return(n);
    }
   if(n>0 && r>0){
       result = func(n - 1, r - 1);
       return result * (n + r) / (n * r);
   }
}
If you save the division for last, you won't have the problem of your code evaluating something like 5/6, resulting in a value of 0.

Again, though, your formula is wrong and still needs to be fixed.
Shouldn't function constantly call itself? Is this still recursion?
 
  • #10
anonim said:
Shouldn't function constantly call itself? Is this still recursion?
The version I wrote does call itself. Look at the next-to-last line of code.
C:
result = func(n - 1, r - 1);
 
  • #11
Mark44 said:
The version I wrote does call itself. Look at the next-to-last line of code.
C:
result = func(n - 1, r - 1);
Yes, right. But it doesn't work if I entered 5 and 2 , output-2. But if n-5 and r-5 ->output-21
 
  • #12
anonim said:
Yes, right. But it doesn't work if I entered 5 and 2 , output-2. But if n-5 and r-5 ->output-21
Like I said before, your formula is incorrect.
In my code, which I haven't shown, entering 5 and 2 gives 10, which is the correct result for the number of combinations of 5 things taken 2 at a time.

In your code, if n = 5 and r = 2, then you should be getting the number of combinations of 7 things (5 + 2) taken 2 at a time, which is 21.

In post #2, I pointed out what was wrong with your code.
 
  • Like
Likes   Reactions: sysprog

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K