What I want to do is by using a for loop, calculate the sum of roots under roots (not a math guy, don't know how you call that).
The sequence should go like this:
Sqrt(2 + Sqrt(2 + ... + Sqrt(2))), where n > 0.
For example, if n = 3 on the for loop (while i = 1, i++), it should make this arithmetic sequence:
sum += Math.Sqrt(2 + Math.Sqrt(2 + Math.Sqrt(2)));
and if n = 4:
sum += Math.Sqrt(2 + Math.Sqrt(2 + Math.Sqrt(2 + Math.Sqrt(2))));
So the problem is I don't know how to loop trough n by adding roots under roots until the loop ends.
My code template
public static double GetSumOfRoots(int n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
{
sum += ...;
}
return sum;
}
Please tell me if I am not clear on my description, I will do my best, thank you.
Unwrap the loop, and have a look on what's going on:
Having math done, you can write corresponding code:
You can simplify the routine with a help of Linq:
Demo:
Outcome: