How would you make a loop to solve the following summation: sqrt (3) / (1 x 2) + sqrt (4) / (2 x 3) + sqrt (5) / (3 x 4) + ... + sqrt (99) / (97 x 98) + sqrt (100) / (98 x 99).
Here is what I can get:
double sum, x, y, z;
x = 3;
y = 1;
z = 2;
sum = 0;
while (x <= 100)
{
sum = sqrt (x) / (y * z);
x++;
y++;
z++;
}
cout << "The summation = " << sum << endl;
I am stumped on how to complete this loop to get the right answer. =(
Any help would be greatly appreciated!
C++ quick question
Started by Nightblaze1, Nov 21 2012 01:13 AM
5 replies to this topic
#1
Posted 21 November 2012 - 01:13 AM

#2
Posted 21 November 2012 - 01:21 AM
I don't see a problem on your algorithm, but... just this:
sum = sqrt (x) / (y * z);
shouldn't be this?:
sum += sqrt (x) / (y * z);
sum = sqrt (x) / (y * z);
shouldn't be this?:
sum += sqrt (x) / (y * z);
#5
Posted 21 November 2012 - 11:02 AM
You are welcome!
#6
Posted 21 November 2012 - 11:56 AM
Or as a Python list comprehension:
sum(math.sqrt(x) / ((x-2)*(x-1)) for x in range(3,101)) = 2.244
Sorry, couldn't resist
sum(math.sqrt(x) / ((x-2)*(x-1)) for x in range(3,101)) = 2.244
Sorry, couldn't resist
Edited by RavenNL, 21 November 2012 - 11:56 AM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











