0

I cannot find a way to code the following formula. My goal is to have a single function into which I can input the values. The context of this calculation is to return the number of possible upDown permutations of n-numbers. Without using bruteforce and checking each permutation, i can simply compute this sum:
formula
My code at the moment is this:

int e(int i, int n)
{
    if (n == 0)
        return (0);
    else
        return (combinations(n, i)*e(n, i)*e(n, n-i));
}

int main(int argc, char *argv[])
{
    int n, sum = 0;
    scanf("%d",&n);
    for (int i = 0; i < n; i++)
    {
        sum = sum + combinations(n, i)*e(n, i)*e(n, n-i);
    }
    return (0);
}
Cornul11
  • 191
  • 1
  • 12
  • What is combinari? – Derek Brown Oct 16 '17 at 00:57
  • @DerekBrown, a typo, fixed it, thanks. – Cornul11 Oct 16 '17 at 00:59
  • Same question though- what is combinations? – Derek Brown Oct 16 '17 at 00:59
  • Combinations as we know them in the mathematical meaning, i just didn't include the function for it. (n!/(k!(n-k)!)) – Cornul11 Oct 16 '17 at 01:01
  • 1
    My suggestion would be to rewrite this without recursion. Recursion is inefficient, as it requires recalculating an entire tree of values. – Derek Brown Oct 16 '17 at 01:03
  • Use a `for` loop to do the sum., Also the formula defines E_n+1, not E_n – M.M Oct 16 '17 at 03:20
  • 1
    Note that a 32-bit `int` can only store up to `12!`; `13!` is too big to fit. A 64-bit `long long` can only store up to `20!`; `21!` is too big to fit. You'll need to review your computations to ensure you avoid overflow. – Jonathan Leffler Oct 16 '17 at 04:05
  • @JonathanLeffler, My input limit is 11, so not such problems should occur. – Cornul11 Oct 18 '17 at 23:15
  • If you only need factorials to 11, use a precomputed table with the 11 possible values. – Jonathan Leffler Oct 18 '17 at 23:50
  • @JonathanLeffler i'm asked to compute those numbers, not to hardcode them. – Cornul11 Oct 18 '17 at 23:55
  • Shrug: that's not the way you'd do it in real life if the range was only to 11. If the range was bigger, you might still use precomputed values, but they'd be stored in an array of `double` values since you rapidly run out of room in any integer type. However, if the instructions are "thou shalt", then you better had do as instructed. Just remember that the instructions may not be the best solution in the 'real world' (if anything on a computer is 'real'). – Jonathan Leffler Oct 19 '17 at 00:14

0 Answers0