September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,14 @@
scope
# print a school style multiplication table
# NB: print outputs a newline at the end, write and printf do not
write( " " );
for i to 12 do printf( " %3d", i ) od;
printf( "\n +" );
for i to 12 do write( "----" ) od;
for i to 12 do
printf( "\n%3d|", i );
for j to i - 1 do write( " " ) od;
for j from i to 12 do printf( " %3d", i * j ) od;
od;
print()
epocs

View file

@ -1,89 +1,113 @@
-- MULTIPLICATION TABLE FOR INTEGERS M TO N
tableText(multTable(1, 12))
-- table :: Int -> Int -> [[String]]
on table(m, n)
-- multTable :: Int -> [[String]]
on multTable(m, n)
set axis to range(m, n)
set axis to enumFromTo(m, n)
script column
on lambda(x)
on |λ|(x)
script row
on lambda(y)
on |λ|(y)
if y < x then
{""}
""
else
{(x * y) as text}
(x * y) as string
end if
end lambda
end |λ|
end script
{x & map(row, axis)}
end lambda
end |λ|
end script
{{"x"} & axis} & concatMap(column, axis)
end table
end multTable
-- TABLE DISPLAY --------------------------------------------------------------
on run
tableText(table(1, 12))
end run
-- TABLE DISPLAY
-- tableText :: [[String]] -> String
-- tableText :: [[Int]] -> String
on tableText(lstTable)
script tableLine
on lambda(lstLine)
on |λ|(lstLine)
script tableCell
on lambda(cell)
(characters -4 thru -1 of (" " & cell)) as string
end lambda
on |λ|(int)
(characters -4 thru -1 of (" " & int)) as string
end |λ|
end script
intercalate(" ", map(tableCell, lstLine))
end lambda
end |λ|
end script
intercalate(linefeed, map(tableLine, lstTable))
end tableText
-- GENERIC LIBRARY FUNCTIONS
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
script append
on lambda(a, b)
a & b
end lambda
end script
foldl(append, {}, map(f, xs))
set lst to {}
set lng to length of xs
tell mReturn(f)
repeat with i from 1 to lng
set lst to (lst & |λ|(item i of xs, i, xs))
end repeat
end tell
return lst
end concatMap
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- justifyRight :: Int -> Char -> Text -> Text
on justifyRight(n, cFiller, strText)
if n > length of strText then
text -n thru -1 of ((replicate(n, cFiller) as text) & strText)
else
strText
end if
end justifyRight
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
@ -96,29 +120,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end range
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate

View file

@ -1,13 +0,0 @@
int main()
{
int i, j, n = 12;
for (j = 1; j <= n; j++) printf("%3d%c", j, j - n ? ' ':'\n');
for (j = 0; j <= n; j++) printf(j - n ? "----" : "+\n");
for (i = 1; i <= n; printf("| %d\n", i++))
for (j = 1; j <= n; j++)
printf(j < i ? " " : "%3d ", i * j);
return 0;
}

View file

@ -1,14 +0,0 @@
1 2 3 4 5 6 7 8 9 10 11 12
------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 | 1
4 6 8 10 12 14 16 18 20 22 24 | 2
9 12 15 18 21 24 27 30 33 36 | 3
16 20 24 28 32 36 40 44 48 | 4
25 30 35 40 45 50 55 60 | 5
36 42 48 54 60 66 72 | 6
49 56 63 70 77 84 | 7
64 72 80 88 96 | 8
81 90 99 108 | 9
100 110 120 | 10
121 132 | 11
144 | 12

View file

@ -1,31 +0,0 @@
print_multiplication_tables = (n) ->
width = 4
pad = (s, n=width, c=' ') ->
s = s.toString()
result = ''
padding = n - s.length
while result.length < padding
result += c
result + s
s = pad('') + '|'
for i in [1..n]
s += pad i
console.log s
s = pad('', width, '-') + '+'
for i in [1..n]
s += pad '', width, '-'
console.log s
for i in [1..n]
s = pad i
s += '|'
s += pad '', width*(i - 1)
for j in [i..n]
s += pad i*j
console.log s
print_multiplication_tables 12

View file

@ -1,15 +0,0 @@
> coffee multiply.coffee
| 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

View file

@ -0,0 +1,23 @@
'Code 'stolen' from Free Basic and altered to work in Gambas
Public Sub Main()
Dim i, j As Integer
Print " X|";
For i = 1 To 12
Print Format(i, "####");
Next
Print
Print "---+"; String(48, "-")
For i = 1 To 12
Print Format(i, "###");
Print "|"; Space(4 * (i - 1));
For j = i To 12
Print Format(i * j, "####");
Next
Print
Next
End

View file

@ -1,8 +1,26 @@
import Control.Monad
import Text.Printf
import Data.Monoid ((<>))
import Data.List (intercalate, transpose)
main = do
putStrLn $ " x" ++ concatMap fmt [1..12]
zipWithM_ f [1..12] $ iterate (" " ++) ""
where f n s = putStrLn $ fmt n ++ s ++ concatMap (fmt . (*n)) [n..12]
fmt n = printf "%4d" (n :: Int)
multTable :: Int -> [[String]]
multTable n =
let xs = [1 .. n]
in xs >>=
\x ->
[ show x <> ":" :
(xs >>=
\y ->
[ if y < x
then mempty
else show (x * y)
])
]
table :: String -> [[String]] -> [String]
table delim rows =
let justifyRight c n s = drop (length s) (replicate n c <> s)
in intercalate delim <$>
transpose
((fmap =<< justifyRight ' ' . maximum . fmap length) <$> transpose rows)
main :: IO ()
main = (putStrLn . unlines . table " " . multTable) 12

View file

@ -0,0 +1,12 @@
// version 1.0.6
fun main(args: Array<String>) {
print(" x|")
for (i in 1..12) print("%4d".format(i))
println("\n---+${"-".repeat(48)}")
for (i in 1..12) {
print("%3d".format(i) +"|${" ".repeat(4 * i - 4)}")
for (j in i..12) print("%4d".format(i * j))
println()
}
}

View file

@ -1,17 +0,0 @@
timesTable(12)
ans =
0 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 0 4 6 8 10 12 14 16 18 20 22 24
3 0 0 9 12 15 18 21 24 27 30 33 36
4 0 0 0 16 20 24 28 32 36 40 44 48
5 0 0 0 0 25 30 35 40 45 50 55 60
6 0 0 0 0 0 36 42 48 54 60 66 72
7 0 0 0 0 0 0 49 56 63 70 77 84
8 0 0 0 0 0 0 0 64 72 80 88 96
9 0 0 0 0 0 0 0 0 81 90 99 108
10 0 0 0 0 0 0 0 0 0 100 110 120
11 0 0 0 0 0 0 0 0 0 0 121 132
12 0 0 0 0 0 0 0 0 0 0 0 144

View file

@ -2,10 +2,10 @@ my $max = 12;
my $width = chars $max**2;
my $f = "%{$width}s";
say 'x'.fmt($f), ' ', (1..$max).fmt($f);
say '' x $width, '', '' x $max*$width + $max;
say 'x'.fmt($f), ' ', (1..$max).fmt($f);
say '' x $width, '', '' x $max*$width + $max;
for 1..$max -> $i {
say $i.fmt($f), ' ', (
say $i.fmt($f), ' ', (
for 1..$max -> $j {
$i <= $j ?? $i*$j !! '';
}

View file

@ -1 +0,0 @@
upper.tri(1:12 %o% 1:12, diag = TRUE) * 1:12 %o% 1:12

View file

@ -1,10 +0,0 @@
multiplication_table <- function(n=12)
{
one_to_n <- 1:n
x <- matrix(one_to_n) %*% t(one_to_n)
x[lower.tri(x)] <- 0
rownames(x) <- colnames(x) <- one_to_n
print(as.table(x), zero.print="")
invisible(x)
}
multiplication_table()

View file

@ -1,45 +1,39 @@
/*REXX program displays a NxN multiplication table (in a boxed grid) to the terminal.*/
parse arg high . /*obtain optional grid size from the CL*/
if high=='' | high=="," then high=12 /*Not specified? Then use the default.*/
bar = '' ; dash = "" /*(vertical) bar; horizontal bar (dash)*/
bj = '' ; tj = "" /*bottom and top junctions (or tees).*/
cj = '' /*center junction (or cross). */
lj = '' ; rj = "" /*left and right junctions (or tees).*/
tlc = '' ; trc = "" /* top left and right corners. */
blc = '' ; brc = "" /*bottom " " " " */
/* [↑] define stuff to hold box glyphs*/
bar = '' ; dash = "" /*(vertical) bar; horizontal bar (dash)*/
bj = '' ; tj = "" /*bottom and top junctions (or tees).*/
cj = '' /*center junction (or cross). */
lj = '' ; rj = "" /*left and right junctions (or tees).*/
tlc = '' ; trc = "" /* top left and right corners. */
blc = '' ; brc = "" /*bottom " " " " */
cell = cj || copies(dash,max(5,length(high) +1)) /*define the top of the cell. */
sep = copies(cell, high+1)rj /*construct the table separator. */
size = length(cell) - 1 /*width for the products in the table. */
box. = left('', size) /*initialize all the cells in the table*/
do j=0 to high /*step through zero ───► high. */
_=right(j, size - 2)'x ' /*build the "label" (border) number. */
box.0.j=center(_, size) /* " " top label cell. */
box.j.0=center(_, max(5, size) ) /* " " left label cell. */
end /*j*/
box.0.0=center('times', max(5, size)) /*redefine box.0.0 with "times". */
do r=1 for high /*step through row one ───► high. */
do c=r to high /*step through column row ───► high. */
box.r.c=right(r*c, size) /*build a single multiplication cell. */
end /*c*/
end /*r*/ /*only build the top right-half of grid*/
do j=0 to high /*step through zero ───► high. */
_=right(j, size - 2)'x ' /*build the "label" (border) number. */
box.0.j=center(_, size) /* " " top label cell. */
box.j.0=center(_, max(5, size) ) /* " " left label cell. */
end /*j*/
box.0.0=center('times', max(5, size)) /*redefine box.0.0 with "times". */
do r=1 for high /*step through row one ───► high. */
do c=r to high /*step through column row ───► high. */
box.r.c=right(r*c, size) /*build a single multiplication cell. */
end /*c*/
end /*r*/ /*only build the top right-half of grid*/
do r=0 to high; @=sep /*step through all lines; use a mod sep*/
if r==0 then do
@=overlay(tlc, @ , 1) /*use a better tlc (top left corner). */
@=overlay(trc, @ , length(sep)) /* " " " trc ( " right " ). */
@=translate(@, tj, cj) /* " " " tj (top junction/tee).*/
end
else @=overlay(lj, @, 1) /* " " " lj (left junction/tee).*/
say @ /*display a single table grid line. */
if r==0 then call buildLine 00 /* " " " blank grid " */
call buildLine r /*build a single line of the grid. */
if r==0 then call buildLine 00 /*display a single blank grid line. */
end /*r*/
do r=0 to high; @=sep; L=length(sep) /*step through all lines; use a mod sep*/
if r==0 then do; @=overlay(tlc, @ , 1) /*use a better tlc (top left corner). */
@=overlay(trc, @ , L) /* " " " trc ( " right " ). */
@=translate(@, tj,cj) /* " " " tj (top junction/tee).*/
end
else @=overlay(lj, @, 1) /* " " " lj (left junction/tee).*/
say @ /*display a single table grid line. */
if r==0 then call buildLine 00 /* " " " blank grid " */
call buildLine r /*build a single line of the grid. */
if r==0 then call buildLine 00 /*display a single blank grid line. */
end /*r*/
@=sep /*allow use of a modified separator. */
@=overlay(blc, @ , 1) /*use a better bottom left corner. */
@=overlay(brc, @ , length(sep) ) /* " " " " right corner. */
@ -47,9 +41,7 @@ box.0.0=center('times', max(5, size)) /*redefine box.0.0 with "
say @ /*display a (single) table grid line. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
buildLine: parse arg row,,$ /*start with a blank cell ($). */
do col=0 to high /*step through zero ───► high. */
$=$ || bar || box.row.col /*build one cell at a time. */
end /*col*/
say $ || bar /*finish building the last cell.*/
return
buildLine: parse arg row,,$; do col=0 to high /*step through zero ───► high. */
$=$ ||bar ||box.row.col /*build one cell at a time. */
end /*col*/ /* [↑] build (row) line by cols*/
say $ || bar; return /*finish building the last cell.*/

View file

@ -0,0 +1,48 @@
//Multiplication Table
object mulTable{
def main( args:Array[String] ){
print(" |") //Horizontal row
for( i <- 1 to 12 )
if( i<10 )
print( " " + i )
else
print(" " + i )
println("")
println("---------------------------------------------------------------------")
for( i <- 1 to 12 ){
if( i<10 ) //Vertical column
print(" " + i + "|" )
else
print(" " + i + "|" )
for( j <- 1 to 12 ){
if( i*j < 10 ){
if( i<=j )
print(" " + i*j )
else
print(" ")
}
else if( i*j < 100 ){
if( i<=j )
print(" " + i*j )
else
print(" ")
}
else{
if( i<=j )
print(" " + i*j )
else
print(" ")
}
}
println("")
}
}
}

View file

@ -1,13 +1,13 @@
var max = 12;
var width = (max**2 -> len+1);
var max = 12
var width = (max**2 -> len+1)
 
func fmt_row(*items) {
items.map { |s| "%*s" % (width, s) }.join('');
items.map {|s| "%*s" % (width, s) }.join
}
 
say fmt_row('x┃', (1..max)...)
say "#{'━' * (width - 1)}╋#{'━' * (max * width)}"
say fmt_row('x┃', (1..max)...);
say "#{'━' * (width - 1)}╋#{'━' * (max * width)}";
max.times { |i|
say fmt_row("#{i}┃", (1..max).map {|j| i <= j ? i*j : ''}...);
};
{ |i| 
say fmt_row("#{i}┃", {|j| i <= j ? i*j : ''}.map(1..max)...)
} << 1..max

View file

@ -0,0 +1,10 @@
fcn multiplicationTable(n){
w,fmt := (n*n).numDigits, " %%%dd".fmt(w).fmt; // eg " %3".fmt
header:=[1..n].apply(fmt).concat(); // 1 2 3 4 ...
println(" x ", header, "\n ", "-"*header.len());
dash:=String(" "*w,"-"); // eg " -"
foreach a in ([1..n]){
print("%2d|".fmt(a),dash*(a-1));
[a..n].pump(String,'*(a),fmt).println();
}
}(12);