2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,3 +1,6 @@
Produce a formatted 12×12 multiplication table of the kind memorised by rote when in primary school.
;Task:
Produce a formatted   12×12   multiplication table of the kind memorized by rote when in primary (or elementary) school.
Only print the top half triangle of products.
<br><br>

View file

@ -0,0 +1,124 @@
-- MULTIPLICATION TABLE FOR INTEGERS M TO N
-- table :: Int -> Int -> [[String]]
on table(m, n)
set axis to range(m, n)
script column
on lambda(x)
script row
on lambda(y)
if y < x then
{""}
else
{(x * y) as text}
end if
end lambda
end script
{x & map(row, axis)}
end lambda
end script
{{"x"} & axis} & concatMap(column, axis)
end table
on run
tableText(table(1, 12))
end run
-- TABLE DISPLAY
-- tableText :: [[String]] -> String
on tableText(lstTable)
script tableLine
on lambda(lstLine)
script tableCell
on lambda(cell)
(characters -4 thru -1 of (" " & cell)) as string
end lambda
end script
intercalate(" ", map(tableCell, lstLine))
end lambda
end script
intercalate(linefeed, map(tableLine, lstTable))
end tableText
-- GENERIC LIBRARY 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))
end concatMap
-- 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)
end repeat
return v
end tell
end foldl
-- 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)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : 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 +1,17 @@
int main()
#include <stdio.h>
int main(void)
{
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 (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 (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
printf(j < i ? " " : "%3d ", i * j);
printf("| %d\n", i);
}
return 0;
}

View file

@ -0,0 +1,46 @@
identification division.
program-id. multiplication-table.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
01 multiplication.
05 rows occurs 12 times.
10 colm occurs 12 times.
15 num pic 999.
77 cand pic 99.
77 ier pic 99.
77 ind pic z9.
77 show pic zz9.
procedure division.
sample-main.
perform varying cand from 1 by 1 until cand greater than 12
after ier from 1 by 1 until ier greater than 12
multiply cand by ier giving num(cand, ier)
end-perform
perform varying cand from 1 by 1 until cand greater than 12
move cand to ind
display "x " ind "| " with no advancing
perform varying ier from 1 by 1 until ier greater than 12
if ier greater than or equal to cand then
move num(cand, ier) to show
display show with no advancing
if ier equal to 12 then
display "|"
else
display space with no advancing
end-if
else
display " " with no advancing
end-if
end-perform
end-perform
goback.
end program multiplication-table.

View file

@ -0,0 +1,24 @@
PROGRAM TABLES
IMPLICIT NONE
C
C Produce a formatted multiplication table of the kind memorised by rote
C when in primary school. Only print the top half triangle of products.
C
C 23 Nov 15 - 0.1 - Adapted from original for VAX FORTRAN - MEJT
C
INTEGER I,J,K ! Counters.
CHARACTER*32 S ! Buffer for format specifier.
C
K=12
C
WRITE(S,1) K,K
1 FORMAT(8H(4H0 |,,I2.2,11HI4,/,4H --+,I2.2,9H(4H----)))
WRITE(6,S) (I,I = 1,K) ! Print heading.
C
DO 3 I=1,K ! Step down the lines.
WRITE(S,2) (I-1)*4+1,K ! Update format string.
2 FORMAT(12H(1H ,I2,1H|,,I2.2,5HX,I3,,I2.2,3HI4),8X) ! Format string includes an explicit carridge control character.
WRITE(6,S) I,(I*J, J = I,K) ! Use format to print row with leading blanks, unused fields are ignored.
3 CONTINUE
C
END

View file

@ -0,0 +1,35 @@
PROGRAM TABLES
C
C Produce a formatted multiplication table of the kind memorised by rote
C when in primary school. Only print the top half triangle of products.
C
C 23 Nov 15 - 0.1 - Adapted from original for VAX FORTRAN - MEJT
C 24 Nov 15 - 0.2 - FORTRAN IV version adapted from VAX FORTRAN and
C compiled using Microsoft FORTRAN-80 - MEJT
C
DIMENSION K(12)
DIMENSION A(6)
DIMENSION L(12)
C
COMMON //A
EQUIVALENCE (A(1),L(1))
C
DATA A/'(1H ',',I2,','1H|,','01X,','I3,1','2I4)'/
C
WRITE(1,1) (I,I=1,12)
1 FORMAT(4H0 |,12I4,/,4H --+12(4H----))
C
C Overlaying the format specifier with an integer array makes it possibe
C to modify the number of blank spaces. The number of blank spaces is
C stored as two consecuitive ASCII characters that overlay on the
C integer value in L(7) in the ordr low byte, high byte.
C
DO 3 I=1,12
L(7)=(48+(I*4-3)-((I*4-3)/10)*10)*256+48+((I*4-3)/10)
DO 2 J=1,12
K(J)=I*J
2 CONTINUE
WRITE(1,A)I,(K(J), J = I,12)
3 CONTINUE
C
END

View file

@ -0,0 +1,33 @@
PROGRAM TABLES
C
C Produce a formatted multiplication table of the kind memorised by rote
C when in primary school. Only print the top half triangle of products.
C
C 23 Nov 15 - 0.1 - Adapted from original for VAX FORTRAN - MEJT
C 24 Nov 15 - 0.2 - FORTRAN IV version adapted from VAX FORTRAN and
C compiled using Microsoft FORTRAN-80 - MEJT
C 25 Nov 15 - 0.3 - Microsoft FORTRAN-80 version using a BYTE array
C which makes it easier to understand what is going
C on. - MEJT
C
BYTE A
DIMENSION A(24)
DIMENSION K(12)
C
DATA A/'(','1','H',' ',',','I','2',',','1','H','|',',',
+ '0','1','X',',','I','3',',','1','1','I','4',')'/
C
C Print a heading and (try to) underline it.
C
WRITE(1,1) (I,I=1,12)
1 FORMAT(4H |,12I4,/,4H --+12(4H----))
DO 3 I=1,12
A(13)=48+((I*4-3)/10)
A(14)=48+(I*4-3)-((I*4-3)/10)*10
DO 2 J=1,12
K(J)=I*J
2 CONTINUE
WRITE(1,A)I,(K(J), J = I,12)
3 CONTINUE
C
END

View file

@ -0,0 +1,2 @@
WRITE(1,4) (A(J), J = 1,24)
4 FORMAT(1x,24A1)

View file

@ -0,0 +1,14 @@
| 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

@ -1,24 +1,20 @@
public class MulTable{
public static void main(String args[]){
int i,j;
for(i=1;i<=12;i++)
{
System.out.print("\t"+i);
}
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 12; i++)
System.out.print("\t" + i);
System.out.println("");
for(i=0;i<100;i++)
System.out.println();
for (int i = 0; i < 100; i++)
System.out.print("-");
System.out.println("");
for(i=1;i<=12;i++){
System.out.print(""+i+"|");
for(j=1;j<=12;j++){
if(j<i)
System.out.print("\t");
else
System.out.print("\t"+i*j);
System.out.println();
for (int i = 1; i <= 12; i++) {
System.out.print(i + "|");
for(int j = 1; j <= 12; j++) {
System.out.print("\t");
if (j >= i)
System.out.print("\t" + i * j);
}
System.out.println("");
System.out.println();
}
}
}

View file

@ -8,22 +8,19 @@
}
// Monadic bind (chain) for lists
function chain(xs, f) {
function mb(xs, f) {
return [].concat.apply([], xs.map(f));
}
var lstRange = range(m, n),
var rng = range(m, n),
lstTable = [['x'].concat(lstRange)].concat(
chain(lstRange, function (y) {
return [[y].concat(
chain(lstRange, function (x) {
return x < y ? [''] : [x * y]; // triangle only
})
)]
})
);
lstTable = [['x'].concat( rng )]
.concat(mb(rng, function (x) {
return [[x].concat(mb(rng, function (y) {
return y < x ? [''] : [x * y]; // triangle only
}))]}));
/* FORMATTING OUTPUT */

View file

@ -0,0 +1,18 @@
printf(" ");
for i to 12 do
printf("%-3d ", i);
end do;
printf("\n");
for i to 75 do
printf("-");
end do;
for i to 12 do
printf("\n%2d| ", i);
for j to 12 do
if j<i then
printf(" ");
else
printf("%-3d ", i * j);
end if
end do
end do

View file

@ -0,0 +1,21 @@
# For clarity
$Tab = "`t"
# Create top row
$Tab + ( 1..12 -join $Tab )
# For each row
ForEach ( $i in 1..12 )
{
$( # The number in the left column
$i
# An empty slot for the bottom triangle
@( "" ) * ( $i - 1 )
# Calculate the top triangle
$i..12 | ForEach { $i * $_ }
# Combine them all together
) -join $Tab
}

View file

@ -0,0 +1,26 @@
function Get-TimesTable ( [int]$Size )
{
# For clarity
$Tab = "`t"
# Create top row
$Tab + ( 1..$Size -join $Tab )
# For each row
ForEach ( $i in 1..$Size )
{
$( # The number in the left column
$i
# An empty slot for the bottom triangle
@( "" ) * ( $i - 1 )
# Calculate the top triangle
$i..$Size | ForEach { $i * $_ }
# Combine them all together (and send them to the out put stream, which in PowerShell implicityly returns them)
) -join $Tab
}
}
Get-TimesTable 18

View file

@ -1,60 +1,55 @@
/*REXX program displays a NxN multiplication table (in a boxed grid). */
parse arg high . /*get optional grid size from the C.L. */
if 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*/
cell = cj || copies(dash, 5) /*define the top of the cell. */
sep = copies(cell, high+1)rj /*build the table separator. */
sepL = length(sep) /*length of the separator line. */
width= length(cell)-1 /*width of the table cells. */
size = width-1 /*width for the table numbers. */
box. = left('', width) /*construct all the cells. */
/*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*/
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-1)'x ' /*build the "label" (border) number. */
box.0.j=_ /*build the top label cell. */
box.j.0=_ /*build the left label cell. */
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=centre('times', width) /*redefine box.0.0 with 'X'. */
box.0.0=center('times', max(5, size)) /*redefine box.0.0 with "times". */
do row=1 for high /*step through one ───► high. */
do col=row to high /*step through row ───► high. */
box.row.col=right(row*col, size)' ' /*build a multiplication cell. */
end /*col*/
end /*row*/
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 row=0 to high /*step through all the lines. */
asep=sep /*allow use of a modified separator. */
if row==0 then do
asep=overlay(tlc, asep, 1) /*make a better tlc. */
asep=overlay(trc, asep, sepL) /*make a better trc. */
asep=translate(asep, tj ,cj) /*make a better tj. */
end
else asep=overlay(lj, asep ,1) /*make a better lj. */
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*/
say asep /*display a table grid line. */
if row==0 then call buildLine 00 /*display a blank grid line. */
call buildLine row /*build one line of the grid. */
if row==0 then call buildLine 00 /*display a blank grid line. */
end /*row*/
asep=sep /*allow use of a modified separator. */
asep=overlay(blc, asep, 1) /*make a better bottom left corner. */
asep=overlay(brc, asep, sepL) /*make a better bottom right corner. */
asep=translate(asep, bj, cj) /*make a better bottom junction. */
say asep /*display a table grid line. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
buildLine: w=; parse arg arow /*start with a blank cell. */
do col=0 to high /*step through zero ───► high. */
w=w||bar||box.arow.col /*build one cell at a time. */
end /*col*/
say w || bar /*finish building the last cell. */
return
@=sep /*allow use of a modified separator. */
@=overlay(blc, @ , 1) /*use a better bottom left corner. */
@=overlay(brc, @ , length(sep) ) /* " " " " right corner. */
@=translate(@, bj, cj) /* " " " " junction. */
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

View file

@ -0,0 +1,10 @@
html "<TABLE border=1 ><TR bgcolor=silver align=center><TD><TD>1<TD>2<TD>3<TD>4<TD>5<TD>6<TD>7<TD>8<TD>9<TD>10<TD>11<TD>12</td></TR>"
For i = 1 To 12
html "<TR align=right><TD>";i;"</td>"
For ii = 1 To 12
html "<td width=25>"
If ii >= i Then html i * ii
html "</td>"
Next ii
next i
html "</table>"

View file

@ -0,0 +1,23 @@
const LIMIT: i32 = 12;
fn main() {
for i in 1..LIMIT+1 {
print!("{:3}{}", i, if LIMIT - i == 0 {'\n'} else {' '})
}
for i in 0..LIMIT+1 {
print!("{}", if LIMIT - i == 0 {"+\n"} else {"----"});
}
for i in 1..LIMIT+1 {
for j in 1..LIMIT+1 {
if j < i {
print!(" ")
} else {
print!("{:3} ", j * i)
}
}
println!("| {}", i);
}
}

View file

@ -0,0 +1,17 @@
begin
integer i, j;
outtext( " " );
for i := 1 step 1 until 12 do outint( i, 4 );
outimage;
outtext( " +" );
for i := 1 step 1 until 12 do outtext( "----" );
outimage;
for i := 1 step 1 until 12 do
begin
outint( i, 3 );
outtext( "|" );
for j := 1 step 1 until i - 1 do outtext( " " );
for j := i step 1 until 12 do outint( i * j, 4 );
outimage
end;
end