Search code examples
java

Not outputting answer - console blank , no errors given, Java


I am new to coding in Java so I am practicing with the Euler Projects to get into it. I am using Eclipse and my issue is that code that to my eyes should work fine doesn't work. I don't get an error, but nothing outputs to the console either. The console is blank.

This is my code for question 5:

    /*
2520 is the smallest number that can be divided by each of the numbers 
from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all 
of the numbers from 1 to 20?
 */

class main {

    public static void main(String[] args) {
        int i = 2520; 
        int biggestNum = 0; 
        while (biggestNum != i) { 
            for (int j = 1; j <= 20; j++) { 
                if (i%j != 0) {break;}
                if (j == 20) {biggestNum = i; }
            }
            i++; 
        }
        System.out.println(biggestNum);
    }
}

and this is the code for question 3:

/*
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

 */

public class euler3 {
    public static boolean isPrime (long num) { 
        for (long i = 2; i < num; i++) { 
            if (num % i == 0 ) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        long num = 600851475143L; 
        long x = 1; 
        for (int i = 2; i <= num; i++) { 
            if (num % i == 0) { 
                if (isPrime(i)) {
                    x = i;
                }
            }
        }
        System.out.println(x);
    }

}

The thing with question 3, though, is that it works for the small number (13195) and it gives me 29, but I get the blank console again for the larger one.

I decided to include both of these questions since they have the same problem of not outputting my answer to the console. Any help would be appreciated.


Solution

  • I decided to include both of these questions since they have the same problem of not outputting my answer to the console. Any help would be appreciated.

    When debugging your code, be sure to do so one at a time. Just because both programs aren't displaying the output that you expect doesn't mean that they have the same issue.

    There are many ways to debug your buggy code. You can do "printf" style debugging by placing System.out.println(...) statements inside of your loops so you can see what is happened or your can use a Java debugger. The 2nd answer can often get a faster result in terms of finding the problem but you need to learn about debuggers and it depends on which IDE you are using.

    When you get more experienced, being able to execute the code in your head really helps with bugs as these. For example, I can look at your code and see:

    1. for loop is going from 2 to 600851475143L
    2. tests if the number if prime
    3. sets x to be the number if it prime.
    4. continue. OH it's not breaking from the loop.

    You have to go step by step through your code either in your head, with a debugger, or by walking through debug output to find these sorts of bugs.