• Curse Sites
Help

# C++ quick question

5 replies to this topic

### #1 Nightblaze1

Nightblaze1

Sexton

• Members
• 269 posts
• Location:Australia

Posted 21 November 2012 - 01:13 AM

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!

### #2 Flashk

Flashk

Specter

• Members
• 0 posts

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);

### #3 Kryses

Kryses

Zealot

• Members
• 50 posts

Posted 21 November 2012 - 01:22 AM

double sum = 0;
int x;

for(x=3; x<=100; x++) {
sum+= sqrt(x)/((x-2)(x-1));
}

### #4 Nightblaze1

Nightblaze1

Sexton

• Members
• 269 posts
• Location:Australia

Posted 21 November 2012 - 02:37 AM

Flashk, on 21 November 2012 - 01:21 AM, said:

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);

Thank you so much!!!! Lol, I couldn't figure out what was going wrong haha! =)

### #5 Flashk

Flashk

Specter

• Members
• 0 posts

Posted 21 November 2012 - 11:02 AM

You are welcome!

### #6 RavenNL

RavenNL

• Members
• 49 posts

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

Edited by RavenNL, 21 November 2012 - 11:56 AM.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users