Difficulty: ★ ★ ★ ★
Srinivasa Ramanujan was a mathematician of the genius variety in that he did amazing things in his short lifetime. The man was insanely brilliant. In fact, he struggled to get other mathematicians to understand his work because it was so unfamiliar. Anyway, he concocted the thing shown in Figure 1, which is one of many Ramanujan Nested Radicals:

Figure 1. One of Ramanujan’s more famous nested radicals also called an infinite identity.
A nested radical is a radial expression that contains another radical expression. A simple and more comprehensible example is shown in Figure 2.

Figure 2. A nested radical.
The radical (square root) contains an expression with another radical (square root). But for Ramanujan, the insanity just kept going.
From Figure 1, you see that the pattern is to add to 1 the square root of one plus two times the square root of one plus three, and so on to infinity.
The delightful part is that the result of the nested radical operation is three. Yes, all that nonsense when calculated out to infinity results in the integer value 3.0.
I hope you can appreciate the brilliance of Srinivasa Ramanujan because he was able to discern this type of contraption in his head and know its result without the aid of a computer.
As a C programmer, you have a computer. And you have in your brain the ability to code the Ramanujan Nested Radical example shown in Figure 1 to prove that the result is 3.0. Yes, this calculation involves recursion. Well, I suppose you could probably muster the code to do calculate the value without recursion (which I’d love to see), but I used recursion for my solution. Here’s the output:
3.000000
Please try this exercise on your own before you peek at my solution, which I’ll publish in a week.
Is the lowest level square root on the right which is shown as √1+… just evaluated as √1?
I implemented this in a while loop (I avoid recursion like the plague!) and got what I believe mathematicians call “the wrong answer”. (Specifically 2).
I then found this video . . .
https://www.youtube.com/watch?v=ORK6apd0ZpE
. . . which shows that the left or outermost square root shouldn’t be there so re-wrote the code by changing the while condition from >=1 to >= 2.
#include<stdio.h>
#include<math.h>
int main()
{
double radicand = 1.0;
double multiplicand = 32;
double result = 0.0;
printf(“%f\n”, radicand);
while(multiplicand >= 2)
{
radicand = 1.0 + multiplicand * sqrt(radicand);
printf(“%f\n”, radicand);
multiplicand–;
}
result = sqrt(radicand);
printf(“%f\n”, result);
return 0;
}
This is the output:
1.000000
33.000000
179.081442
402.463943
582.783616
676.945527
703.490775
690.608413
657.985737
616.629584
572.136630
527.226309
483.189592
440.631478
399.833253
360.924956
323.966426
288.985078
255.993417
224.997120
195.998752
168.999465
143.999774
120.999906
99.999961
80.999985
63.999994
48.999998
35.999999
25.000000
16.000000
9.000000
3.000000
Neat.
I was able to code a solution starting with 1. Then again, I don’t know whether I did it correctly. My answer is 3.0. We’ll see in a week. I just think that this stuff is fun!
Wouldn’t it be funny if we both coded this wrong but both got the right answers by chance? I’m still not sure about the 1+… at the end. I started off with √1=1 but that might not be right.
A simpler way to find 3 with square roots 🙂
√1+√1+√1=3