물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Factorial

BEstyle 2022. 9. 27. 12:32

DESCRIPTION:

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 * 4 * 3 * 2 * 1 = 120. By convention the value of 0! is 1.

Write a function to calculate factorial for a given input. If input is below 0 or above 12 throw an exception of type ArgumentOutOfRangeException (C#) or IllegalArgumentException (Java) or RangeException (PHP) or throw a RangeError (JavaScript) or ValueError (Python) or return -1 (C).

More details about factorial can be found here.


def factorial(n):
    ans=1
    for i in range(1,n+1):
        ans*=i
    if n<0 or n>12:
        raise ValueError
    return ans

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Friend or Foe?  (0) 2022.09.28
Reverse Letter  (0) 2022.09.28
Will you make it?  (0) 2022.09.27
You're a square!  (0) 2022.09.27
Sort the odd  (0) 2022.09.27