luc040.c (1605B)
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 #include <stdio.h> 8 9 #define limit 100000 10 #define max_base 47 11 12 int main() 13 { 14 15 long long sum1, sum2; 16 int count = 0; 17 18 printf("Ramanujan numbers : \n"); 19 20 int found_match; 21 22 for (int a = 1; a <= max_base; a++) 23 { 24 for (int b = a + 1; b <= max_base; b++) 25 { 26 sum1 = (long long)a * a * a + (long long)b * b * b; 27 if (sum1 > limit) 28 { 29 break; 30 } 31 32 found_match = 0; 33 34 for (int c = a + 1; c <= max_base; c++) 35 { 36 if (found_match) 37 { 38 break; 39 } 40 for (int d = c + 1; d <= max_base; d++) 41 { 42 sum2 = (long long)c * c * c + (long long)d * d * d; 43 if (sum2 > sum1) 44 { 45 break; 46 } 47 if (sum1 == sum2) 48 { 49 count++; 50 printf("(%d.) %lld = %d^3 + %d^3 = %d^3 + %d^3\n", count, sum1, a, b, c, d); 51 52 found_match = 1; 53 break; 54 } 55 } 56 } 57 } 58 } 59 60 printf("-------------------------------\n"); 61 printf("Search complete."); 62 63 return 0; 64 }