interface C{static void main(String[]a){var n=Double.valueOf(a[0])+1;for(int i=1;++i<5;)System.out.print(i*Math.ceil(n/i)+" ");}}
Try it online!
You may notice that no class
has been defined, but instead an interface. This is just to further golf the code. Since the interface hasn't been given a public
access modifier, the main
method doesn't need it, either.
For a better understanding of this code, it's shown below with the variable a
remnamed to its traditional name args
:
var n = Double.valueOf(args[0]) + 1;
for (int i = 1; ++i < 5;)
System.out.print(i * Math.ceil(n / i) + " ");
}
The program inputs a value as a command-line argument which is immediately incremented for compactness before being stored in the double
variable n
. Next, the for loop cycles through each integer i
from 2 through to 4, printing the output values each cycle. See here for an interactive demonstration of the formula used to calculate said values.
In order to further golf this code, I think finding a way to use Java 6 would be ideal due to the static
block's functionality, as expressed below:
public class Foo {
static {
// Your code here
}
}
As you can see, although it's definitely good practice, explicitly defining the main
method isn't necessary in this particular version of Java. (Correct me if that was incorrectly phrased)
Unfortunately, in performing such a conversion, the var
keyword would no longer exist (since it was introduced in Java 10). More importantly, reconstructing the value input mechanism would be necessary, since a static
block obviously doesn't have any method parameters, and especially not for command-line arguments. Feel free to leave comments and suggestions.
C B A
) if it's clearly specified in the answer? \$\endgroup\$