سوال ۶۶ کنکور ارشد علوم کامپیوتر ۱۳۹۹
اهمیت
۰/۵
زمان اجرای قطعه کد مقابل، از چه مرتبهای است؟
i = 2;
while(i <= n)
{
write(*);
i = i * i;
}
پاسخ صحیح: گزینهٔ D —
پاسخ تشریحی
The loop variable starts at and is squared every iteration (), so it grows much faster than the usual doubling.
Track the value of . After iterations of the body, the value is
Each step doubles the exponent, because .
Find when the loop stops. The body runs while , i.e. while
Take once:
Take again:
So the number of iterations is . Each iteration does work, giving total running time
The answer is option 4.
Exam trick: A loop with runs times, (geometric growth) runs times, and (repeated squaring) runs times. Squaring applies twice.
#loop-analysis#time-complexity#asymptotic-notation#logarithm#big-o