Find all Ramanujan Numbers that can be formed by numbers upto L

Given a positive integer L, the task is to find all the Ramanujan Numbers that can be generated by any set of quadruples (a, b, c, d), where 0 < a, b, c, d ≤ L. Ramanujan Numbers are the numbers that can be expressed as sum of two cubes in two different ways.Therefore, Ramanujan Number (N) = a3 + b3 = c3 + d3. Examples: Input: L = 20Output: 1729, 4104Explanation:The number 1729 can be expressed as 123 + 13 and 103 + 93.The number 4104 can be expressed as 163 + 23 and 153 + 93. Input: L = 30Output: 1729, 4104, 13832, 20683 Naive Approach: The simplest approach is to check for all combination of quadruples (a, b, c, d) from the range [1, L] consisting of distinct elements that satisfies the equation a3 + b3 = c3 + d3. For elements found to be satisfying the conditions, store the Ramanujan Numbers as 3 + b3. Finally, after checking for all possible combinations, print all the stored numbers. Below is the implementation of the above approach: Python3 import time    def ramanujan_On4(limit):     dictionary = dict()                  for a in range(0, limit):         for b in range(0, limit):             for c in range(0, limit):                 for d in range(0, limit):                                                                  if ((a != b) and (a != c) and (a != d)                         and (b != c) and (b != d)                             and (c != d)):                            x = a ** 3 + b ** 3                         y = c ** 3 + d ** 3                         if (x) == (y):                             number = a ** 3 + b ** 3                             dictionary[number] = a, b, c, d             return dictionary          L = 30 ra1_dict = ramanujan_On4(L)    for i in sorted(ra1_dict):     print(f'{i}: {ra1_dict[i]}', end ='n') Output: 1729: (9, 10, 1, 12) 4104: (9, 15, 2, 16) 13832: (18, 20, 2, 24) 20683: (19, 24, 10, 27) Time Complexity: O(L4)Auxiliary Space: O(1) Efficient Approach: The above approach can also be optimized by using Hashing. Follow the steps below to solve the problem: Initialize an array, say ans[], to stores all the possible Ramanujan Numbers that satisfy the given conditions. Precompute and store the cubes of all numbers from the range [1, L] in an auxiliary array arr[]. Initialize a HashMap, say M, that stores the sum of all possible combinations of a pair of cubes generated from the array arr[]. Now, generate all possible pairs(i, j) of the array arr[] and if the sum of pairs doesn’t exist in the array, then mark the occurrence of the current sum of pairs in the Map. Otherwise, add the current sum to the array ans[] as it is one of the Ramanujan Numbers. After completing the above steps, print the numbers stored in the array ans[]. Below is the implementation of the above approach: Python3 from array import * import time    def ramanujan_On2(limit):     cubes = array('i', [])             dict_sum_pairs = dict()             dict_ramnujan_nums = dict()     sum_pairs = 0             for i in range(0, limit):         cubes.append(i ** 3)                       for a in range(0, limit):         for b in range(a + 1, limit):             a3, b3 = cubes[a], cubes[b]                             sum_pairs = a3 + b3                             if sum_pairs in dict_sum_pairs:                                                                       c, d = dict_sum_pairs.get(sum_pairs)                 dict_ramnujan_nums[sum_pairs] = a, b, c, d                                                       else:                 dict_sum_pairs[sum_pairs] = a, b                      return dict_ramnujan_nums          L = 30 r_dict = ramanujan_On2(L)    for d in sorted(r_dict):     print(f'{d}: {r_dict[d]}', end ='n') Output: 1729: (9, 10, 1, 12) 4104: (9, 15, 2, 16) 13832: (18, 20, 2, 24) 20683: (19, 24, 10, 27) Time Complexity: O(L2)Auxiliary Space: O(L2) Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.