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