Sum of the Series

Pages: 12
Hello,

I'm trying to figure out how I might do this:

Calculate the sum of the series Sn, where Sn is the sum, from i = 1 to n, of ( n + n2 ) / 3.

Although I truly appreciate anyone who is willing to help me with this in any way, I will say that simply linking me to a tutorial/article is not my idea of being helpful.

Thanks!
Last edited on
should it be (i + i^2)/3 ?

Like [tex]\sum\limits_{i=1}^{n}\frac{i+i^2}{3}[/tex]
.
Last edited on
As it is all that would happen is a multiplication. Unless you use the sequence I said.

A sum from i=1 to n of (n+n^2)/3 would just be n x (n+n^2)/3.
.
Last edited on
Here is the question:

Write a program to compute, and later output to the screen, the sum of the series Sn, where Sn is the sum, from i = 1 to n, of ( n + n2 ) / 3. The value n, is entered, via the keyboard, by the user.
What have you tried?

Everything you need is in my posts.

You just need to code it in a loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
using namespace std;

int main () {

	int i = 1;
	double Sn, n;

	cout << "Please enter a value for n: ";
	cin >> n;

	do {

		( Sn = n + pow(n,2) ) / 3; }

	while ( i = 1 );

	cout << Sn;

	system("PAUSE");
	return 0;
}


I know this is wrong. I also know that I have no idea what is right.
Last edited on
I also know I have no idea what you meant by this:

[tex]\sum\limits_{i=1}^{n}\frac{i+i^2}{3}[/tex]
It says write a program. It doesn't say what language to use, right?

If you wanted to print the addition of numbers 1 through 10, could you do that?

using 'bash' script as an example:

1
2
3
4
5
declare -i i sum=0 n=10
for ((i=1; i<=n; ++i)); do
  sum=$sum+$i
done
printf "sum is %d\n" "$sum"


Now the divide by 3, that's done for everything, so do it outside of the loop.

You also want to toss in n2, so in the above loop you would calculate "$i*$i" and add it to sum as well... preferably in the same line like

sum=$sum+$i+$i*$i

For the final answer, after the done and before you print, divide '$sum' by 3:

sum=$sum/3

Now since you are posting this in a C++ forum, you likely want a c++ answer, but certainly you wouldn't want me to give you that much help. But the above is *how* you would do it. As for what language you do it in, that's between you and your teacher... ;-)

The above will work in a 'bash' "shell" on linux or in "cygwin" on windows. and will give an 'integer' answer (bash doesn't do floats or doubles), but hopefully that should give you an idea...? ;-)

http://www.tlhiv.org/ltxpreview/

It is latex code.

replace the [tex] ...[/tex] with $$ in the link and you will see a summation.

Anyways, n is an integer not a double. You are not increasing i either. You need some increments in there.

Condition (while i = 1) ? You are summing so this needs changing. Some loop involving i and n.

You have declared stuff but have not initialised it, not sure if this is good practise, I would always initialise what needs initialising. (" Good practice is to always initialize variables to a value in this language" Grex)

You have Sn in brackets too (line 15), I doubt you want this line as it is. Sum means what?

If you write out the maths on some paper first it might help with the code. Like try giving some value to n.

Also pow is slower than just typing n*n.

Is this hobby or coursework? What things have you covered so far?
Last edited on
From a pure mathematics standpoint, what does "where Sn is the sum, from i = 1 to n, of ( n + n2 ) / 3" even mean? Forgive me, part of the reason I'm struggling with this stuff is that I really don't have a sufficient math background. I guess I just don't really understand what this question is actually asking me to do.

where Sn is the sum, from i = 1 to n, of ( n + n2 ) / 3.

How does the from i=1 to n mean?
Last edited on
Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main () {

	int i = 1;
	int n, Sn, sSum

	cout << "Please enter a value for n: ";
	cin >> n;	

/*	For each value in the series i to n, run the equation

	Sn = ( n + n2 ) / 3

	Then, the sum of the value(s) of Sn equal sSum	*/

	cout << sSum;

	system("PAUSE");
	return 0;
}


But how would I write that loop?
Last edited on
closed account (D80DSL3A)
How does the from i=1 to n mean?

Not knowing that would present a difficulty, for sure!
It means:
Sn = (1 + 1*1 )/3 + (2 + 2*2 )/3 + (3 + 3*3 )/3 +...+ (n + n*n )/3

Use a for loop
for(int i=1; i<=n; ++i)
// sum the terms
Thank you for your help!

This is the loop I've got now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main () {

	int i = 1;
	int n, Sn, sSum = 1;

	cout << "Please enter a value for n: ";
	cin >> n;

    for( i; i <= n; i++)
    {                   
        Sn = ( n + n*2 ) / 3 / 3;
        sSum += Sn
    }
	cout << sSum;

	return 0;
}


But I made a mistake in the equation, it's suppose to be:

Sn = ( n + n2) / 3

However, when I make that change in the code to:

Sn = ( n + pow(n,2)) / 3

The "pow" has a squiggly red line under it, and I get an error message:

IntelliSense: more than one instance of overloaded function "pow" matches the argument list

I don't understand why it wont let me make that change.
Last edited on
Nevermind. I've got it now. Thanks again!
Here are some links

This explains summation notation

https://www.youtube.com/watch?v=hEPk36Yncxg

This is a good resource for GCSE~ IB ~ A-Level Maths concepts

http://www.examsolutions.net/#

There is misunderstanding of the notation in here.

If you have a sum from i = 1 to n of (n+n^2)/3 then n is fixed to whatever you input, so you will just have a multiplication.

If you choose n = 2 you will have sum i = 1 to 2 of (2+2^2)/3 which is (2+2^2)/3 + (2+2^2)/3.

If you have a sum from i = 1 to n of (i+i^2)/3 now i is variable, so you will have a series of additions tending to infinity.

Gauss found a closed form for the sum formula when he was 11...What a guy.

Line 6 initialising i = 1 outside the loop condition?
Also line 7, you have given value 1 to things that don't want 1.
Line 14 you are dividing by 3 twice?

To use pow you need to include something.

Mark as solved once you have what you need, cheers.

Plus try not to have multiple threads asking the same question.
Last edited on
//take it
#include<iostream>
using namespace std;
void main()
{
int i,n,sn=0;
cout<<"enter the nth value:";
cin>>n;
for(i=1;i<=n;i++)
{
sn+=(i+i*i)/3;
}
cout<<sn;
cout<<endl;
system("pause");
}




}


Here is what I've got now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream>
#include <math.h>
using namespace std;

int main () {

	int i = 1;
	int n, Sn, sSum = 1;

	cout << "Please enter a value for n: ";
	cin >> n;

    for( i; i <= n; i++)
    {                    
        Sn = ( n + pow(n,2) ) / 3;
        sSum += Sn;
    }
	
	cout << sSum;

    system("PAUSE");
	return 0;
}

I've included <math.h>, but that has not solved the problem of being able to use the pow function.

I'm still having the same issue with this:

Sn = ( n + pow(n,2)) / 3

The "pow" has a squiggly red line under it, and I get an error message:

IntelliSense: more than one instance of overloaded function "pow" matches the argument list

I still don't understand.

Last edited on
Pages: 12