cwtests/cw03/collatz2.while
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 25 Sep 2023 15:12:11 +0100
changeset 929 9541e073f2ed
parent 836 a3418ee8c404
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
836
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Collatz series
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
// needs writing of strings and numbers; comments
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
bnd := 1;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
while bnd < 101 do {
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
  write bnd;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
  write ": ";
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  n := bnd;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
  cnt := 0;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  while n > 1 do {
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
    write n;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
    write ",";
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
    
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
    if n % 2 == 0 
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
    then n := n / 2 
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
    else n := 3 * n+1;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
    cnt := cnt + 1
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
  };
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
  write " => ";
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  write cnt;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  write "\n";
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  bnd := bnd + 1
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
}