Who knows Java?
can you write a method that takes in an integer and checks to see if its a perfect square (ie returns a boolean value)?
ie 4 is a perfect square sqrt(4) = 2
2 is not a perfect square since sqrt(2) = not an int.
or is there a method that checks if something is an integer??
ie 4 is a perfect square sqrt(4) = 2
2 is not a perfect square since sqrt(2) = not an int.
or is there a method that checks if something is an integer??
Originally Posted by å
can you write a method that takes in an integer and checks to see if its a perfect square (ie returns a boolean value)?
ie 4 is a perfect square sqrt(4) = 2
2 is not a perfect square since sqrt(2) = not an int.
or is there a method that checks if something is an integer??
ie 4 is a perfect square sqrt(4) = 2
2 is not a perfect square since sqrt(2) = not an int.
or is there a method that checks if something is an integer??
but checking if something is an integer would only apply to an object that is not already an integer, like a string or a double (probably going to be a double). i don't think there's a built in method to check whether an object is an integer or not, so you might have to do it yourself. should be pretty easy, tho. look up the java API:
http://java.sun.com/j2se/1.4.2/docs/api/
something like:
boolean SquareCheck(int x){
double y = sqrt(x);
int q = sqrt(x);
y = y * 10000000; //use however many decimal places the double
//could hold
x = x * 10000000;
if( (y-x) != 0) return false;
return true;
}
This would rely on the sqrt function merely truncating the value returned from the sqrt function, which I can't remember if that's what happens or not. I'm a little rusty.
boolean SquareCheck(int x){
double y = sqrt(x);
int q = sqrt(x);
y = y * 10000000; //use however many decimal places the double
//could hold
x = x * 10000000;
if( (y-x) != 0) return false;
return true;
}
This would rely on the sqrt function merely truncating the value returned from the sqrt function, which I can't remember if that's what happens or not. I'm a little rusty.
public boolean perfectSquare(int anInteger)
{
boolean perfectSquare;
double theInteger = (double)anInteger;
double sqrt = sqrt(theInteger);
double perfectSquare = sqrt * sqrt;
if(theInteger == perfectSquare)
{
perfectSqaure = true;
}
else
{
perfectSquare = false;
}
return perfectSquare;
}
Something like that....include java.lang.Math
Check out http://java.sun.com/j2se/1.4.2/docs/api/index.html
{
boolean perfectSquare;
double theInteger = (double)anInteger;
double sqrt = sqrt(theInteger);
double perfectSquare = sqrt * sqrt;
if(theInteger == perfectSquare)
{
perfectSqaure = true;
}
else
{
perfectSquare = false;
}
return perfectSquare;
}
Something like that....include java.lang.Math

Check out http://java.sun.com/j2se/1.4.2/docs/api/index.html
um.. how about..
if the mantissa of sqrt(x) == 0, then x is a perfect square. but you'd have to figure out what function gets the mantissa, which puts you in a similar spot.
if the mantissa of sqrt(x) == 0, then x is a perfect square. but you'd have to figure out what function gets the mantissa, which puts you in a similar spot.
Last edited by reno96teg; Apr 7, 2004 at 07:08 AM.
i already got it last night
h: thanks for the help anyways 
h: thanks for the help anyways 
Code:
public static boolean isSquare(int num)
{
if(Math.sqrt(num)==(int)(Math.sqrt(num)))
return true;
else
return false;
}
are you sure that works? I thought that when you caste from a double to an int, it truncates, which would cause problems with the way you are doing it. Have you checked it on several different numbers?
it gave me the output i wanted..
i wanted to find 4 numbers where the sum of each pair is a perfect square .. it gave me some that werent .. but bleh it gave me lots that were.. i needed 1 set
im done
i wanted to find 4 numbers where the sum of each pair is a perfect square .. it gave me some that werent .. but bleh it gave me lots that were.. i needed 1 set
im done
Thread
Thread Starter
Forum
Replies
Last Post



