class ReverseFactorial
{
public static void unfactorialize(int input) {
double x = input;
int f = 2;
while(x > 1) {
x = x / f;
if(x == 1) {
System.
out.
println(input
+ " = " + f
+ "!"); //check for x == 1 BEFORE you increase f return;
}
f++;
}
System.
out.
println(input
+ " NONE"); //if the program reaches this point, it has left while loop } //and there is no reverse factorial
public static void main
(String[] args
) { unfactorialize(120);
unfactorialize(150);
unfactorialize(3628800);
unfactorialize(479001600);
unfactorialize(6);
unfactorialize(18);
}
}
Y2xhc3MgUmV2ZXJzZUZhY3RvcmlhbAp7CglwdWJsaWMgc3RhdGljIHZvaWQgdW5mYWN0b3JpYWxpemUoaW50IGlucHV0KSB7CiAKICAgIGRvdWJsZSB4ID0gaW5wdXQ7CiAgICBpbnQgZiA9IDI7CiAKICAgIHdoaWxlKHggPiAxKSB7CiAgICAgICAgeCA9IHggLyBmOwogICAgICAgIGlmKHggPT0gMSkgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oaW5wdXQgKyAiID0gIiArIGYgKyAiISIpOyAgIC8vY2hlY2sgZm9yIHggPT0gMSBCRUZPUkUgeW91IGluY3JlYXNlIGYKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBmKys7CiAgICB9CiAgICBTeXN0ZW0ub3V0LnByaW50bG4oaW5wdXQgKyAiICBOT05FIik7ICAgIC8vaWYgdGhlIHByb2dyYW0gcmVhY2hlcyB0aGlzIHBvaW50LCBpdCBoYXMgbGVmdCB3aGlsZSBsb29wCn0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vYW5kIHRoZXJlIGlzIG5vIHJldmVyc2UgZmFjdG9yaWFsCgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIHVuZmFjdG9yaWFsaXplKDEyMCk7CiAgICAgICAgdW5mYWN0b3JpYWxpemUoMTUwKTsKICAgICAgICB1bmZhY3RvcmlhbGl6ZSgzNjI4ODAwKTsKICAgICAgICB1bmZhY3RvcmlhbGl6ZSg0NzkwMDE2MDApOwogICAgICAgIHVuZmFjdG9yaWFsaXplKDYpOwogICAgICAgIHVuZmFjdG9yaWFsaXplKDE4KTsKICAgIH0KfQ==