1. C++ Program Help

Hi, new to the forum but I have read and understand u guys aren't here to do this for me and I know that and want to learn how to do this.

My goal is to write a program that to compute the following sum: 1+2*3+3*4*5+4*5*6*7+5*6*7*8*9+6*7*8*9*10*11+...+n* (n+1)*(n+2)*...*(2n-1)

Now me and some people figured this to be the same thing as
(2n-1)!/(n-1)!

here is what I have come up with so far but I am sure it's not correct any tips, suggestions, or constructive criticism is welcomed.

Code:
```#include <iostream>
using namespace std;

int main()
{

int k, n, total=0, sum=0;

cout << "Enter a whole number: \n";
cin >> n;
k = n;

while (n>=1)
{
while (k<=2*n-1)
{
sum = k * ++k;
}
total= total+sum;
k = --n;
--n;
}

cout << "The answer to the equation for your number is ";
cout << total << endl;

return 0;
}```
I know this isnt right b/c if n = 3 the answer output from the program should be 67 b/c
(5!/2!) + (3!/1!) + (1!/0!) = 67 but I get 72 when I run the program.

Thanks and sorry for the long post, me and my friends are just noobs trying to learn from mistakes.

2. Originally Posted by yankees07
Now me and some people figured this to be the same thing as
(2n-1)!/(n-1)!
If that's the case, then this should be easy.

Write a function to compute the factorial of a number. Then use that function to compute (2n -1)! and (n - 1)!. Then it's just a matter of simply dividing.

3. Code:
`sum = k * ++k;`
I believe you shouldn't do something like this in C++. I think the compiler is free to do ++k first and the result would be undefined.

Rather
Code:
```sum = k * (k + 1);
++k;```
My math skills are very rusty, but your formula doesn't look right. If n = 3 then
1 + 2 * 3 + 3 * 4 * 5 = 67
and
(6-1)! / (3-1) ! = 5! / 2! = 60

You are quite certainly supposed to program this iteratively (dumbly doing all the calculations and steps shown in the requirements). Programmers don't need math because computers are fast!

4. Ok I figured out how to get it to answer the (2n-1)!/(n-1)! which was way easier than i thought it would be thanks for the comment on it MacGyver. here is the working code:
Code:
```#include <iostream>
using namespace std;

int factorial ( int n )
{
int result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}

int main()
{
int x, q, r;
cout<<"Enter a number: \n";
cin >> x;
q = (2 * x - 1);
r = (x - 1);

cout<<"The answer is "<< factorial(q)/factorial(r) <<endl;

return 0;
}```
Anon in my example of n =3 this (2n-1)!/(n-1) should be done for n= 3....all the way until it's counted down to one.

For example if I type in 3 (just using 3 b/c it's small) the program should compute:

(2(3)-1)!/(3-1)! + (2(2)-1)!/(2-1)! + (2(1)-1)!/(1-1)!) which is simplified to this:

(5!/2!) + (3!/1!) + (1!/0!) = 67 (note 0! = 1)

the program I just posted only computes the first one (5!/2!) when i enter in 3 but I think I should have some kind of loop inserted to countdown from what the user inputs to 1 but I am having trouble with this. any tips?

5. My goal is to write a program that to compute the following sum: 1+2*3+3*4*5+4*5*6*7+5*6*7*8*9+6*7*8*9*10*11+...+n* (n+1)*(n+2)*...*(2n-1)

Now me and some people figured this to be the same thing as
(2n-1)!/(n-1)!
Not quite. That is the same thing as the sum from 1 to n of (2n-1)!/(n-1)!.

Writing a function to compute factorial is easy, but here's the limitation: for a 32 bit int, the best you can do is compute 11!. 12! is beyond the range of a 32 bit unsigned int. On the other hand, 19!/12! is well within the range of a 32 bit signed int, even though 19! and 12! are both too large.

The way to overcome this limitation is to compute the product of the integers from 13 to 19 inclusive. Effectively, you just need to note that:
(2n-1)!/(n-1)! = n * (n+1) * ... * (2n-1)
Since the 1 * 2 * ... * (n-1) can be cancelled out.

This product computation is trivial with a for loop. Then, all you need is another outer for loop to loop from 1 to n and take the sum of the products. Ironically, this is what you were directed to do in the first place.

6. @yankees07, indent your code. You'll find it easier to follow, and people will be more inclined to help you.

Also, using braces in all the optional places will save you from some hard to find problems caused by slipping from "optional" to "necessary" without realising it.
Code:
```#include <iostream>
using namespace std;

int factorial(int n)
{
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}

int main()
{
int x, q, r;
cout << "Enter a number: \n";
cin >> x;
q = (2 * x - 1);
r = (x - 1);

cout << "The answer is " << factorial(q) / factorial(r) << endl;

return 0;
}```