Chapter - 4: The Loop Control Structure

Ramanujan number is the smallest number that can be expressed as a sum of two cubes in two different ways. Write a program to print all such numbers up to a reasonable limit.


E
Sections
15
Exercises

A

B

C

D

E

1

2

3

4

5

6

7

8

10

11

12

13

14

15

16

9

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
	int num, i, j, k, l;
	for (num = 1; num < 5000; num++)
	{
		for (i = 1; i < num; i++)
		{
			if (num < i*i*i)
				break;
			for (j = i + 1; j < num; j++)
			{
				if (num < j*j*j)
					break;
				for (k = i + 1; k < num; k++)
				{
					if (k*k*k > i*i*i + j*j*j)
						break;
					for (l = k + 1; l < num; l++)
					{
						if (num < k*k*k + l*l*l)
							break;
						if ((num == i*i*i + j*j*j) && (num == k*k*k + l*l*l))
						{
							printf("\n%d^3 + %d^3 = %d^3 + %d^3 = num : %d",i, j, k, l, num);
							break;
						}
					}
				}
			}
		}
	}
	_getch();
	return 0;
}

© 2020 Garbage Valuegarbage value logo