KnowledgeBoat Logo

Computer Science

Consider the following Python function that uses recursion.

def check (n) :
    if n <=1:
        return True
    elif n % 2 == 0:
        return check (n/2) 
    else:
        return check (n/1)

What is the value returned by check(8) ?

Python Functions

3 Likes

Answer

True is returned by check(8).

Explanation

The provided code defines a recursive function check(n) that takes single argument. Inside the function, it checks if a given number n is less than or equal to 1, in which case it returns True. If n is even (i.e., divisible by 2 without a remainder), the function recursively calls itself with n/2. If n is odd, it recursively calls itself with n/1 (which effectively does not change n). However, the condition n/1 doesn't modify n and leads to an infinite recursion for odd numbers greater than 1, causing the function to keep calling itself indefinitely without reaching a base case.

Answered By

3 Likes


Related Questions