Printing solutions to a brute force search-- error in print conditions?

So I'm doing an assignment for my Intro to Programming class, where we have to write a program, using a brute force search algorithm with for-loops, that finds and prints the solutions to x^3 + y^3 = z, where the user is prompted to input integer n, and z runs from integers 0 to n.

Simple enough, but I've come into a formatting snag concerning z's that have two solutions (like 1729, for example).

When it prints out a second solution for a z, it always prints it out on the following line (instead of the same line), and I cannot figure out why. The only place I print a new line is when x == y (where there is no more need to check for more solutions) and a solution has been printed once, else-if x == y and printed two solutions. The conditions I set shouldn't be satisfied twice (thus a new line should only be printed once per z), unless I'm just misunderstanding what I've written.

Variable z_check is for the purpose of establishing whether or not a solution has been found already for the same z-value, while print_check is for determining if a solution has been printed.

I tried to debug my code for the new lines, printing "!" for the first new line condition set, and printing "@" for the second. Also printed the print_check to make sure it updates correctly.

It prints:
"new print_check: 0 1729: 1,12, print_check: 1!
new print_check: 0 9,10, print_check: 1!
new print_check: 0"

1729 is the z, while 1,12 and 9,10 are x,y values, respectively.

Which means the first if condition for a new line-- specifically the x == y part of it-- is being satisfied when from my interpretation, it shouldn't be.

So I guess my specific question is, what am I misinterpreting here or what errors have I made, and what changes should I make?

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


int main()
{
	int n;
	std::cin >> n;

	int z_check = 0;		
	int print_check = 0;

	for (int z = 0; z <= n; ++z)
	{
		int x, y;
		print_check = print_check * 0;
		for (x = 1; x <= z; ++x)
		{
			for (y = z; y >= x; --y)
			{
				if ((x * x * x) + (y * y * y) == z)
				{
					if (z != z_check)
					{
						std::cout << z << ": " << x << ',' << y << ", ";
						z_check = z;
						print_check = 1 + print_check;
						std::cout << "print_check: " << print_check;
					}
					else
					{
						std::cout << x << ',' << y << ", ";
						print_check = 1 + print_check;
						std::cout << "print_check: " << print_check;
					}
				}
				if (print_check == 1 && x == y)
				{
					std::cout << "!\n";
					print_check = print_check * 0;
					std::cout << "new print_check: " << print_check << ' ';
				}
				else if (print_check == 2 && x == y)
				{
					std::cout << "@\n";
					print_check = print_check * 0;
					std::cout << "new print_check: " << print_check << ' ';
				}
			}
		}
	}

	return 0;
}
Last edited on
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
#include <iostream>

int main()
{
    int n ;
    std::cout << "n? " ;
    std::cin >> n ;

    for( int z = 0 ; z <= n ; ++z ) // for each number from 0 to n
    {
        bool solution_found = false ; // no solutions found yet for this value of z

        // brute force with x <= y
        for( int x = 1 ; x < z ; ++x )
        {
            const int xcubed = x * x * x ;
            if( xcubed > z ) break ; // no point in looking for higher values of x

            for( int y = x ; y < z ; ++y )
            {
                const int ycubed = y * y * y ;
                const int sum = xcubed + ycubed ;
                if( sum > z ) break ; // no point in looking for higher values of y
                else if( sum == z ) // a solution is found
                {
                    if( !solution_found ) // if this is the first solution
                    {
                        std::cout << '\n' << z ; // start a new line
                        solution_found = true ;
                    }
                    else std::cout << "  and also " ; // otherwise print on the same line

                    std::cout << " == " << x << "^3 + " << y << "^3" ;
                }
            }
        }
    }
    std::cout << '\n' ; // finally put a new line (to end the last solution)
}
Topic archived. No new replies allowed.