-1

So I´ve been trying to solve this problem, but I just can´t. The thing is, I put a number of row in triangle, (can be any random number/row) and the program should return the sum of the squares of all the coefficients in that row.

#include <stdio.h>

unsigned long sum_triangle(const int row);

int main(){
 printf("%lu\n", sum_triangle(7));

 // sum(7)....7 is number of row

return 0;
}

unsigned long sum_triangle(const int row){
unsigned long sum = 0;
int temp =0;
for (int line = 0; line <= row ; line++) {
    for (int number = 0; number <= line ; number++){
        if (line == row){
            temp = THIS NEEDS TO BE COMBINED SOMEHOW(line , number);
            sum += (temp * temp);
        }
    }
 }
return sum;
}

Problem is in the "temp". I need to somehow combine row and number and thats it. Any help would be much appreciated.

  • 1
    No, you can't vandalize your question, especially if there are answers people put an effort into. – Eugene Sh. Nov 03 '21 at 21:24
  • The questions and answers are supposed to help future readers, not just you. Also you agreed to the [terms of service](https://stackoverflow.com/help/licensing), which permit SO to distribute your content even if you no longer want them to. – HolyBlackCat Nov 03 '21 at 21:27
  • Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under a [CC BY-SA license](//creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: [How does deleting work? …](//meta.stackexchange.com/q/5221). If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. – Makyen Nov 03 '21 at 21:31
  • Editing Questions to improve them (e.g. clarification, adding information, etc.) *is encouraged*. However, editing a Question into a different question is against policy when doing so invalidates one or more answers, even if the original question isn't what you really intended to ask. Your edit here invalidated an answer. Users with edit privileges should proactively revert such changes. I've reverted your edit. You're *encouraged to [ask a new Question](/questions/ask)*, perhaps linking this one for additional context. We want to help, but your new/additional issue needs to be a new Question. – Makyen Nov 03 '21 at 22:45
  • @Makyen Im sorry but if you read [link](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question) there s this point : If your question is unnecessarily specific, edit to generalize it. So I did that, I also eddited the answer, so where s the problem – helperman200 Nov 03 '21 at 22:53
  • @helperman200 At the time I loaded the page, your suggested edit on the answer didn't exist. Your suggested edit on the answer makes the type used for `c` and `i` inconsistent with the rest of the calculation and returns the wrong type (i.e. if I were to review it now, I'd reject it). Given your intervening edits to your question, it's no longer simple for me to just rollback my rollback and fix your suggested edit to the answer. Another time, please don't make the issue even more complex. Fix your suggested edit & make edits to the question consistent with the revised changes to the answer. – Makyen Nov 03 '21 at 23:21

1 Answers1

1

There is a formula for the sum of squares of a row:

sum of row m squares = binomial(2m, m)

Thus all you need is to implement a proper binomial that doesn't overflow on reasonably sized integers. Coincidentally that Wikipedia article already has an implementation in C. Combining it all together:

// from https://en.wikipedia.org/wiki/Binomial_coefficient
unsigned long binomial(unsigned long n, unsigned long k) {
    unsigned long c = 1, i;
    if (k > n-k)
        k = n-k;
    for (i = 1; i <= k; i++, n--)
        c = c / i * n + c % i * n / i;
    return c;
}

unsigned long sum_triangle(int m) {
    return binomial(2*m, m);
}
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220