How to do an analysis in 5 steps
Step 0: It is good to start by putting your code in.
unsigned int factorial (unsigned int n){
unsigned int rc = 1;
//multiplying 1*1 gives 1 so we don't start from 1
for (unsigned int i=2;i<=n;i++){
rc=rc*i;
}
return rc;
}```cpp
put your code here. code will be c++ syntax formatted{% endhint %}
## **Step 1: Establish variables and functions \(mathematical ones\):**
{% hint style="info" %}
Similar to writing a program you want to declare your variables and function prototypes first. You need to do this to establish context... variables need a meaning, functions need a meaning. You need to state what they represent first. **These statements need to be placed above the code block**
{% endhint %}
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
## **Step 2: Count your operations**
{% hint style="info" %}
In this step you are explaining how it is that you come up with your final equation. Essentially, you are establishing the logic behind the mathematical statement you will make in step 3-
{% endhint %}
Put the operation count in a comment beside each line of the code blurb. The loop runs n-1 times \(i goes from 2 to n inclusive\). Thus any operations that occur in the loop is counted as n-1.
```cpp
unsigned int factorial (unsigned int n){
unsigned int rc = 1; // 1
//multiplying 1*1 gives 1 so we don't start from 1
for (unsigned int i=2;i<=n;i++){ //1 + (n-1) + (n-1)
rc=rc*i; //2(n-1)
}
return rc; //1
}Step 3: Establish the Mathematical Expression for T(n)
Step 4: Simplify your Equation
Step 5: State your final result.
Last updated