Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java program isn't working please help

  1. Jun 21, 2010 #1

    I wanted to make a program that would check if a number if it was an integer or not in Java. here's the source code:

    import static java.lang.Math.*;

    public class gcd {
    public static void main(String [] args) {

    double a = 1;
    double b =1;
    double c = 1;
    int counter = 1;

    if ( counter < 10) {

    b =10 * Math.sqrt((a * a - 42)/3);

    c = b%10;

    System.out.println("a: " + a);
    System.out.println("b: " + b);
    System.out.println("c: " + c);
    System.out.println("counter: " + counter);

    if(c == 0) {
    counter = counter + 1;

    For the output I get
    a: 2.0
    b: NaN
    c: NaN

    So the loop doesn't work and i get weird NaN values for b and c....i run it by changing the directory on command prompt and typing in "java gcd"

    I used to use JCreator in my Java class....any recommended free programs for java?

  2. jcsd
  3. Jun 21, 2010 #2

    Filip Larsen

    User Avatar
    Gold Member

    There is no loop in your code. You probably want to change the first if-statement to a while-statement.
    Your code calculates the square root of a negative number. I have no idea what it is you think the code is supposed to do, but it surely is not what the name gcd (greatest common divisor) implies. If you want an algorithm for calculating gcd then search for "Euclid's algorithm".
  4. Jun 21, 2010 #3
    The idea for this program is derived from Euclid's algorithm...i just need to see if a number is an integer or not

    If I fix the loop to while and increase the upper bound of the counter to say 35, woudl this program help me see if b contains decimals or not?
  5. Jun 21, 2010 #4


    Staff: Mentor

    It's very difficult to ascertain what you're trying to do. You said in the OP
    yet the name of your class is gcd, which suggests you want to find the greatest common divisor.

    What are you trying to do? If you want to check that a double is actually an integer, you can use the floor() function to find the largest integer that is less than or equal to your number. IOW, val - floor(val) should evaluate to 0.0 if val is an integer.
    thread title is
  6. Jun 21, 2010 #5
    It's very complicated and one of the steps to finding the GCD requires me to find a number that is an integer...
  7. Jun 21, 2010 #6

    Filip Larsen

    User Avatar
    Gold Member

    Euclid's algorithm is very easy to implement unless you have some very special needs. If you need to process arbitrary big numbers you can always fall back on using the java.math.BigInteger class which can calculate GCD for you [1].

    I still have no idea what you mean with "find a number that is an integer". What type of numbers do you have and why do you need to find GCD in the first place?

    [1] http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html#gcd(java.math.BigInteger)
    Last edited: Jun 21, 2010
  8. Jun 21, 2010 #7

    Filip Larsen

    User Avatar
    Gold Member

    It dawns on me that you perhaps are trying to find (positive) integer solutions to the equation a2 = 3b2+42. If so, I will suggest that you start with a suitable value of a (like 7) and b (like 0) and then test in a loop if a and b are a solution by inserting their value into the left hand side (lhs) and right hand side (rhs). If the lhs equals the rhs you have a solution which you can print, after which you can stop the loop or increase either a or b by one and loop for more solutions. If lhs is less than the rhs, increase a by one; if lhs is greater than the rhs, increase b by one.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook