Welcome, guest | Sign In | My Account | Store | Cart

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.

Python, 84 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#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

4 comments

Christophe Simonis 14 years, 11 months ago  # | flag

In [11]: range(7, 0, -2) Out[11]: [7, 5, 3, 1]

In [12]: range(-7, 0, 2) Out[12]: [-7, -5, -3, -1]

In [13]: range(8, -1, -2) Out[13]: [8, 6, 4, 2, 0]

In [14]: range(-8, 1, 2) Out[14]: [-8, -6, -4, -2, 0]

Fouad Teniou (author) 14 years, 11 months ago  # | flag

To test my program you need to write the name of the function, which is OddEvenRecursion, thus if you would like to test the numbers 11,12,13, and 14 you just need to write the following:

>>> for item in range(11,15):
>>>      print OddEvenRecursion(item),OddEvenRecursion(-item)
and you will get 
>>> [11,9,7,5,3,1]
>>> [-11,-9,-7,-5,-3,-1]
>>> [12,10,8,6,4,2,0]
>>> [-12,-10,-8,-6,-4,-2,0]
>>> [13,11,9,7,5,3,1]
>>> [-13,-11,-9,-7,-5,-3,-1]
>>> [14,12,10,8,6,4,2,0]
>>> [-14,-12,-10,-8,-6,-4,-2,0]

and your outcome for whatever fuction ( In , Out ) you used is wrong!!!

Fouad Teniou (author) 14 years, 11 months ago  # | flag

PS: You will get the sums of the sets too..., which are not shown in your commentby applying your (In Out)function and which are available using my Program.

Fouad Teniou (author) 14 years, 10 months ago  # | flag
Created by Fouad Teniou on Sat, 9 May 2009 (MIT)
Python recipes (4591)
Fouad Teniou's recipes (37)

Required Modules

  • (none specified)

Other Information and Tasks