Ramanujan First Problem

Srinivasa Ramanujan is one of my heroes. A brilliant mind from Southern India (Chennai) who had an incredible talent for numbers and unfortunately a very tragic and brief life. If you want to learn more about him I recommend reading The Man Who Knew Infinity instead of watching the movie. In the movie there is no, zero, mathematics. And if you learn about Ramanujan without appreciating his work in math it is as if you were learning about Einstein without appreciating his work in Physics.

In the book you'll find one of the very first problems proposed by Ramanujan when he was still a kid. It was an infinite series (95% of his work revolved around infinity) where he asked all the top mathematicians in the world if they could solve it. Simply stated, here it is:


What's the value of X? Top mathematicians all over the world jumped into this problem but with no success. They worked on it for months and months until they all, one by one, gave up. Finally Ramanujan gave away the solution. X = 3.

The mathematics behind the proof are pretty advanced. Only if the mathematicians 100+ years ago had access to a Surface Laptop with Visual Studio installed....

Writing a simple code to converge the series above is relatively easy. The equation is very suitable for a recursive solution, and hopefully the convergence will happen quick enough that the stack won't blow up. In my 2-lines recursive function I use a max seed to stop the recursion (remember, this is an infinite sum). Running the code for, say, 70 iterations quickly shows where it is converging towards. Code is below, output down below, cheers, Marcelo.

class Program
{
static void Main(string[] args)
{
int max = 70;
for (int i = 0; i <= max; i++)
{
Console.WriteLine("For terms = {0}, RFP = {1}", i, RamanujanFirstProblem(2, i));
}
}
static double RamanujanFirstProblem(int currentSeed, int maxSeed)
{
if (currentSeed >= maxSeed) return 0.0;
return Math.Sqrt(1 + currentSeed * RamanujanFirstProblem(currentSeed + 1, maxSeed));
}
}

Output:

For terms = 0, RFP = 0
For terms = 1, RFP = 0
For terms = 2, RFP = 0
For terms = 3, RFP = 1
For terms = 4, RFP = 1.73205080756888
For terms = 5, RFP = 2.23606797749979
For terms = 6, RFP = 2.55983016530012
For terms = 7, RFP = 2.7550532613299
For terms = 8, RFP = 2.86710292823774
For terms = 9, RFP = 2.92917335973214
For terms = 10, RFP = 2.96272300427958
For terms = 11, RFP = 2.98055375021182
For terms = 12, RFP = 2.98992036063105
For terms = 13, RFP = 2.99480026926621
For terms = 14, RFP = 2.9973274414787
For terms = 15, RFP = 2.99863031788908
For terms = 16, RFP = 2.99929967806766
For terms = 17, RFP = 2.99964261492875
For terms = 18, RFP = 2.99981791758458
For terms = 19, RFP = 2.99990736080556
For terms = 20, RFP = 2.99995292420987
For terms = 21, RFP = 2.99997610308338
For terms = 22, RFP = 2.99998788059949
For terms = 23, RFP = 2.99999385870344
For terms = 24, RFP = 2.9999968903158
For terms = 25, RFP = 2.99999842644538
For terms = 26, RFP = 2.99999920423618
For terms = 27, RFP = 2.99999959779522
For terms = 28, RFP = 2.99999979681498
For terms = 29, RFP = 2.99999989740273
For terms = 30, RFP = 2.999999948216
For terms = 31, RFP = 2.99999997387325
For terms = 32, RFP = 2.99999998682296
For terms = 33, RFP = 2.99999999335639
For terms = 34, RFP = 2.99999999665146
For terms = 35, RFP = 2.99999999831275
For terms = 36, RFP = 2.99999999915007
For terms = 37, RFP = 2.99999999957197
For terms = 38, RFP = 2.99999999978449
For terms = 39, RFP = 2.99999999989152
For terms = 40, RFP = 2.9999999999454
For terms = 41, RFP = 2.99999999997253
For terms = 42, RFP = 2.99999999998618
For terms = 43, RFP = 2.99999999999305
For terms = 44, RFP = 2.9999999999965
For terms = 45, RFP = 2.99999999999824
For terms = 46, RFP = 2.99999999999912
For terms = 47, RFP = 2.99999999999956
For terms = 48, RFP = 2.99999999999978
For terms = 49, RFP = 2.99999999999989
For terms = 50, RFP = 2.99999999999994
For terms = 51, RFP = 2.99999999999997
For terms = 52, RFP = 2.99999999999999
For terms = 53, RFP = 2.99999999999999
For terms = 54, RFP = 3
For terms = 55, RFP = 3
For terms = 56, RFP = 3
For terms = 57, RFP = 3
For terms = 58, RFP = 3
For terms = 59, RFP = 3
For terms = 60, RFP = 3
For terms = 61, RFP = 3
For terms = 62, RFP = 3
For terms = 63, RFP = 3
For terms = 64, RFP = 3
For terms = 65, RFP = 3
For terms = 66, RFP = 3
For terms = 67, RFP = 3
For terms = 68, RFP = 3
For terms = 69, RFP = 3
For terms = 70, RFP = 3

Comments

Post a Comment

[フレーム]

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Not really an FSM problem since the state isn't changing, it is just defined by the current input. Simply following the instructions should do it. Using VSCode IDE you can also engage the help of Cline or Copilot for a combo of coding and vibe coding, see below screenshot. Cheers, ACC. Process String with Special Operations I - LeetCode You are given a string  s  consisting of lowercase English letters and the special characters:  * ,  # , and  % . Build a new string  result  by processing  s  according to the following rules from left to right: If the letter is a  lowercase  English letter append it to  result . A  '*'   removes  the last character from  result , if it exists. A  '#'   duplicates  the current  result  and  appends  it to itself. A  '%'   reverses  the current  result . Return the final string  result  after processing all char...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. πŸ” Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...

Classic Dynamic Programming IX

A bit of vibe code together with OpenAI O3. I asked O3 to just generate the sieve due to laziness. Sieve is used to calculate the first M primes (when I was using Miller-Rabin, was giving me TLE). The DP follows from that in a straightforward way: calculate the numbers from i..n-1, then n follows by calculating the min over all M primes. Notice that I made use of Goldbach's Conjecture as a way to optimize the code too. Goldbach's Conjecture estates that any even number greater than 2 is the sum of 2 primes. The conjecture is applied in the highlighted line. Cheers, ACC. PS: the prompt for the sieve was the following, again using Open AI O3 Advanced Reasoning: " give me a sieve to find the first M prime numbers in C#. The code should produce a List<int> with the first M primes " Minimum Number of Primes to Sum to Target - LeetCode You are given two integers  n  and  m . You have to select a multiset of  prime numbers  from the  first   m  pri...