I typed this up quickly as an intuitive solution for determining if a given number is a perfect square.
How could I improve this? I already know that it doesn't handle the case where the number is 1. I'm wondering if I could generalize the calculation so it rounds up. What can I look into to develop a mindset for tackling these sorts of problems?
int number = 1000000;
int half = (number + 2 - 1)/2;
int square = 0;
boolean isSquare = false;
for( int i = half; i > 0 ; i-- ){
square = i*i;
if( square > number )
continue;
if( square == number ){
System.out.println( "This number is a perfect square" );
isSquare = true;
break;
}
}
if( !isSquare ){
System.out.println( "This number is not a perfect square" );
}