#### Archived

This topic is now archived and is closed to further replies.

# Formulating a perfect square algorithm

This topic is 5452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

public class FunLoops {
//blah, these wont be used

private long myint;
private double myintsquared;
private long mysum;

//...

FunLoops(){
myint = 1;
myintsquared = 1;
mysum = 0;
}

public void magicSquare(int x) {
int increment = 0;
int sum = 0;
for ( int i = 1; i <= x; i++ ){
if ( i * i == sum ){
System.out.println(i);
continue;
}else if(sum > 1){
i++;
}else{
increment++;
sum = sum + increment;
}
}
}
}

The compiler bitches and complains if i put a return statement out of the if ( i * i == sum ) block. Changing this function to void doesnt help to much ... If anyone could give me a hand that would be great, thanks.

##### Share on other sites
you dont need to return anything , you could just pass x by reference, void(int &x), and max x = to what ever you want and it will alter the variable instead of making a copy of the variable.

##### Share on other sites
quote:
Original post by Anonymous Poster
you dont need to return anything , you could just pass x by reference, void(int &x), and max x = to what ever you want and it will alter the variable instead of making a copy of the variable.

Thanks, but i was looking for something which had to deal more with my algorithm itself

##### Share on other sites
quote:
Original post by Anonymous Poster
you dont need to return anything , you could just pass x by reference, void(int &x), and max x = to what ever you want and it will alter the variable instead of making a copy of the variable.

The poster''s language is Java. Java only has call-by-value.

##### Share on other sites
quote:
Original post by MrPoopypants
public class FunLoops {	//blah, these wont be used        private long myint;	private double myintsquared;	private long mysum;        //...	FunLoops(){		myint = 1;		myintsquared = 1;		mysum = 0;	}	public void magicSquare(int x) {		int increment = 0;		int sum = 0;		for ( int i = 1; i <= x; i++ ){			if ( i * i == sum ){				System.out.println(i);				continue;			}else if(sum > 1){				i++;			}else{				increment++;				sum = sum + increment;			}		}	}}

The compiler bitches and complains if i put a return statement out of the if ( i * i == sum ) block. Changing this function to void doesnt help to much ...

If anyone could give me a hand that would be great, thanks.

The compiler probably bitches because not all execution paths terminate with a return statement in the case you mentioned. You can either throw a dummy return statement at the end of the function, or have a value that holds the return value and mutate it''s value where you would have placed a return in the original version. Then, after that assignment, place a break statement. The added return statement will return the assigned value.

Why does the compiler do this? Consider the case when x is 0. The comparison i <= x will always be false and the for-loop will never be executed. What is the return in this case? Your code doesn''t provide any options.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007692
• ### Who's Online (See full list)

There are no registered users currently online

×