From the course: Python Parallel and Concurrent Programming Part 2

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Divide and conquer: Python demo

Divide and conquer: Python demo - Python Tutorial

From the course: Python Parallel and Concurrent Programming Part 2

Start my 1-month free trial

Divide and conquer: Python demo

- [Narrator] To demonstrate a parallel divide and conquer algorithm in Python, we'll implement a function that recursively sums together all of the integers between two values. It should return the same result as we would get by calling Python's built in sum function on that same range of numbers. We'll start with a single threaded implementation of the recursive sum routine. Which takes two input parameters on line four, a low value, and a high value representing the range of numbers to sum over. The if statement on line five looks at the difference between the low and high values to determine if the problem has been sufficiently subdivided. And if so, we've reached the base case, so it returns the sum of numbers in that range. Otherwise, the else statement beginning on line seven determines the middle index between low and high, then recursively calls the recursive sum function on the low to middle index. Which is referred to here as the left half. And from the middle to high index,…

Contents