简体   繁体   中英

python program to find if number is even or odd using recursion

so i want to find a even number by using a recursive function that repeatedly subtracts 2 from the number to find if it is even. So far the function that i have is

def isEven(number):
   if number!=0:
     return(n-2)
   while number =<2:
     if number==2:
        print("NUmber is even")
     else:
        print("number is odd")

is this function is not working could somehelp me fix it

A recursive implementation would look like this:

def isEven(number):
    if number < 2:
        return number % 2 == 0
    return isEven(number - 2)

Output:

>>> isEven(3)
False
>>> isEven(2)
True

Your function doesn't call itself and it is therefore not using recursion. A while loop would be used in a non-recursive function as the loop would repeatedly subtract 2 from the number.

The function given above checks whether the number is less than 2 and then outputs the answer ( number % 2 == 0 means: is the remainder after dividing by 2 equal to zero?). If the number is bigger than 2 we recursively call isEven with a number that is smaller ( number - 2 ). This means we'll repeatedly call isEven with a smaller number until the number is less than 2.

This is a classical example of mutually recursive functions:

def even(number):
    if number == 0:
        return True
    return odd(number - 1)

def odd(number):
    if number == 0:
        return False
    return even(number - 1)

def isEven(number):
    if even(number):
        print("Number is even")
    else:
        print("Number is odd")
def isEven(x):
    if x == 0:
        return True
    elif x == 1:
        return False
    else:
        return not isEven(x-1)

Just don't try doing it on numbers greater than 994 or smaller than 0!

Explanation:

You can't call this function on any numbers smaller than zero, becuase any input that isn't 0 or 1 results in the function being called on a smaller number. If the first call of the function is on a negative number, subtracting one from it is clearly never going to lead to it being 0 or 1, so the function will endlessly call itself, the recursive equivalent to an infinite loop. However, you could modify this function to allow it to work for negative inputs - this is left as an exercise to the reader!

You can't call it on numbers greater than 994 (or thereabouts (or rather numbers greater than, equal to or slightly less than the recursion depth)) because it causes a StackOverflow error (described as a RuntimeError: maximum recursion depth exceeded ). To understand what this means, you will need to understand recursion (and in order to understand recursion, you need to understand recursion, as the old joke goes). Or you could look at useful resources like this and this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM