Code-golf: Output multiplication table to the Cons

2019-03-14 15:35发布

I recently pointed a student doing work experience to an article about dumping a multiplication table to the console. It used a nested for loop and multiplied the step value of each.

This looked like a .NET 2.0 approach. I was wondering, with the use of Linq and extension methods,for example, how many lines of code it would take to achieve the same result.

Is the stackoverflow community up to the challenge?

The challenge: In a console application, write code to generate a table like this example:

01 02 03 04 05 06 07 08 09
02 04 06 08 10 12 14 16 18
03 06 09 12 15 18 21 24 27
04 08 12 16 20 24 28 32 36
05 10 15 20 25 30 35 40 45
06 12 18 24 30 36 42 48 54
07 14 21 28 35 42 49 56 63
08 16 24 32 40 48 56 64 72
09 18 27 36 45 54 63 72 81

As this turned into a language-agnostic code-golf battle, I'll go with the communities decision about which is the best solution for the accepted answer.

There's been alot of talk about the spec and the format that the table should be in, I purposefully added the 00 format but the double new-line was originally only there because I didn't know how to format the text when creating the post!

30条回答
祖国的老花朵
2楼-- · 2019-03-14 16:09

COBOL - 218 chars -> 216 chars

PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 I PIC 9.
1 N PIC 99.
PROCEDURE DIVISION.PERFORM 9 TIMES
ADD 1 TO I
SET N TO I
PERFORM 9 TIMES
DISPLAY N' 'NO ADVANCING
ADD I TO N
END-PERFORM
DISPLAY''
END-PERFORM.

Edit

216 chars (probably a different compiler)

PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 I PIC 9.
1 N PIC 99.
PROCEDURE DIVISION.

  PERFORM B 9 TIMES
  STOP RUN.

B.
 ADD 1 TO I
 set N to I
 PERFORM C 9 TIMES
 DISPLAY''.

C.
 DISPLAY N" "NO ADVANCING
 Add I TO N.
查看更多
forever°为你锁心
3楼-- · 2019-03-14 16:09

Haskell — 85 84 79 chars

r=[1..9]
s x=['0'|x<=9]++show x
main=mapM putStrLn[unwords[s$x*y|x<-r]|y<-r]

If double spacing is required (89 81 chars),

r=[1..9]
s x=['0'|x<=9]++show x
main=mapM putStrLn['\n':unwords[s$x*y|x<-r]|y<-r]
查看更多
Rolldiameter
4楼-- · 2019-03-14 16:10

R (very similar to Matlab on this level): 12 characters.

> 1:9%*%t(1:9)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    2    3    4    5    6    7    8    9
[2,]    2    4    6    8   10   12   14   16   18
[3,]    3    6    9   12   15   18   21   24   27
[4,]    4    8   12   16   20   24   28   32   36
[5,]    5   10   15   20   25   30   35   40   45
[6,]    6   12   18   24   30   36   42   48   54
[7,]    7   14   21   28   35   42   49   56   63
[8,]    8   16   24   32   40   48   56   64   72
[9,]    9   18   27   36   45   54   63   72   81
查看更多
够拽才男人
5楼-- · 2019-03-14 16:12

Ruby — 47 chars

puts (a=1..9).map{|i|a.map{|j|"%2d"%(j*i)}*" "}

Output

 1  2  3  4  5  6  7  8  9
 2  4  6  8 10 12 14 16 18
 3  6  9 12 15 18 21 24 27
 4  8 12 16 20 24 28 32 36
 5 10 15 20 25 30 35 40 45
 6 12 18 24 30 36 42 48 54
 7 14 21 28 35 42 49 56 63
 8 16 24 32 40 48 56 64 72
 9 18 27 36 45 54 63 72 81

(If we ignore spacing, it becomes 39: puts (a=1..9).map{|i|a.map{|j|j*i}*" "} And anyway, I feel like there's a bit of room for improvement with the wordy map stuff.)

查看更多
Emotional °昔
6楼-- · 2019-03-14 16:13

Perl, 44 chars

(No hope of coming anywhere near J, but languages with matrix ops are in a class of their own here...)

for$n(1..9){printf"%3d"x9 .$/,map$n*$_,1..9}
查看更多
姐就是有狂的资本
7楼-- · 2019-03-14 16:15

MATLAB - 10 characters

a=1:9;a'*a

... or 33 characters for stricter output format

a=1:9;disp(num2str(a'*a,'%.2d '))
查看更多
登录 后发表回答