Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What is a non-zero square?

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: What is a non-zero square?

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

    for(int i = 0; i < x; i++)
    {
      if((x/i)==i)
      {
         //x is a perfect square.
      }
    }
    or
    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.

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

Similar Threads

  1. URgh, drawing a square
    By BITmixit in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 25th, 2011, 12:49 PM
  2. HW Accelerated Square Root
    By tomster in forum Java SE APIs
    Replies: 2
    Last Post: October 3rd, 2011, 05:12 AM
  3. triad square
    By mistu in forum Member Introductions
    Replies: 2
    Last Post: August 22nd, 2011, 08:13 AM
  4. Latin square logic
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2010, 09:38 AM
  5. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM