Odd-Even Recursion

Odd-Even Recursion program is a recursive function, which does produce a positive or a negative set of odd or even numbers. However, the function return numbers which are lower or greater than the value of the number entered by the user until it reaches its base case value, where the function returns a value and the recursion stops. Though solving repetitive problems requires a powerful method and yet recursion allows the design of faster recursive algorithms. Recursion method could be used to solve higher-level mathematics' problems, such as sequences and it is a branch in the computer science study.


Beyond Space Project link page https://buzzword.acrobat.com/#d=wNPnofoI*0aljK5jXaw9KQ

#On the name of ALLAH
#Author : Fouad Teniou
#Date : 9/05/09
#version :2.4

""" 

OddEvenRecursion program uses a recursion function
to calculate the sum of odd or even numbers less or equal
to the odd or the even number entered by the user
and it produce a set of these numbers. 

"""

# Starting with an empty list.
list_A = []

def OddEvenRecursion(number):
    """ Returns a set and a sum of numbers in the set using recursion """
    
    try:
        #Append the list with odd or even numbers
        list_A.append(number)
    
        if number == 0 or number == 1 or number == -1:
            # Display the even numbers' set and the sum of these numbers
            print "\n<The numbers' set : %s \n\n<And the sum is :" % \
              list_A
            return number
        elif number < 1:
            return number + OddEvenRecursion(number + 2)
        else:
            return number + OddEvenRecursion(number - 2)

    except TypeError:
        print 'Please enter an integer.'
    
if __name__ == "__main__":
    
    # testing an odd value.
    odd = OddEvenRecursion(7) 
    print odd
    # Empty the list
    del list_A[:]
    # testing a negative odd  value.
    negativeOdd = OddEvenRecursion(-7) 
    print negativeOdd
    # Empty the list
    del list_A[:]    
    # testing an even value.
    even = OddEvenRecursion(8) # testing an even value.
    print even
    # Empty the list
    del list_A[:]
    # testing a negative even value.
    negativeEven = OddEvenRecursion(-8) # testing an even value.
    print negativeEven

-----------------------------------------------------------------------------------------------

# c:\Python26>python "C:\Users\Fouad Teniou\OddEvenR7

# <The numbers' set : [7, 5, 3, 1]

# <And the sum is :
# 16

# <The numbers' set : [-7, -5, -3, -1]

# <And the sum is :
# -16

# <The numbers' set : [8, 6, 4, 2, 0]

# <And the sum is :
# 20

# <The numbers' set : [-8, -6, -4, -2, 0]

# <And the sum is :
# -20

# c:\Python26> 
# Ref XM + DA

code.activestate.com ↗