progs/while-tests/collatz2.while
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 06 Oct 2020 00:39:34 +0100
changeset 775 16aafc7691d8
parent 750 e93a9e74ca8e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     1
// Collatz series
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     2
//
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     3
// needs writing of strings and numbers; comments
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     4
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     5
bnd := 1;
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     6
while bnd < 101 do {
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     7
  write bnd;
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     8
  write ": ";
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     9
  n := bnd;
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    10
  cnt := 0;
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    11
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    12
  while n > 1 do {
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    13
    write n;
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    14
    write ",";
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    15
    
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    16
    if n % 2 == 0 
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    17
    then n := n / 2 
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    18
    else n := 3 * n+1;
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    19
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    20
    cnt := cnt + 1
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    21
  };
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    22
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    23
  write " => ";
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    24
  write cnt;
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    25
  write "\n";
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    26
  bnd := bnd + 1
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    27
}