You are on page 1of 2

Loops and iterations

The standard approach in most programming languages for repeating a process a certain number of
times, such as moving sequentially through an array to perform the same operations on each item,
involves a loop. In pseudocode, this would typically take the general form
For i = 1,...,N,
do something
and in programming languages like C and Java this would be written as the for-loop
for( i = 0 ; i < N ; i++ ) {
// do something
}
A typical program for solving numpower will look like,
int calculate_power_iteration(int num, int power ){
int ans = 1;
for( i = 0 ; i < power ; i++ ) {
ans = ans * num;
}}

We are considering the case where the base and exponent are positive integers.
An alternate approach to solve the problem called Recursion.
Recursion
Recursion means "defining a problem in terms of itself". This can be a very powerful tool in writing
algorithms. Recursion comes directly from Mathematics, where there are many examples of
expressions written in terms of themselves.
For example, the Fibonacci sequence is defined as:
F(i) = F(i-1) + F(i-2)
Parts of a Recursive Algorithm
All recursive algorithms must have the following:
1. Base Case (i.e., when to stop)
2. Work toward Base Case
3. Recursive Call (i.e., call ourselves)
A typical program for solving numpower using recursion will look like,
int calculate_power_recursion(int num, int power ){
if (power == 0){ ---------- Base Case
return(1);
}
else{
return(num * calculate_power_recursion(num, power – 1));
} | |
} | |
| |
| |
Recursive Call Work toward Base Case
As we can see in this problem, the number of computations (here, multiplications), required to be
able to reach to the solution is dependent of exponent and by large, it is independent of base.
Now finally, Can we do better then this??
Divide and Conquer
A divide and conquer algorithm is a strategy of solving a large problem by
1. Divide
2. Conquer, and
3. Combine.
Here are the steps involved:
1. Divide: Divide the given problem into sub-problems using recursion.
2. Conquer: Solve the smaller sub-problems recursively. If the subproblem is small enough,
then solve it directly.
3. Combine: Combine the solutions of the sub-problems that are part of the recursive process
to solve the actual problem.
A typical program for solving numpower using Divide and conquer will look like,
int calculate_power_dac(int num, int power ){
if (power == 0){
return(1);
}
else if (power%2 == 1){
return(num *
calculate_power_dac(num, power/2) *
calculate_power_dac(num, power/2));
}
else{
return(calculate_power_dac(num, power/2) *
calculate_power_dac(num, power/2));
}
}
 Divide.
 Conquer.
 Combine.
The number of multiplications to reach to the solution when power is of the form 2n where n = 1, 2,
3, ... is log2(2n), which is basically, n. Howerver, for other values of power number of
n
⌊ ⌋
n 2
⌈ ⌉
multiplications will be 2 + ∑ 2i .
2

i=0
This gives rise to an important question, which number should be considered?

You might also like