Pseudo Code

Pseudo code is a sort of cross between English and computer code.
It allows for easy transfer of the algorithm to a compter language.

Zeller's algorithm would be written in pseudo code like this:

  1. Let D be Day number.
    Let M be Month number.
    Let Y be Year number.
  2. If M < 3 then M := M + 12 and Y := Y - 1
  3. Let A = INT (Y / 100)
    Let B = Y - (100 x A)
  4. Let S = INT (2.6 x M - 5.39) + INT (B / 4) + INT (A / 4) + D + B - (2 x A)
  5. Let Day = S - (7 x INT (S / 7))

This algorithm can be used to find the real roots of the quadratic equation

ax2 + bx + c = 0

using the quadratic formula.

  1. Let d = b2 - 4ac
  2. If d < 0 print 'no real solutions' and go to step 5
  3. Let X1 = (-b + SQRT(d))/(2a)
  4. Let X2 = (-b - SQRT(d))/(2a)
  5. Print X1 and X2
  6. Stop

Here is the Russian peasants' algorithm for long multiplication

Algorithm

  1. Write the two numbers to be multiplied next to each other.
  2. Double the left number and write the result underneath the left number.
    Halve the right number, ignore any remainder and write the result underneath the right number.
  3. Repeat step 2 until the right number is 1.
  4. Delete those rows where the number in the right hand column is even.
    Add the remaining numbers in the left column.
    This is the result of multiplying the two original numbers.

Write the Russian peasants' algorithm in pseudo code.

Click here for one possible answer.

Example

To find 34 x 51

34        51
68        25
136       12
272        6
544        3
1088      1
Crossing out the rows where the right hand number is even would leave:
34         51
68         25
-----------
-----------
544         3
1088        1
Adding the remaining numbers in the left hand column gives 1734 which is the answer to 34 x 51