Member Avatar for joankim

Hi :) I am trying to make a program that will find the first x perfect squares. I have done and redone this many times, but it never works. So basically, it needs to do two things: check if y is a perfect square. If it is, it needs to check if this number is a magic square.
A magic square is a perfect square where you can do 1+2+3+4 and so on, and end up with a perfect square. This for my AP comp sci class, and the full assignment is here:
http://www.howard.k12.md.us/rhhs/helloworld/LessonA12/Lab-A12-1.html

import java.util.Scanner;

public class FunLoops {import java.util.Scanner;

public class FunLoops {
	int x;
	int a = 0;
	double b = 1;
	int incrementer = 0;
	int total = 0;
	Scanner in;

	public FunLoops(){
		in = new Scanner(System.in);
		loops();
	}

public void loops(){
System.out.println("How many magic squares do you want?");
int input = in.nextInt();
for(double x = Math.pow(b, 2); a < input; b++){ //x will only be a perfect square

while(true){

if (total == x){	// total is the result of 1+2+3.... If total = x, the perfect square is a magic square.
	System.out.print(x + ", ");	// prints all magic squares
	a++;	// stops "for" loop when it has reached input.
	break;	//breaks "while" loop to start over with next perfect square, after printing magic square.
		}

else{
	incrementer += 1; // 1+2+3....
	total += incrementer; // .... = ?

	if (total / 2.0 > x){ // if the perfect square isn't a magic square, after testing, total/2 > x (x is the perfect square)
		break;	// ....and it will stop the "while" loop, in order to try next perfect square
			}
		}

	}


	}
}

}

Main:

public class FunLoopsTester {
	public static void main(String[] args) {

	FunLoops app = new FunLoops();
	}
}

Right now, the code prints "1" (which is in fact a magic square) "input" times. (if the input is 4, it will print "1" four times.)

I really appreciate all help I can get. I added a lot of comments to make it easier for you to follow.

Recommended Answers

All 16 Replies

Member Avatar for joankim

After looking through it numerous times, and doing some system.out.prints, it seems like the error here is the for loop. It fails to change the value of x, and x is always 1. Any suggestion to how I can fix this?

EDIT: Seems like there is two problems with the code. I changed around a little bit on stuff, and this:

for(int useless = 5; a < input; b++){ //x will only be a perfect square
x = Math.pow(b, 2);
if(x > 1000){
	break;
}
System.out.print(x + ", ");

Will print 1^2, 2^2, 3^2, 4^2 and so on, until x is bigger than 1000. Before i added if(x > 100){ break; }, it created an infinite loop, even though the rest of the code (see original post) should prevent this.

for(double x = Math.pow(b, 2)..

that's because as it loops it keeps replacing the value of x from
double x = Math.pow(b, 2)
maybe remove it from the loop ;)

you should have posted this in the java forum cause your problem is using a specific language(java) directly...maybe a mod will move this anytime

for(int useless = 5; a < input; b++)

if the value of a remains the same then it will loop forever as the condition a can never be greater than or equal to input

Member Avatar for joankim

but a should be incremented by one for every x (magic square) that is printed, right?

if (total == x){	// total is the result of 1+2+3.... If total = x, the perfect square is a magic square.
			System.out.print(x + ", ");	// prints all magic squares
			a++;	// stops "for" loop when it has reached input.

By the way, I'm changing the code around to figure out what's wrong. I didn't need the while loop. This is hwo it looks right now:

import java.util.Scanner;

public class FunLoops {
	double x;
	int a = 0;
	double b = 1;
	int incrementer = 1;
	int total = 1;
	Scanner in;

	public FunLoops(){
		in = new Scanner(System.in);
		loops();
	}

public void loops(){
System.out.println("How many magic squares do you want?");
int input = in.nextInt();
	for(int useless = 5; a < input; b++){ //x will only be a perfect square
x = Math.pow(b, 2);
if(x > 1500){
	break;
}
System.out.print(" x: " + x + ", ");
		if (total == x){	// total is the result of 1+2+3.... If total = x, the perfect square is a magic square.
			System.out.print(x + ", ");	// prints all magic squares
			a++;	// stops "for" loop when it has reached input.
			//break;	//breaks "while" loop to start over with next perfect square, after printing magic square.
			}

		else{
			incrementer++;	//1, 2, 3....
			total += incrementer; // 1+2+3... = ?

		/*	if (total / 2.0 > x){ // if the perfect square isn't a magic square, after testing, total/2 > x (x is the perfect square)
				break;	// ....and it will stop the "while" loop, in order to try next perfect square
			}*/
		}

		}


	}
}

Start of the output for this code: x: 1.0, 1.0, x: 4.0, x: 9.0, x: 16.0, x: 25.0, x: 36.0, x: 49.0

Try to print out the value of a,x,b and total every time their value changes to see what happens to them in your loops

It will be easier to pinpoint the problem

Member Avatar for joankim

for me, it looks like total never matches x.

EDIT: I'm getting closer :P Look at the start of the last output. it prints x where it is supposed to for x = 1, but not for x = 1225.

Which means that something is probably wrong with:

else{
			incrementer++;	//1, 2, 3....
			total += incrementer; // 1+2+3... = ?
		}

Any ideas? It is supposed to do: 1, then check if statement again, then 1+2=3, then check if statement again, then 1+2+3=6 and check if statement, and so on.

for me, it looks like total never matches x.

and maybe that's why 'a' never iterates which makes it loops forever

Member Avatar for joankim

exactly. And now I think I remember why I had the while loop there. I need it to go back and check the if statement every time it does the else statement. I will put the while back in there, and then try.

Member Avatar for joankim

alright... with the while loop back, i tried to run it, and it was an infinite loop. it kept printing 1.0, 1.0, 1.0 and so on. Which means that the "if" statement was initialized since that's the only place with a system.out.print. and that means a should be incremented too.

There is only two things I can think of that will make this happen:
1. the variable a is only changed within the while loop or if statement, and never inside the for loop.
2. the break statement does not stop the while loop, and it keeps initializing the if statement over and over again without breaking the loop.

Member Avatar for joankim

For every thing I try, I get a little bit more confused. Here is the current code:

import java.util.Scanner;

public class FunLoops {
	double x;
	int a = 0;
	double b = 1;
	int incrementer = 0;
	int total = 0;
	Scanner in;

	public FunLoops(){
		in = new Scanner(System.in);
		loops();
	}

public void loops(){
System.out.println("How many magic squares do you want?");
int input = in.nextInt();
	for(int useless = 5; a < input; b++){ //x will only be a perfect square
x = Math.pow(b, 2);
if(x > 1500){
	break;
}
System.out.print(" x: " + x + ", ");

	while(true){

		if (total == x){	// total is the result of 1+2+3.... If total = x, the perfect square is a magic square.
			System.out.print(x + ", ");	// prints all magic squares
			a++;	// stops "for" loop when it has reached input.
			break;
			}

		else{
			incrementer++;	//1, 2, 3....
			total += incrementer; // 1+2+3... = ?

			if(total / 2 > x){
				break;
			}
		}
		break;
		}
}

	}
}

Start small like trying to get if the input is a perfect square before the others

Member Avatar for joankim

OH MY GOD YES! I finally figured this shit out. It was a simple logic mistake. total and incrementer needs to be reset to 0 before every single while loop! DUH....

Thank you again for your help :)

Here is the complete code, as nice as I can make it.

import java.util.Scanner;

public class FunLoops {
	double x;
	int a = 0;

	double b = 1;
	double incrementer = 0;
	double total = 0;
	Scanner in;

	public FunLoops(){
		in = new Scanner(System.in);
		loops();
	}

public void loops(){
System.out.println("How many magic squares do you want?");
int input = in.nextInt();
System.out.println();
int num = 0;
	for(int useless = 5; a < input; b++){ //x will only be a perfect square
x = Math.pow(b, 2);
total = 0;
incrementer = 0;

	while(a < input){

		if (total == x){	// total is the result of 1+2+3.... If total = x, the perfect square is a magic square.
			num = num + 1;
			System.out.print(" Magic square nr " + num + ": " + (int)x);	// prints all magic squares
			System.out.println();
			a++;	// stops "for" loop when it has reached input.
			break;
			}

		else{
			incrementer++;	//1, 2, 3....
			total += incrementer; // 1+2+3... = ?

			if(total / 2 > x){
				break;
			}
		}
		}
}

	}
}

Ive made my own version of your assign I'll post what it should do to solve if the number is a magic square
note: I won't post my whole code and the one I'm posting should only serve as a guide to simplify your work

boolean one = false;
boolean two = false;
while(a!=input){
    a++;
    x = (int) Math.pow(a, 2);
    if(x==input) one = true;	
    else if(x>input){ 
        break;
    } 
}
x=0;a=0;
while(a!=input){
    a++;
    x+=a;
    if(x==input) two = true;	
    else if(x>input){
        break;
    }
if(two == true && one == true) System.out.println(input + " is a magic number");
}
Member Avatar for joankim

hehe, just have to laugh seeing how fkn easy you made it :P At least nobody can say I didn't do this by myself, as I was a minute faster than you :D

Anyways, make it try to find the 8th square. Took my powerplug of a computer with i7 and around 3 Ghz 5 minutes to find. haha

Again, I really appreciate your help mate :)

In my code I break the loop if the operations get bigger than the input so as not to put stress on the cp and since its not anymore necessary to continue the operation since its already not perfect square

else if(x>input){
        break;
    }

PS. I made a program that checks if the input is a magic number not create one ;)

Member Avatar for joankim

yea, I guess there are ways to optimize my program, but to be honest, I couldn't care less if the program is slow. Maybe I'll take a look at it in a year so that I can laugh over my shitty programming skillz ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.