Turing Trains

 

computational train track layouts

menu

Triangular Numbers

We can construct a program to find the terms of the triangular numbers series. Each term is the sum of first n natural (counting) numbers.

These follow the arithmetic sequence 0, 1, 3, 6, 10, 15... where each term increases by the count value 1, 2, 3...

We need 3 registers:

  1. An Input register, set to the nth term required. (n)
  2. A Count register incremented with each cycle. (C)
  3. A Sum register to hold the accumulating sum. (S)
Count
n
Count
+ Sum
new Sum
0
1
2
3
4
5
6
7
0 + 0
1 + 0
2 + 1
3 + 3
4 + 6
5 + 10
6 + 15
7 + 21
0
1
3
6
10
15
21
28

The operator resets all registers to 0 and the Input register to the nth term required. The train engine then begins its computational journey. It needs to carry out the following steps:

  1. Compare the Count with n (the nth term requested).
  2. If the Count is equal to n, then Halt
    - we're done, the sum will be showing in the Sum Register ...else...
  3. Increment the Count register (ie increase by 1).
  4. Add the Count to the Sum register, producing the next triangular number.
  5. Loop back to the first step.

Note that we need to compare first in case we are asked for the 0th term (or more likely forgot to set the input register). Also, all overflow errors from the Count Up or Adder are returned to the station. If C is greater than n then the train is returned as an error.

The 5 step program loop uses 3 different functions:

  1. Comparator function, to halt if C = n
  2. Count Up function, to increment C
  3. Add function, to add C to S
Train track Layout to Calculate the Sum of the First n Numbers
triangular number series calculator
Click grass to pause/run train Click points to switch 0/1 Click start to reset train/points
lazy point Lazy points switch between upper 0 or lower 1 branch lines.
Trains arriving on a branch line switch the point to that line.
sprung point Sprung points allow branch line trains to join the main line.
All main line trains go straight ahead and never 'branch off'.

How it works

The train will traverse the layout without intervention. Each return to the main program loop will yield the next triangular number in the lower sum Register S.

Eventually, when the Count equals the requested nth term, the comparator will return the train to the 'halt' siding. The nth triangular number can be read in the lower Sum S register.

Note

This layout will produce an overflow error for the 6th and 7th terms. Increasing the register size by adding more stages will allow larger numbers to be computed. Each register can be extended indefinitely to the left. An S register with 8-stages would allow the 255th triangular number of 32,640 to be computed.

Turing Trains | cr31.co.uk | 2017

 

use your browsers 'zoom' option to enlarge layouts