Lattice multiplication computes the product of two numbers digit by digit on a grid. If the first number A has m digits and the second number B has n digits, the lattice has n rows and m columns, and a frame is drawn around it.
Multiplying 345 by 56 looks like this.
+---------------+
| 3 4 5 |
| +---+---+---+ |
| |1 /|2 /|2 /| |
| | / | / | / |5|
|1|/ 5|/ 0|/ 5| |
| +---+---+---+ |
|/|1 /|2 /|3 /| |
| | / | / | / |6|
|9|/ 8|/ 4|/ 0| |
| +---+---+---+ |
|/ 3 / 2 / 0 |
+---------------+
The first number, 345, sits above the lattice with each digit centered over its column. The second number, 56, sits to the right of the lattice with each digit centered beside its row.
A single cell of the lattice looks like this.
+---+
|3 /|
| / |
|/ 0|
+---+
A cell holds the product of the digit above its column and the digit to the right of its row. In the picture above, that cell is 5×6=30, the product of the 5 in 345 and the 6 in 56. The tens digit of the product goes in the upper left of the cell and the ones digit in the lower right. A one digit product is written with a 0 in the tens place.
The whole product comes from adding the diagonals that carry the same place value. For 345×56=19320 the sums are:
| ones | = 0 |
| tens | = 5 + 3 + 4 = 12, so 2 with a carry of 1 |
| hundreds | = (carry 1) + 2 + 0 + 2 + 8 = 13, so 3 with a carry of 1 |
| thousands | = (carry 1) + 2 + 5 + 1 = 9 |
| ten thousands | = 1 |
The ones digit of the product goes at the far right of the row below the lattice, and the remaining digits continue to the left. When the product does not fit in that row, the leading digits turn the bottom left corner and climb the column to the left of the lattice. Every digit of the product sits exactly on the extension of the diagonal it was summed from.
Horizontal lines are minus characters (-), vertical lines are pipe characters (|), and diagonal lines are slash characters (/). A plus character (+) goes wherever a horizontal line meets a vertical line. Inside the frame, from top to bottom, come the row holding the first number, the lattice, and the row holding the lower part of the product. There is a column of width 1 to the left of the lattice, which may hold the upper part of the product, and a column of width 1 to the right of the lattice, which holds the second number. If the product is short enough that it never turns the bottom left corner, the left column holds only spaces.
Leading zeros are written inside lattice cells, but the product itself never has a leading zero, and no slash is drawn in front of the first digit of the product.
The input holds one or more tests. Each test is a line with two integers A and B. (1≤A≤9999, 1≤B≤9999)
The line after the last test is 0 0.
For each test, print the picture that shows how the two numbers are multiplied with the lattice method. Print nothing between one picture and the next.
Every character, spaces included, must match the rules and the pictures above.