質問

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?

役に立ちましたか?

解決

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.

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top