Skip to content

luc040.c

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.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 12 Dec 2025
License MIT License (See the LICENSE file for details)
Difficulty Beginner (index: 1 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Pointers
  • Sorting (possible)
  • Iteration

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#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;
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc040.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;
    }