Jump to content

Multiplication tables: Difference between revisions

(added factor implementation)
(→‎{{header|Ruby}}: Added REBOL example.)
Line 683:
Declaring a file type of UTF-8 and adding a u to all string literals to transform them into Unicode literals would make the above work in Python 2.X.
<small>(As would using ASCII minus, plus, and pipe characters: "-", "+", "|"; instead of the non-ASCII chars used to draw a frame)</small>.
 
=={{header|REBOL}}==
<lang REBOL>REBOL [
Title: "12x12 Multiplication Table"
Author: oofoe
Date: 2009-12-26
URL: http://rosettacode.org/wiki/Print_a_Multiplication_Table
]
 
size: 12
 
; Because of REBOL's GUI focus, there's nothing really to do pictured
; output, so I roll my own. See Formatted_Numeric_Output for more
; comprehensive version:
 
pad: func [pad n][
n: to-string n
insert/dup n " " (pad - length? n)
n
]
p3: func [v][pad 3 v] ; A shortcut, I hate to type...
 
--: does [repeat x size + 1 [prin "+---"] print "+"] ; Special chars OK.
 
.row: func [label y /local row][
row: reduce ["|" label "|"]
repeat x size [append row reduce [either x < y [" "][p3 x * y] "|"]]
print rejoin row
]
 
-- .row " x " 1 -- repeat y size [.row p3 y y] --
 
print [ crlf "What about" size: 5 "?" crlf ]
-- .row " x " 1 -- repeat y size [.row p3 y y] --
 
print [ crlf "How about" size: 20 "?" crlf ]
-- .row " x " 1 -- repeat y size [.row p3 y y] --</lang>
 
Output (only 12x12 shown):
 
<pre>+---+---+---+---+---+---+---+---+---+---+---+---+---+
| x | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 1| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
| 2| | 4| 6| 8| 10| 12| 14| 16| 18| 20| 22| 24|
| 3| | | 9| 12| 15| 18| 21| 24| 27| 30| 33| 36|
| 4| | | | 16| 20| 24| 28| 32| 36| 40| 44| 48|
| 5| | | | | 25| 30| 35| 40| 45| 50| 55| 60|
| 6| | | | | | 36| 42| 48| 54| 60| 66| 72|
| 7| | | | | | | 49| 56| 63| 70| 77| 84|
| 8| | | | | | | | 64| 72| 80| 88| 96|
| 9| | | | | | | | | 81| 90| 99|108|
| 10| | | | | | | | | |100|110|120|
| 11| | | | | | | | | | |121|132|
| 12| | | | | | | | | | | |144|
+---+---+---+---+---+---+---+---+---+---+---+---+---+</pre>
 
=={{header|Ruby}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.