Saturday, August 8, 2026
HomeC ProgrammingRamanujan Nested Radicals – Answer

Ramanujan Nested Radicals – Answer


The subject for this month’s Train is to code a recursive perform that solves the Ramanujan Nested Radical, illustrated in Determine 1. Do not forget that Ramanujan created and solved this puzzle in his head. #genius

Determine 1. One among Ramanujan’s extra well-known nested radicals additionally known as an infinite id.

In previous Workout routines on this weblog, I’ve checked out continued fractions and nested radicals resembling this one and recursion instantly involves thoughts as a solution to code it. After all, the problem with recursion is methods to unwind the factor.

For my answer, the root() perform contains an integer argument depend. This variable decrements every time the perform calls itself till the worth of depend is zero. At this level, the recursion unwinds.

2026_08-Train.c


#embrace <stdio.h>
#embrace <math.h>

float root(float a,int depend)
{
    whereas( count-- )
        return( sqrt(1.0+(a+1.0) * root(a+1.0,depend)) );
    return(a);
}

int primary()
{
    float a = 0.0;

    a = root(1.0,25);
    printf("%fn",a);

    return 0;
}

Within the primary() perform, the recursive root() perform is initially known as with values of 1 and 25. The one is written as 1.0, which the compiler identifies as an actual quantity; 25 is an integer. I discovered that 25 repetitions is satisfactory to achieve the results of 3.0 (no less than on my computer systems).

My C language model of the Ramanujan Nested Radical thingy seems within the return assertion within the root() perform:

sqrt(1.0+(a+1.0) * root(a+1.0,depend))

Variable a represents the incrementing worth within the nested radical, growing by one every time the root() perform known as. The result’s lastly returned as soon as the worth of depend is zero, which occurs within the whereas assertion: whereas( count-- )

Keep in mind so as to add the -lm swap when constructing this code in Linux on the terminal immediate. This swap brings within the math library, which is required for the sqrt() perform to behave.

Right here is output from a pattern run:

3.000000

I hope your answer met with success!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments