JavaEasyNumbers10 min

Compute a factorial with BigInteger

Print n factorial as an exact decimal integer. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe algorithm selection, invariants, and boundary handling.

Input

One integer n.

Output

Print n factorial as an exact decimal integer.

Constraints

0 <= n <= 1000.

Example

Input

5

Output

120

Example 2 input

100

Output

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

This example exercises exact arithmetic beyond primitive integer capacity.

Public test cases (2)

  • Input

    5

    Expected output

    120
  • Input

    10

    Expected output

    3628800
Hints (2)
  1. Identify the state needed to avoid recomputing work for each element.
  2. Multiply the integers from 2 through n using BigInteger to avoid overflow.

Target complexity: time O(n) multiplications · space O(n log n) bits

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All Java programs