need a little help

Heyy! :) I just started learning programing and there is a little assigment that i need to do, but i cant figure out why my program is not doing what it should do. If i look at it, everything seems correct, but when i try to run it, it never stops.
I have to find minimal number m, where m=a^3+b^3=c^3+d^3. It should print out 1729, but it doesnt.
No idea how to fix this. :(

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
#include <iostream>
#include <cmath>

using namespace std;

int maks(int num1, int num2);
int main()
{
    int atgr,m,n,sk=100;

    for (int k=1; k<=sk; k++)

        for (int  l=1; l<=sk; k++){

            for (int i=1; i<=sk; i++){

                for (int  j=1; j<=sk; j++){

                    m= k*k*k + l*l*l;
                    n= j*j*j + i*i*i;

                    if(k!=l && k!=i && k!=j && l!=i && l!=j && i!=j){
                    cout<<atgr;}


            }
            }
            };
    atgr=maks(m,n);

}

    int maks(int num1, int num2){
    int m;
    if (num1==num2) m=num1;
    return m;
    }
You are missing a set of curly brackets. There should be one after the first for statement. If not, k will repeatedly initialize back to one, even though it is repeatedly incremented by one as well. It would just flop between one and two for infinity. At least that's what I think it would do. I could be wrong. I'm new to programming myself.
Line 13
 
  for (int  l=1; l<=sk; k++){


You probably meant l++;
oh, yeah. but it is still doing the same thing.

#include <iostream>

using namespace std;

int maks(int num1, int num2);
int main()
{
    int atgr,m,n,sk=100;

    for (int k=1; k<=sk; k++){

        for (int  l=1; l<=sk; l++){

            for (int i=1; i<=sk; i++){

                for (int  j=1; j<=sk; j++){

                    m= k*k*k + l*l*l;
                    n= j*j*j + i*i*i;

                    if(k!=l && k!=i && k!=j && l!=i && l!=j && i!=j){
                    cout<<atgr;}

                }
            }
            }
            };
    atgr=maks(m,n);

}

    int maks(int num1, int num2){
    int m;
    if (num1==num2) m=num1;
    return m;
    }
Last edited on
lol Your program keeps running because the inner loop is forced to execute 100 million times.
Topic archived. No new replies allowed.