Untitled

mail@pastecode.io avatar
unknown
python
a month ago
813 B
2
Indexable
Never
def factorial(n):
    result = 1

    for i in range(2, n + 1):
        result *= i

    return result


def factorial_recursion(n):
    if n == 2:
        return 2
    else:
        return n * factorial_recursion(n - 1)


if __name__ == '__main__':
    n = int(input("Podaj n: "))
    k = int(input("Podaj k: "))

    if n < 1:
        print(f"Liczba n = {n} nie jest naturalna")
        exit(0)
    if k < 1:
        print(f"Liczba k = {k} nie jest naturalna")
        exit(0)

    if k < 0:
        print(f"Liczba k = {k} nie spelnia warunkow zadania")
        exit(0)
    if k > n:
        print(f"Liczba k = {k} nie spelnia warunkow zadania")
        exit(0)

    result = factorial_recursion(n) / (factorial_recursion(k) * factorial_recursion(n - k))

    print(result)
Leave a Comment