3

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

rggod
  • 611
  • 5
  • 11
  • 19
  • Well, one thing is that Python does not have a `when` keyword. Perhaps you meant `while`? –  Nov 21 '13 at 18:32
  • This will never correctly print that a number is even, and never print at all if the number is odd. It should recurse infinitely if you have an odd number, and if you do have an even number, it will print out that it is odd because it only gets to the bottom if it is 0, and 0 is not 2. – jonhopkins Nov 21 '13 at 18:35
  • Also, `number =<2` is a SyntaxError. This should be `number <= 2`... – Tim Pietzcker Nov 21 '13 at 18:43

3 Answers3

3

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.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
3

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")
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2
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.

Community
  • 1
  • 1
rlms
  • 10,650
  • 8
  • 44
  • 61