Pages

Is one?

/** A REAL Classic First Program
 *
 * This is a mysterious Java program. It's not very efficient,
 * but it gets the job done. Can you figure out what it does?
 * When you think you know, run it and find out for sure.
 * Before you do, however, try to predict what it will print
 * if you pass it "104" as the command-line argument. Were you
 * right? Tip: Go to https://www.jdoodle.com/online-java-compiler
 * to run the program. Just copy and paste the code below (and
 * this comment too, if you want) into JDOODLE's Java editor;
 * enter your command line argument; and press Execute.
 *
 * See: https://www.webofstories.com/play/donald.knuth/21
 * See also: https://en.wikipedia.org/wiki/Goldbach%27s_conjecture
 */
public class PrintHelloWorld {
    public static void main(String args[]) {
        final int m = args.length > 0 ? Integer.parseInt(args[0]) : 0;
        for (int helloWorld = 1; helloWorld <= m; helloWorld++) {
            boolean isOne = true;
            for (int d = 2; d < helloWorld; d++)
            {
                int r = helloWorld % d;
                if (r == 0)
                    isOne = false;
            }
            if (isOne)
                System.out.println(helloWorld);
        }
    }
}