← demo0x6de4e7…4154Succeededblock 90
No position yet0 / 863
FetchingOpeningPositioning
noir
Narrow session: Code, Call Trace and Values only, read-only. The event log and stepping need a wider viewport.
Code
1 rows = "3"
2 cols = "4"
1 // TOUR: loops and iteration
2 //
3 // The subject is the loop rail. Everything here exists so that a reader who
4 // opens this program sees iterations they can move between one at a time, and
5 // values that DIFFER from one iteration to the next — a loop whose variables
6 // never change is a rail with nothing on it.
7 //
8 // Three shapes, because the rail has to cope with all three:
9 // * a flat `for` with a carried accumulator,
10 // * a `for` nested inside a `for`, so an iteration of the outer rail
11 // contains a whole inner rail,
12 // * a `while` whose bound is computed, so the iteration count is not
13 // readable off the source.
14
15 mod sieve;
16
17 unconstrained fn triangular(n: u32) -> u32 {
18 // A flat loop with one carried variable. Iteration i adds i, so the
19 // accumulator takes a different value on every rung of the rail and the
20 // sequence 0, 0, 1, 3, 6, 10 ... is derivable from the source alone.
21 let mut acc: u32 = 0;
22 for i in 0..n {
23 acc = acc + i;
24 }
25 acc
26 }
27
28 unconstrained fn grid_sum(rows: u32, cols: u32) -> u32 {
29 // Nested loops. The outer rail has `rows` rungs; each one contains an
30 // inner rail of `cols` rungs. `row_total` resets at the top of every outer
31 // iteration, which is the thing per-iteration navigation makes visible and
32 // a single "current value" cannot show.
33 let mut total: u32 = 0;
34 for r in 0..rows {
35 let mut row_total: u32 = 0;
36 for c in 0..cols {
37 row_total = row_total + (r * cols + c);
38 }
39 total = total + row_total;
40 }
41 total
42 }
43
44 unconstrained fn collatz_steps(start: u32) -> u32 {
45 // A `while` whose trip count depends on the values, not on the bounds.
46 // Reading the source tells you what each step does; only the recording
47 // tells you how many there were.
48 let mut n = start;
49 let mut steps: u32 = 0;
50 while n != 1 {
51 if n % 2 == 0 {
52 n = n / 2;
53 } else {
54 n = 3 * n + 1;
55 }
56 steps = steps + 1;
57 }
58 steps
59 }
60
61 unconstrained fn first_gap(limit: u32) -> u32 {
62 // `break` and `continue` in one loop, so the rail has to show iterations
63 // that did nothing and an iteration that ended the loop early.
64 let mut found: u32 = 0;
65 for candidate in 2..limit {
66 if candidate % 2 == 0 {
67 continue;
68 }
69 if !sieve::is_prime(candidate) {
70 found = candidate;
71 break;
72 }
73 }
74 found
75 }
76
77 unconstrained fn main(rows: u32, cols: pub u32) -> pub u32 {
78 let t = triangular(6);
79 let g = grid_sum(rows, cols);
80 let c = collatz_steps(27);
81 let f = first_gap(30);
82 t + g + c + f
83 }
1 // A second file, so the source pane has more than one document to switch
2 // between and the call trace crosses a module boundary.
3
4 pub unconstrained fn is_prime(n: u32) -> bool {
5 if n < 2 {
6 false
7 } else {
8 let mut prime = true;
9 let mut d: u32 = 2;
10 while d * d <= n {
11 if n % d == 0 {
12 prime = false;
13 }
14 d = d + 1;
15 }
16 prime
17 }
18 }
1 [package]
2 name = "tour_loops"
3 type = "bin"
4 authors = ["BlockTracer capability tour"]
5
6 [dependencies]
Event Log

The recorded event stream is in the published recording. Reading it needs the replay engine, which this page has not started.

Call Trace

The call structure is in the published recording. Reading it needs the replay engine, which this page has not started.

Values

The recorded values are in the published recording. Reading them needs the replay engine, which this page has not started.