Recursive Multiplication Using Only Addition and Subtraction

 1. Write a python program named “recursion_add_subtract.py”. The program should contain a recursive python function to compute the product of two positive integers’ m and n, using only addition and subtraction. The main method should test the function.

2. Write a python program named “recursion_integers.py”. The program should contain a recursive python function that rearranges a list of integers from largest to smallest. The initial list should not be sort and do not sort it with the sort method. It should all be done recursively. The main method should test the function.

3. Using timeit, code the measurements for 5 test runs but also code the average from the results received from timeit.



The answer


Description:

This program defines a recursive function to compute the product of two positive integers using only addition and subtraction, without using multiplication or built-in product functions. The main section tests the function with sample input.

Code:

python

# recursion_add_subtract.py def recursive_product(m, n): """Recursively multiply m by n using only addition and subtraction.""" if n == 0: return 0 elif n == 1: return m else: return m + recursive_product(m, n - 1) def main(): m = 7 n = 4 print(f"Product of {m} and {n} is {recursive_product(m, n)}") # More tests print(f"Product of 5 and 0 is {recursive_product(5, 0)}") print(f"Product of 9 and 1 is {recursive_product(9, 1)}") if __name__ == "__main__": main()

2. recursion_integers.py

Title:

Recursive Sorting of Integers from Largest to Smallest

Description:

This program defines a recursive function that rearranges a list of integers from largest to smallest. The function does not use the sort method or any built-in sort; the sorting is done recursively. The main section demonstrates and tests the function.

Code:

python

# recursion_integers.py def recursive_sort_desc(lst): """Recursively sort the list in descending order.""" if len(lst) <= 1: return lst else: max_elem = max(lst) lst.remove(max_elem) return [max_elem] + recursive_sort_desc(lst) def main(): nums = [7, 2, 9, 3, 5, 10, 1] print("Original list:", nums) sorted_nums = recursive_sort_desc(nums.copy()) print("Sorted list (largest to smallest):", sorted_nums) if __name__ == "__main__": main()

3. Timing and Averaging with timeit

Title:

Measuring and Averaging Execution Time Using timeit

Description:

This example uses the timeit module to measure the execution time of the recursive sort function for 5 runs and computes the average time.

Code:

python

import timeit from recursion_integers import recursive_sort_desc def test_run(): nums = [7, 2, 9, 3, 5, 10, 1] recursive_sort_desc(nums.copy()) # Measure 5 runs times = timeit.repeat("test_run()", setup="from __main__ import test_run", repeat=5, number=1) for i, t in enumerate(times, 1): print(f"Run {i}: {t:.6f} seconds") average_time = sum(times) / len(times) print(f"Average time: {average_time:.6f} seconds")

Note:
If you place all code in one file, you can copy the sort and test functions above the timeit section.
If you want to measure the product function, replace recursive_sort_desc with recursive_product and adapt the arguments.


Now you have a complete, modular, and well-documented solution for all requirements!


📩 Need a similar solution? Email me: adel455@hotmail.com

Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming