Least Factorial

Description


Given an integer n, find the minimal k such that

  • k = m! (where m! = 1 _ 2 _ ... \* m) for some integer m;
  • k >= n.

In other words, find the smallest factorial which is not less than n.

Example

For n = 17, the output should be leastFactorial(n) = 24.

17 < 24 = 4! = 1 _ 2 _ 3 _ 4, while 3! = 1 _ 2 \* 3 = 6 < 17)

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    1 \leq n \leq 120.

  • [output] integer

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}

Solution


1
2
3
4
5
6
7
8
function leastFactorial(n) {
  var f = 1;
  var i = 1;
  while (f < n) {
    f *= ++i;
  }
  return f;
}