factor/extra/rosetta-code/multiplication-tables/multiplication-tables.factor

27 lines
817 B
Factor
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

! Copyright (c) 2012 Anonymous
! See http://factorcode.org/license.txt for BSD license.
USING: io kernel math math.parser math.ranges sequences ;
IN: rosetta-code.multiplication-tables
! http://rosettacode.org/wiki/Multiplication_tables
! Produce a formatted 12×12 multiplication table of the kind
! memorised by rote when in primary school.
! Only print the top half triangle of products.
: print-row ( n -- )
[ number>string 2 CHAR: space pad-head write " |" write ]
[ 1 - [ " " write ] times ]
[
dup 12 [a,b]
[ * number>string 4 CHAR: space pad-head write ] with each
] tri nl ;
: print-table ( -- )
" " write
1 12 [a,b] [ number>string 4 CHAR: space pad-head write ] each nl
" +" write
12 [ "----" write ] times nl
1 12 [a,b] [ print-row ] each ;