pythonpython-3.xrecursionfactorialpyhook

Python Code when submitted returns Runtime Error - NZEC


def fact(y):
    if y == 1 or y == 0:
        return 1
    else:
        return y*fact(y-1)

x,n= map(int,input().split())

f = fact(n)%10
l = x**f

print(l%10)

It was getting submitted partially.


Solution

  • most likely value of y is greater than 10^4. python's recursion stack space is 10^4. if this space runs out compiler throws nzec this can be prevented by adding this.set the limit according to the max length of y.

    from sys import setrecursionlimit
    setrecursionlimit(10**9)
    

    Add constraints from next time.