luc040.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 12 Dec 2025
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Ramanujan number (1729) is the smallest number that can be expressed as sum of cubes in two different ways - 1729 can be expressed as 1^3 + 12^3 and 9^3 + 10^3. Write a program to print all such numbers up to a reasonable limit.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#define limit 100000
#define max_base 47
int main()
{
long long sum1, sum2;
int count = 0;
printf("Ramanujan numbers : \n");
int found_match;
for (int a = 1; a <= max_base; a++)
{
for (int b = a + 1; b <= max_base; b++)
{
sum1 = (long long)a * a * a + (long long)b * b * b;
if (sum1 > limit)
{
break;
}
found_match = 0;
for (int c = a + 1; c <= max_base; c++)
{
if (found_match)
{
break;
}
for (int d = c + 1; d <= max_base; d++)
{
sum2 = (long long)c * c * c + (long long)d * d * d;
if (sum2 > sum1)
{
break;
}
if (sum1 == sum2)
{
count++;
printf("(%d.) %lld = %d^3 + %d^3 = %d^3 + %d^3\n", count, sum1, a, b, c, d);
found_match = 1;
break;
}
}
}
}
}
printf("-------------------------------\n");
printf("Search complete.");
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58