]> gitweb.factorcode.org Git - factor.git/blob - extra/rosetta-code/multiplication-tables/multiplication-tables.factor
2384dbaedd87fd63df2f3b2fb6b157d62267d00c
[factor.git] / extra / rosetta-code / multiplication-tables / multiplication-tables.factor
1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io kernel math math.parser ranges sequences ;
4 IN: rosetta-code.multiplication-tables
5
6 ! http://rosettacode.org/wiki/Multiplication_tables
7
8 ! Produce a formatted 12×12 multiplication table of the kind
9 ! memorised by rote when in primary school.
10
11 ! Only print the top half triangle of products.
12
13 : print-row ( n -- )
14     [ number>string 2 CHAR: space pad-head write " |" write ]
15     [ 1 - [ "    " write ] times ]
16     [
17         dup 12 [a..b]
18         [ * number>string 4 CHAR: space pad-head write ] with each
19     ] tri nl ;
20
21 : print-table ( -- )
22     "    " write
23     1 12 [a..b] [ number>string 4 CHAR: space pad-head write ] each nl
24     "   +" write
25     12 [ "----" write ] times nl
26     1 12 [a..b] [ print-row ] each ;