# Recursive algorithm

• Oct 17th 2009, 01:50 PM
zap1231
Recursive algorithm
Give a recursive algorithm for computing n * a using only addition, where n is a positive integer and a is a real number. Hint: add a to itself n times.
• Oct 17th 2009, 01:53 PM
Matt Westwood
Quote:

Originally Posted by zap1231
Give a recursive algorithm for computing n * a using only addition, where n is a positive integer and a is a real number. Hint: add a to itself n times.

This is the standard one:

$n \times a = \begin {cases} 0 & : n = 0 \\ (n-1) \times a + a & : n \ne 0 \end{cases}$

Or if 0 is not considered a positive integer, start with na = 1 when n = 1.