سوال ۶۶ کنکور ارشد علوم کامپیوتر ۱۴۰۱
خروجی الگوریتم زیر از چه مرتبهای است؟
j = ۰
i = ۱
while(i ≤ n)
j = j + ۲*i
i = ۴*i
print j
پاسخ صحیح: گزینهٔ A —
The question asks for the order of the value printed (the output j), not the running time.
Trace the loop. The variable i starts at and is multiplied by each pass, so it takes the values
The loop continues while , i.e. while . Let be the largest such exponent.
Accumulate j. Each iteration adds , so
This is a geometric sum dominated by its last term, so .
Relate to . By definition of we have , hence
Therefore
The printed value grows linearly in , so the answer is — option 1.
Exam trick: A geometric series with ratio equals . Here the largest term is itself , so you never need to count iterations — only the magnitude of the final term matters. (The number of iterations is , which would be the running time; do not confuse it with the output value.)