luc040-logic.c (5617B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 12 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Ramanujan number (1729) is the smallest number that can be 9 expressed as sum of cubes in two different ways - 1729 can be 10 expressed as 1^3 + 12^3 and 9^3 + 10^3. Write a program to print all such 11 numbers up to a reasonable limit. */ 12 /* Let Us C, Chap- 6, Page - 106, Qn No.: B(g) */ 13 14 // 1. INCLUDE SECTION 15 #include <stdio.h> // Includes the standard input/output library, necessary for printf() 16 17 // 2. CONSTANT DEFINITIONS (MACROS) 18 // These define fixed values we can easily use and change later. 19 #define LIMIT 100000 // The maximum number we want to search up to. 20 // The maximum base value (a, b, c, or d) we need to check. 21 // Since 47^3 is greater than 100,000, checking bases up to 47 covers the LIMIT. 22 #define MAX_BASE_VAL 47 23 24 // 3. MAIN FUNCTION 25 int main() 26 { 27 // 4. VARIABLE DECLARATION 28 29 // sum1 and sum2 store the results of a^3 + b^3 and c^3 + d^3. 30 // We use 'long long' because cubes (like 47^3) are large and can exceed 31 // the capacity of a standard 'int', preventing errors. 32 long long sum1, sum2; 33 34 int count = 0; // Counter to keep track of how many Ramanujan numbers we find. 35 36 // This flag is used to optimize the search. If we find the second way (c^3 + d^3) 37 // to make sum1, we set this to 1 and immediately stop the inner loops. 38 int found_match; 39 40 // Print introductory message to the user. 41 printf("Ramanujan Number Finder (a^3 + b^3 = c^3 + d^3)\n"); 42 printf("Searching up to %d (Max base value is %d)\n", LIMIT, MAX_BASE_VAL); 43 printf("---------------------------------------------------\n"); 44 45 // 5. OUTER LOOPS: Establishing the FIRST SUM (a^3 + b^3 = sum1) 46 47 // Loop for 'a' (the smaller base of the first pair) 48 for (int a = 1; a <= MAX_BASE_VAL; a++) 49 { 50 51 // Loop for 'b' (the larger base of the first pair) 52 // We start 'b' at 'a + 1' to enforce the rule a < b. 53 // This prevents us from checking redundant pairs like (1, 12) and (12, 1). 54 for (int b = a + 1; b <= MAX_BASE_VAL; b++) 55 { 56 57 // Calculate the first sum: sum1 = a^3 + b^3 58 // (long long) is a cast to ensure the math is done using the 'long long' type. 59 sum1 = (long long)a * a * a + (long long)b * b * b; 60 61 // Optimization 1: Check if the sum exceeds the global limit. 62 if (sum1 > LIMIT) 63 { 64 // Since 'b' is increasing, any further increase will also be over the limit. 65 // We stop the 'b' loop and move to the next 'a'. 66 break; 67 } 68 69 // Reset the flag for every new sum1. 70 // We start searching for a second way for this new 'sum1', so we reset the flag to 0. 71 found_match = 0; 72 73 // 6. INNER LOOPS: Searching for the SECOND SUM (c^3 + d^3 = sum2) 74 75 // Loop for 'c' (the smaller base of the second pair) 76 // We start 'c' at 'a + 1' to enforce the rule a < c. 77 // This ensures the two pairs {(a, b) and (c, d)} are truly distinct (e.g., 1729 = 1^3 + 12^3 and 9^3 + 10^3). 78 for (int c = a + 1; c <= MAX_BASE_VAL; c++) 79 { 80 81 // Optimization 2: Check the flag to exit the 'c' loop early. 82 if (found_match) 83 { 84 // If we already found the second way (c, d) in a previous iteration of 'c', stop searching and move to the next (a, b) pair. 85 break; 86 } 87 88 // Loop for 'd' (the larger base of the second pair) 89 // We start 'd' at 'c + 1' to enforce c < d. 90 for (int d = c + 1; d <= MAX_BASE_VAL; d++) 91 { 92 93 // Calculate the second sum: sum2 = c^3 + d^3 94 sum2 = (long long)c * c * c + (long long)d * d * d; 95 96 // Optimization 3: Check if the second sum has passed the first sum. 97 if (sum2 > sum1) 98 { 99 // Since 'd' is increasing, any further increase will also be greater than sum1. 100 // We stop the 'd' loop and move to the next 'c'. 101 break; 102 } 103 104 // 7. CONDITION CHECK: Have we found a Ramanujan number? 105 // Check if the two sums are equal. 106 if (sum1 == sum2) 107 { 108 count++; // Increment the counter 109 110 // Print the result in the required format. 111 printf("[%d] %lld = %d^3 + %d^3 = %d^3 + %d^3\n", 112 count, sum1, a, b, c, d); 113 114 // Set the flag to 1, confirming we found the second way. 115 found_match = 1; 116 117 // Stop the 'd' loop immediately, as we found the required second pair. 118 break; 119 } 120 } 121 // If found_match was set to 1, the 'break' in the d loop will execute, 122 // then the 'if (found_match) break;' in the c loop will execute, 123 // and the program will move to the next (a, b) pair. 124 } 125 } 126 } 127 128 // 8. CONCLUSION 129 printf("---------------------------------------------------\n"); 130 printf("Search complete. Found %d Ramanujan-type numbers.\n", count); 131 132 return 0; // Standard way to indicate successful program execution. 133 }