Exercise 3: A Tripling Loop — Possible Solution ==================================================================== GIVEN ------------------------------ i starts at 1, and i *= 3 each iteration, continuing until i >= n. n = 1,000,000. STEP 1: THE BIG-O CLASS ------------------------------ Per this chapter's own Gotcha 2, a loop variable that grows MULTIPLICATIVELY (here, tripling each iteration) rather than by a fixed increment gives O(log n), not O(n) - the same reasoning as the chapter's own doubling-loop example, just with a different base. STEP 2: COMPUTING THE EXACT ITERATION COUNT ------------------------------ Tracking i after each iteration (starting at i=1): Iteration 1: i = 3 Iteration 2: i = 9 Iteration 3: i = 27 ...continuing to triple each time... Iteration 13: i = 3^13 = 1,594,323 (first value >= 1,000,000) Checking iteration 12: i = 3^12 = 531,441, which is still less than 1,000,000, so the loop has not yet stopped. So the loop runs exactly 13 times before i first reaches or exceeds 1,000,000. STEP 3: CONFIRMING AGAINST log BASE 3 ------------------------------ log base 3 of 1,000,000 ~= 12.58, and the loop must run a whole number of times, rounding up to the next complete iteration: 13 - matching the direct count exactly, per this chapter's own O(log n) classification (using base 3 instead of base 2, though the Big-O class itself, O(log n), doesn't depend on which base is used). WHY THIS WORKS AS AN ANSWER ------------------------------ The Big-O classification is justified using this chapter's own Gotcha 2 reasoning about multiplicative loop variables, and the exact iteration count is both traced step by step and independently confirmed against the log-base-3 calculation, matching the same two- method verification approach this chapter's own doubling-loop example used.