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 "\npython "C:\Users\Fouad Teniou\OddEvenR7 # # Ref XM + DA ```

Christophe Simonis 14 years, 1 month ago

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, 1 month ago

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, 1 month ago

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) 13 years, 11 months ago

``````>>> https://buzzword.acrobat.com/#d=wNPnofoI*0aljK5jXaw9KQ