Funcia
Tutorialmath

Factorial

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Given an integer n, return its factorial n!=1×2×⋯×nn! = 1 \times 2 \times \cdots \times n, with 0!=10! = 1.

The value grows fast, so the return type is a 64-bit integer (long); 20!20! is about 2.4×10182.4 \times 10^{18}.

Examples

nresult
5120
01
11

Example 1 — 1 × 2 × 3 × 4 × 5 = 120.

Example 2 — 0! is defined as 1.

Example 3 — 1! = 1.

This is a tutorial problem: there are no performance tests, just return the correct value.

Function signature

solution(n: int) → long

Constraints

  • 0≤0 \le n ≤20\le 20

Expected complexity

Time
O(N)
Space
O(1)

Examples

nExpected
5120
01
11

Hint

Hint

Start a result variable at 1 and multiply by 2, 3, …, n in a loop. In C, C++, Java and C#, declare it as long (long long in C/C++): an int overflows from 13!13! on.

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1200 msestimated
C# 14 (.NET 10)1200 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness10010verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.