0% found this document useful (0 votes)
36 views3 pages

Handouts LectureSlides Lecture5 2

This document describes a recursive algorithm for multiplying two numbers a and b. It defines multiplication as adding a to itself b times. It then explains that this can be computed recursively by reducing the problem to a smaller case where b is decreased by 1, plus adding the original number a. Pseudocode is provided that bases the recursion on b equaling 1, and otherwise recursively calls the function with b decreased by 1 and a added to the result.

Uploaded by

priyanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Handouts LectureSlides Lecture5 2

This document describes a recursive algorithm for multiplying two numbers a and b. It defines multiplication as adding a to itself b times. It then explains that this can be computed recursively by reducing the problem to a smaller case where b is decreased by 1, plus adding the original number a. Pseudocode is provided that bases the recursion on b equaling 1, and otherwise recursively calls the function with b decreased by 1 and a added to the result.

Uploaded by

priyanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Recursive

version
An alterna0ve is to think of this computa0on
as:
a * b = a + a + + a

b copies

= a + a + + a

b-1
copies
= a + a * (b 1)

Recursion
This is an instance of a recursive algorithm
Reduce a problem to a simpler (or smaller) version of
the same problem, plus some simple computa0ons
Recursive step

Keep reducing un0l reach a simple case that can be


solved directly
Base case

a * b = a; if b = 1 (Base case)
a * b = a + a * (b-1); otherwise (Recursive case)

def recurMul(a, b):!


if b == 1:!
return a!
else:!
return a + recurMul(a, b-1)!

You might also like