Question

For this algorithm:

       i = 1
       while(i<=2n){
       x = x + 1
       i = i + 2
       }

can someone tell me how to find the formula for the number of times x = x + 1 is executed?

Was it helpful?

Solution

i goes from 1 to 2n (inclusive), so first thought is 2n.
But we see that i increments by two at a time instead of one, so it's half of that: n.

OTHER TIPS

With n<1 number of times executed=0. With n>=1 number of times executed=n.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top