This text was translated automatically and has not been checked by a native speaker yet. If you spot a mistake, please let us know.
Mathematics · Grade 10 · logic
Algorithms: step-by-step execution Turn on JavaScript to use the trainer. The rule and worked examples are available without it.
More practice for this grade Rule and examples Algorithms: step-by-step execution Run an algorithm step by step, writing down the values of the variables. A “while” loop repeats as long as its condition is true; at a branch, check the condition first. In Euclid’s algorithm, divide a by b with remainder until the remainder is 0.
Example 1. Run the algorithm. What number does it output? 1. x = 1 2. If x < 38, go to step 3; otherwise go to step 4. 3. x = 2 · x; go back to step 2. 4. Output x.
x: 1 → 2 → 4 → 8 → 16 → 32 → 64 64 ≥ 38 — the loop stops after 6 repetitions. Answer: 64
Example 2. Run the algorithm. What number does it output? 1. a = 34, b = 33 2. If a + b > 50, then c = a + b − 50; otherwise c = 50 − a − b. 3. Output c.
Here a = 34, b = 33: the condition “a + b > 50” is true. c = a + b − 50 = 17 Answer: 17
Example 3. Run the algorithm. What number does it output? 1. x = 1 2. If x < 83, go to step 3; otherwise go to step 4. 3. x = 3 · x; go back to step 2. 4. Output x.
x: 1 → 3 → 9 → 27 → 81 → 243 243 ≥ 83 — the loop stops after 5 repetitions. Answer: 243