Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ramanujan number:a number expressed as sum of cubes of two or more different numbers.
C++
#include<stdio.h>
int main()
{
    int a,b,i,count=1;
    for(i=1; i<=10000; i++)
    {
        for(a=1; a<=10000; a++)
        {
            for(b=1; b<=10000; b++)
            {
                if(i==a*a*a+b*b*b)
                {
                    count++;
                }
                if(count>=2)
                {
                    printf("%d",i);
                }
                break;
            }
            break;
        }
        break;
    }
    return 0;
}


enhzflep: code formatted
Posted
Updated 14-Jan-15 2:22am
v2
Comments
Member 10641779 22-Jan-15 2:44am    
Dont use `break;` outside otherwise it will work only once and break the loop. keep inner break inside if(count>=2).
Member 10641779 22-Jan-15 2:56am    
Start count from zero .After getting a number make `count=0;`
Member 10641779 22-Jan-15 2:57am    
Number can be -ve also for example `91 = 6(3) + (−5)(3) = 4(3) + 3(3)`

You did not ask a specific question. But your code will not work as expected.

You are using integer calculations where mathematical operations will fail when the result is larger than the largest number that can be represented. So you must limit your algorithm accordingly.

With int, the largest number is defined as INT_MAX in the limits.h include file. This value is usually 2147483647. So your algorithm must be limited to loop variables that are below the cube root which is 1290.16.

If you need larger loop variables, you must use a larger data type like long long. That are usually 64-bit integers (see LLONG_MAX) where the cube root is 2097152.
 
Share this answer
 
Comments
Member 11324568 14-Jan-15 8:29am    
thanks...but why are you considering the cubeth root here..."??
Jochen Arndt 14-Jan-15 8:34am    
You are calculating the cubes inside the loops (a*a*a and b*b*b). To avoid overflows, the variables a and b must be less than the cube root of MAX_INT.
Member 11324568 14-Jan-15 8:35am    
thanks
Jochen Arndt 14-Jan-15 8:41am    
You are welcome.

And follow also the advise from OriginalGriff.

I recognized that execution of the code will take a lot of time but hoped that you realise yourself that your algorithm is not good.

But I did not recognized the break statements until the code was reformatted.
As written, than won't work at all, becuas eyou are "break"ing out of the loops after the first iteration. Removes the breaks, and consider putting it into a function so you can return when you match your values.

But...when you fix it, that may take some time to execute...quite a lot of time.
You are executing the inner "if" conditions 1,000,000,000,000 times - which is unlikely to complete on my computer before I go off for my supper.

I think you need to reconsider your algorithm - the "brute force and ignorance" approach is not a good strategy here.
 
Share this answer
 
Comments
Member 11324568 14-Jan-15 8:28am    
thanks...if i put the condition as <=30 then is it okay.?
OriginalGriff 14-Jan-15 8:44am    
Well, without teh breaks and with a different termination it'll work. Sort of. But...it's still very, very poor, and there are much, much better ways to do this.

All they would take is a bit of thinking on your part.
For example: What is the cube root of 10000? Why would that be relevant?
Member 11324568 14-Jan-15 8:47am    
thanks sir :) ill try doing that..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900