سؤال

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