recursion (python 2.7.2)

Discuss Computer Science and Programming related problems

Moderators:Labib, bristy1588

bidrohi
Posts:33
Joined:Mon Dec 27, 2010 1:33 am
recursion (python 2.7.2)

Unread post by bidrohi » Tue Jan 17, 2012 5:49 am

Code: Select all

while 1==1:
    def f(x):
        if x==1:
            return 1
        elif x==0:
            return 1
        else:
            return x*f(x-1)
    i=int(raw_input("ENTER VALUE: "))
    print f(i)

মনে করি আমি উপরের কোড লিখলাম। এরপর IDLE থেকে F5 চেপে অথবা script এ double click করার পর i এর মান ঋণাত্মক সংখ্যা বা অনেক বড় ধনাত্মক সংখ্যা দিলে এটা আসে কেন???

RuntimeError: maximum recursion depth exceeded in cmp

novice
Posts:31
Joined:Sun Dec 12, 2010 12:34 am

Re: recursion (python 2.7.2)

Unread post by novice » Tue Jan 17, 2012 3:45 pm

Every time a recursion takes place,some extra ( except for the ones used up by variables,arrays etc ) memory is required. So if recursion takes place for like 1000000 times , memory limit gets exceeded. Usually memory allocated for such a program in contests is 64 MB. Your code must not make too many calls to exceed that memory limit.

Your code falls into an infinite loop when you give a negative number.

Post Reply