How to do an analysis in 5 steps
in Algorithm
Contents
- Example Code
- Step 1: Establish variables and functions (mathematical ones)
- Step2: Count your operations
Example Code
unsigned int factorial(unsigned int n){
unsigned int rc = 1;
// we don't start from 1 because multiplying 1*1 gives 1. So start from 2
for(unsigned int i = 2; i <= n; i++){
rc = rc * i;
}
return rc;
}
Step 1: Establish variables and functions (mathematical ones)
Let n represent the value we are finding the factorial for
Let T(n) represent number of operations needed to find n! using the code