solutions/cw4a/collatz2.while
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:59:32 +0000
changeset 976 e9eac62928f5
parent 910 b655ce68983f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
910
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Collatz series
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
// needs writing of strings and numbers; comments
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
bnd := 1;
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
while bnd < 100001 do {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
  write bnd;
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
  write ": ";
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  n := bnd;
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
  cnt := 0;
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  while n > 1 do {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
    write n;
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
    write ",";
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
    
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
    if n % 2 == 0 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
    then n := n / 2 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
    else n := 3 * n+1;
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
    cnt := cnt + 1
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
  };
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
  write " => ";
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  write cnt;
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  write "\n";
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  bnd := bnd + 1
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
}