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:24

Oracle SQL, 103 characters:

select n, n*2, n*3, n*4, n*5, n*6, n*7, n*8, n*9 from (select rownum n from dual CONNECT BY LEVEL < 10)
查看更多
Bombasti
3楼-- · 2019-03-14 16:26

C - 66 Chars

This resolves the complaint about the second parameter of main :)

main(x){for(x=8;x++<89;)printf("%.2d%c",x/9*(x%9+1),x%9<8?32:10);}

C - 77 chars

Based on dreamlax's 97 char answer. His current answer somewhat resembles this one now :)

Compiles ok with gcc, and main(x,y) is fair game for golf i reckon

#define f(i){for(i=0;i++<9;)
main(x,y)f(x)f(y)printf("%.2d ",x*y);puts("");}}
查看更多
走好不送
4楼-- · 2019-03-14 16:26

Java - 155 137 chars


  • Update 1: replaced string building by direct printing. Saved 18 chars.

class M{public static void main(String[]a){for(int x,y=0,z=10;++y<z;System.out.println())for(x=0;++x<z;System.out.printf("%02d ",x*y));}}

More readable format:

class M{
 public static void main(String[]a){
  for(int x,y=0,z=10;++y<z;System.out.println())
   for(x=0;++x<z;System.out.printf("%02d ",x*y));
 }
}
查看更多
爷、活的狠高调
5楼-- · 2019-03-14 16:27

J - 8 chars - 24 chars for proper format

*/~1+i.9

Gives:

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

This solution found by @earl:

'r(0)q( )3.'8!:2*/~1+i.9

Gives:

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 
查看更多
ら.Afraid
6楼-- · 2019-03-14 16:28

Not really a one-liner, but the shortest linq i can think of:

var r = Enumerable.Range(1, 9);
foreach (var z in r.Select(n => r.Select(m => n * m)).Select(a => a.Select(b => b.ToString("00 "))))
{
    foreach (var q in z)
        Console.Write(q);
    Console.WriteLine();
}

In response to combining this and SRuly's answer

Enumberable.Range(1,9).ToList.ForEach(n => Enumberable.Range(1,9).ToList.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });

查看更多
狗以群分
7楼-- · 2019-03-14 16:28

PostgreSQL: 81 74 chars

select array(select generate_series(1,9)*x)from generate_series(1,9)as x;
查看更多
登录 后发表回答