0

I already read the other questions and answers but couldn't implement any of the solutions to my code. I'm still clueless about the reason why this code gives a runtime error.

I'm trying to submit the code on CodeChef, yet it gives the Runtime Error(NZEC), although the code runs flawlessly on my console for some inputs. Here's my code:

def GetSquares(base):
    if not base or base < 4:
        return 0
    else:
        x = (base - 4) - (base % 2) + 1
        return x + GetSquares(base - 4)

num_test = int(input())

for test in range(num_test):
    base = int(input())
    print (int(GetSquares(base)))

Codechef's explanation for NZEC:

NZEC stands for Non Zero Exit Code. For C users, this will be generated if your main method does not have a return 0; statement. Other languages like Java/C++ could generate this error if they throw an exception.

The problem I'm trying to solve:

https://www.codechef.com/problems/TRISQ

3
  • If I had to guess, the int() conversion is failing because the input isn't a valid integer. Try printing it before doing anything, if you can - are you sure that you're not supposed to split it or something? My either guess is that you're experiencing a (heh) stack overflow Commented Aug 2, 2016 at 12:58
  • What inputs does the program run with? What inputs does the program NOT run with? Commented Aug 2, 2016 at 13:00
  • What is the input when it crashes? Commented Aug 2, 2016 at 13:01

2 Answers 2

3

The problem description says that the input is constrained to be < 10^4. That's 10,000! Your code will need to make 10,000/4 = 2500 recursive calls to GetSquares, that's a lot! In fact, it's so much that it's going to give you, fittingly, this error:

RuntimeError: maximum recursion depth exceeded

You're going to have to think of a better way to solve the problem that doesn't involve so much recursion! Because you're doing this coding challenge, I'm not going to give a solution in this answer as that would sort of defeat the purpose, but if you'd like some prodding towards an answer, feel free to ask.

Sign up to request clarification or add additional context in comments.

Comments

0

The question puts a constraint on the value of 'B' which is 10000 at max, which means there are a lot of recursive calls and giving a runtime error. Try solving using iteration.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.