سوال ۶۲ کنکور ارشد علوم کامپیوتر ۱۴۰۲
اگر توان صحیحی از ۳ باشد، خروجی شبهکد زیر چند است؟
i = ۱
j = ۱
while (i < n) {
i = i * ۳
j = j * ۳
}
print(j)
پاسخ صحیح: گزینهٔ D —
Let , so .
Trace the variable . It starts at and is tripled each pass. The guard i < n holds until reaches , so the loop executes exactly times. On pass (for ) the statement i = i * 3 runs first, making
Trace the variable . Each pass multiplies by the current value of (i.e. , since is updated before ):
Convert back to . Using and :
Hence the output is
which is option 4.
Sanity check (): , and . ✓
Pitfall: The exponents of form an arithmetic sequence , so the product of all -values gives , a super-polynomial (quasi-polynomial) growth — not the linear you would get if were simply tripled each step. Watch whether the accumulator multiplies by a constant or by the growing loop variable.