What is a non-zero square?

Hi all,

In my program i have to test for the following condition:

If (Q2-4PR) is not a non-zero square (i.e. √(Q2-4PR) hasn’t got a root

and if its true then i have to print out a certain message.

Does this basically mean if the answer isnt a square number? ie. 1, 2, 4, 9, 16, 25, 36,49 etc..

also how would you go about testing this.

All i can think of is say if x represents (Q2-4PR)... :

if(x != 1 || x != 2 || x != 4 || x != 9 etc...) {

System.out.println("Error message here");

}

Obviously there must be a correct way to test if a number is a square. Would a boolean be involved ?

Any help appreciated.

Re: What is a non-zero square?

Hmm I don't know any easy methods, but I would go about it like

Code Java:

for(int i = 0; i < x; i++)
{
if((x/i)==i)
{
//x is a perfect square.
}
}

or

Code Java:

for(int i = 0;i<x;i++)
{
if((i^2) == x)
{
//x is a perfect square
}
}

However, a Google search brings up

java - Fastest way to determine if an integer's square root is an integer - Stack Overflow

It all depends on readability vs efficiency when it comes down to it.

Re: What is a non-zero square?

Hi djl1990,

The equation √(Q2-4PR), is normally written as √(b^2 - 4ac) which is used to determine how many "real" roots a quadratic equation has.

if (b^2 - 4ac) is equal to 0 you have exactlt 1 real root, well technically you have 2 real roots but they are identically.

if it is larger than 0, you have 2 real roots

if it is negative, you have no real roots, only imaginary roots, but you will not be interested in those.

I hope this will help you.

Regards,

Chris