0
\$\begingroup\$

I was looking for a very easy task to look how short of a code can be produced on a beginners exercise.

The goal is to produce a simple table that looks like this common java/c# code is about 4 lines. So get your golfing-languages ready ;) Try to align the matrix in any way it is readable.

1 2 3    .. 10

2 4 6    .. 20

3 6 9    .. 30

. . .    .. .

10 20 30 .. 100

EDIT: The first answer below shows a propper alignment and solution

\$\endgroup\$
17
  • 2
    \$\begingroup\$ Looks like a simplified version of Print this Multiplication Table and Addition/Multiplication table generator. Not much challenging. Though occasionally the extremely simple questions' popularity is much higher. Unless closed. So I suggest to specify whether input will be provided, output must be text or formatted array is fine and whether the numbers need some kind of alignment. \$\endgroup\$
    – manatwork
    May 28, 2018 at 8:23
  • 1
    \$\begingroup\$ Nice first challenge, but you would be wise to specify more clearly what exactly is required. E.g. may we return a list of ten lists? Don't use the word "Try"; either require it or say it isn't required. Etc. \$\endgroup\$
    – Adám
    May 28, 2018 at 8:28
  • 1
    \$\begingroup\$ If you do require alignment, which alignments are acceptable (left, right, all columns same width, single/multiple space separating columns, additional white space left and/or right, etc.)? \$\endgroup\$
    – Adám
    May 28, 2018 at 8:29
  • 5
    \$\begingroup\$ Please give one or more complete correct outputs. One might think that the output actually needs all the dots. \$\endgroup\$
    – Adám
    May 28, 2018 at 8:30
  • 3
    \$\begingroup\$ I don't think anybody has mentioned this yet, but welcome to PPCG! \$\endgroup\$
    – Giuseppe
    May 28, 2018 at 11:31

13 Answers 13

3
\$\begingroup\$

MATL, 5 bytes

10:&*

Try it online!

10:    % Range: [1,2,3,4,5,6,7,8,9,10]
   &*  % Multiply the range by itself transposed, with broadcasting.

Outputs:

 1   2   3   4   5   6   7   8   9  10
 2   4   6   8  10  12  14  16  18  20
 3   6   9  12  15  18  21  24  27  30
 4   8  12  16  20  24  28  32  36  40
 5  10  15  20  25  30  35  40  45  50
 6  12  18  24  30  36  42  48  54  60
 7  14  21  28  35  42  49  56  63  70
 8  16  24  32  40  48  56  64  72  80
 9  18  27  36  45  54  63  72  81  90
10  20  30  40  50  60  70  80  90 100
\$\endgroup\$
2
\$\begingroup\$

Ruby, 48 45 44 bytes

1.upto(10){|x|puts"%4d"*10%[*x.step(100,x)]}

Try it online!

-3 bytes thanks to manatwork then -1 thanks to Kirill L.

But I still think there must be a better way.

\$\endgroup\$
2
  • \$\begingroup\$ You not need the parenthesis around the "%4d"*10. And instead of x*10 you can write just 100 then the placeholders in the format string will limit it to 10. \$\endgroup\$
    – manatwork
    May 28, 2018 at 10:40
  • \$\begingroup\$ Now, when there are no parentheses, you don't need the space after puts either. As for a better way, it looks not so easy, e.g. an alternative approach gives still the same 44 bytes \$\endgroup\$
    – Kirill L.
    May 28, 2018 at 13:11
1
\$\begingroup\$

Jelly, 5 bytes

⁵×þ`G

Try it online!

Prints the following text:

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

Another 5-byter.

\$\endgroup\$
2
  • \$\begingroup\$ I'm so fascinated by those languages like Jelly, Brainfuck etc.... But I think I will never understand how to write code with these. Is there any 'tutorial' you can recommend? \$\endgroup\$ May 28, 2018 at 8:39
  • 5
    \$\begingroup\$ For Jelly, I can recommend its dedicated wiki. However, I suggest not accepting an answer this early, and in fact not accepting an answer at all because that doesn't encourage competition. \$\endgroup\$
    – Mr. Xcoder
    May 28, 2018 at 8:41
1
\$\begingroup\$

Octave, 18 17 bytes

1 byte saved thanks to @StewieGriffin

disp((x=1:10)'*x)

Try it online!

Or, as @maxb points out: more readable, same length:

x=1:10;disp(x'*x)
\$\endgroup\$
3
  • \$\begingroup\$ You could simply do x=1:10;disp(x'*x). Same amount of bytes, and more readable. \$\endgroup\$
    – maxb
    May 28, 2018 at 10:56
  • \$\begingroup\$ @maxb Good idea; updated \$\endgroup\$
    – Luis Mendo
    May 28, 2018 at 10:59
  • 2
    \$\begingroup\$ ... and more readable. That's almost a reason to not update the answer! \$\endgroup\$ May 28, 2018 at 21:47
1
\$\begingroup\$

R, 32 bytes

write(format((x=1:10)%o%x),1,10)

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ write(format(1:10%o%1:10),1,10) is shorter by one byte. \$\endgroup\$
    – JayCe
    May 28, 2018 at 15:45
1
\$\begingroup\$

Python 2, 64 55 bytes

i=1
exec"print'%4d'*10%tuple(range(i,11*i,i));i+=1;"*10

Try it online!

\$\endgroup\$
0
\$\begingroup\$

APL+WIN, 9 bytes

n∘.×n←⍳10

Try it online! Courtesy of Dyalog Classic

Outer product multiplication of a vector of integers 1 to 10

\$\endgroup\$
2
  • 1
    \$\begingroup\$ on Dyalog you can get 7 bytes \$\endgroup\$
    – dzaima
    May 28, 2018 at 9:53
  • \$\begingroup\$ @dzaima Unfortunately I am running an ancient version of APL+WIN which does not have the ⍨ operator ;( \$\endgroup\$
    – Graham
    May 28, 2018 at 10:15
0
\$\begingroup\$

Brainfuck, 1147 bytes

++[[<+++>-->----<]>]<<<<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<--------.-.<<+.>>++.>..<++.>..<++.>..<++.>..<-------.-.>.<+.+.>.<-.+++.>.<---.+++++.>.<-----.+++++++.>.<------.--.<<.>>+++.>..<+++.>..<+++.>..<--------.+.>.<-.++++.>.<----.+++++++.>.<------.-.>.<+.++.>.<--.+++++.>.<----.---.<<.>>++++.>..<++++.>..<-------.+.>.<-.+++++.>.<----.--.>.<++.++.>.<--.++++++.>.<-----.-.>.<+.+++.>.<--.----.<<.>>+++++.>..<----.-.>.<+.++++.>.<---.--.>.<++.+++.>.<--.---.>.<+++.++.>.<-.----.>.<++++.+.>.<.-----.<<.>>++++++.>..<-----.+.>.<-.+++++++.>.<------.++.>.<-.---.>.<+++.+++.>.<--.--.>.<++.++++.>.<---.-.>.<++.------.<<.>>+++++++.>..<------.+++.>.<--.-.>.<+.++++++.>.<-----.++.>.<-.--.>.<++.+++++.>.<----.+.>.<.---.>.<++++.-------.<<.>>++++++++.>..<-------.+++++.>.<----.++.>.<-.-.>.<++.----.>.<++++.++++.>.<---.+.>.<.--.>.<+++.-----.>.<++++++.--------.<<.>>+++++++++.>..<--------.+++++++.>.<------.+++++.>.<----.+++.>.<--.+.>.<.-.>.<++.---.>.<++++.-----.>.<++++++.-------.>.<++++++++.---------.<<.>>+.-.>.<++.--.>.<+++.---.>.<++++.----.>.<+++++.-----.>.<++++++.------.>.<+++++++.-------.>.<++++++++.--------.>.<+++++++++.---------.>.<+.-..

Try it online!

Thanks for Jo King for this solution !

Old answer, 1164 bytes

+++++[>+++++[>++>+<<-]>>+>++<<<<-]>>-->++<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<--------.-.>>.<<++.>..<++.>..<++.>..<++.>..<-------.-.>.<+.+.>.<-.+++.<.>---.+++++.<.>-----.+++++++.<.>------.--.>>.<<+++.>..<+++.>..<+++.>..<--------.+.>.<-.++++.>.<----.+++++++.>.<------.-.>.<+.++.>.<--.+++++.>.<----.---.>>.<<++++.>..<++++.>..<-------.+.>.<-.+++++.>.<----.--.>.<++.++.>.<--.++++++.>.<-----.-.>.<+.+++.>.<--.----.>>.<<+++++.>..<----.-.>.<+.++++.>.<---.--.>.<++.+++.>.<--.---.>.<+++.++.>.<-.----.>.<++++.+.>.<.-----.>>.<<++++++.>..<-----.+.>.<-.+++++++.>.<------.++.>.<-.---.>.<+++.+++.>.<--.--.>.<++.++++.>.<---.-.>.<++.------.>>.<<+++++++.>..<------.+++.>.<--.-.>.<+.++++++.>.<-----.++.>.<-.--.>.<++.+++++.>.<----.+.>.<.---.>.<++++.-------.>>.<<++++++++.>..<-------.+++++.>.<----.++.>.<-.-.>.<++.----.>.<++++.++++.>.<---.+.>.<.--.>.<+++.-----.>.<++++++.--------.>>.<<+++++++++.>..<--------.+++++++.>.<------.+++++.>.<----.+++.>.<--.+.>.<.-.>.<++.---.>.<++++.-----.>.<++++++.-------.>.<++++++++.---------.>>.<<+.-.>.<++.--.>.<+++.---.>.<++++.----.>.<+++++.-----.>.<++++++.------.>.<+++++++.-------.>.<++++++++.--------.>.<+++++++++.---------.>.<+.-..

Try it online!

Oh god, this ended up way longer than I expected.

Here is how i did it :

Store values 48 ('0'), 32 (space) and 10 (new line)
+++++[>+++++[>++>+<<-]>>+>++<<<<-]>>-->++<
Print 1st line
+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<+.>..<--------.-.>>.<<
Print 2nd line
++.>..<++.>..<++.>..<++.>..<-------.-.>.<+.+.>.<-.+++.<.>---.+++++.<.>-----.+++++++.<.>------.--.>>.<<
Etc.
+++.>..<+++.>..<+++.>..<--------.+.>.<-.++++.>.<----.+++++++.>.<------.-.>.<+.++.>.<--.+++++.>.<----.---.>>.<< Etc.
++++.>..<++++.>..<-------.+.>.<-.+++++.>.<----.--.>.<++.++.>.<--.++++++.>.<-----.-.>.<+.+++.>.<--.----.>>.<<
+++++.>..<----.-.>.<+.++++.>.<---.--.>.<++.+++.>.<--.---.>.<+++.++.>.<-.----.>.<++++.+.>.<.-----.>>.<<
++++++.>..<-----.+.>.<-.+++++++.>.<------.++.>.<-.---.>.<+++.+++.>.<--.--.>.<++.++++.>.<---.-.>.<++.------.>>.<<
+++++++.>..<------.+++.>.<--.-.>.<+.++++++.>.<-----.++.>.<-.--.>.<++.+++++.>.<----.+.>.<.---.>.<++++.-------.>>.<<
++++++++.>..<-------.+++++.>.<----.++.>.<-.-.>.<++.----.>.<++++.++++.>.<---.+.>.<.--.>.<+++.-----.>.<++++++.--------.>>.<<
+++++++++.>..<--------.+++++++.>.<------.+++++.>.<----.+++.>.<--.+.>.<.-.>.<++.---.>.<++++.-----.>.<++++++.-------.>.<++++++++.---------.>>.<<
+.-.>.<++.--.>.<+++.---.>.<++++.----.>.<+++++.-----.>.<++++++.------.>.<+++++++.-------.>.<++++++++.--------.>.<+++++++++.---------.>.<+.-..
\$\endgroup\$
2
  • \$\begingroup\$ 1147 bytes. You were printing null bytes on the second line \$\endgroup\$
    – Jo King
    May 28, 2018 at 9:12
  • 3
    \$\begingroup\$ There's got to be an algorithmic way of printing the numbers instead of kolmogorov'ing it \$\endgroup\$
    – Jo King
    May 28, 2018 at 9:13
0
\$\begingroup\$

Batch, 177 bytes

@echo off
for /l %%i in (1,1,10)do call:r %%i
exit/b
:r
set s=
for /l %%j in (1,1,10)do set/an=%1*%%j&call:c
echo %s%
:c
set n= %n%
if "%n:~3%"=="" goto c
set s=%s%%n%
\$\endgroup\$
0
\$\begingroup\$

J, 9 bytes

*/~1+i.10

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Red, 56 bytes

repeat n 10[repeat m 10[prin pad rejoin[n * m]4]print""]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

CJam, 24 23 bytes

A,:)_m*{:*"%4d"e%}%A/:n

Try it online!

Alternative without pretty printing:

A,:)_m*{:*}%A/:p
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 79 bytes

(_=[...".........."])=>_.map((a,i)=>Array.from({length:10},(v,k)=>(k+1)*(i+1)))

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.