Beginners 10x10 multiplication matrix

0

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

Comann Silvan

Posted 2018-05-28T08:05:15.000

Reputation: 119

Question was closed 2018-05-28T12:33:33.680

Can you provide an example of the desired output ? Or are you asking us to print the 10*10 matrix ? – The random guy – 2018-05-28T08:07:23.707

yes, just the 10x10 matrix – Comann Silvan – 2018-05-28T08:19:49.593

2

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.

– manatwork – 2018-05-28T08:23:58.583

1Nice 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. – Adám – 2018-05-28T08:28:17.583

1If 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.)? – Adám – 2018-05-28T08:29:39.653

5Please give one or more complete correct outputs. One might think that the output actually needs all the dots. – Adám – 2018-05-28T08:30:23.317

1There's some grammatical problem in the sentence The goal is to produce a simple table that looks like this common java/c# code is about 4 lines. – Adám – 2018-05-28T08:30:55.290

Although not exactly duplicate, I think that this challenge is too similar to be interesting. It will only be closed if there are 5 people (or a gold badge holder) agree with that and nobody vote to reopen. – user202729 – 2018-05-28T10:11:28.417

3I don't think anybody has mentioned this yet, but welcome to PPCG! – Giuseppe – 2018-05-28T11:31:39.557

My answer before it was closed – sergiol – 2018-05-28T13:00:07.393

@sergiol Annoying when a question gets closed just when you wanted to post an answer, isn't it? ;) My answer in Java 10 and my answer in 05AB1E. Both probably golfable (second one especially, I'm pretty new to 05AB1E).

– Kevin Cruijssen – 2018-05-28T13:46:48.083

Strange, it looks like the system somehow allowed me to post an answer when it was already closed! Nevertheless, I think it is not very nice to close a challenge, which is not technically an exact duplicate, when it has already accumulated a dozen of answers! – Kirill L. – 2018-05-28T14:15:56.597

@KevinCruijssen: The maximum you can do about it is voting to reopen! – sergiol – 2018-05-28T14:17:06.643

@KirillL. Can I suggest you voting to reopen? – sergiol – 2018-05-28T14:17:53.607

@sergiol I already did... – Kirill L. – 2018-05-28T14:19:30.167

I would vote to reopen but I have a gold badge, and I think this should be closed as unclear rather than a dupe. – Giuseppe – 2018-05-28T17:07:35.897

@Giuseppe: For me the output sample made very clear what the question's poster want people to answer. – sergiol – 2018-05-29T09:22:47.207

Answers

2

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

Stewie Griffin

Posted 2018-05-28T08:05:15.000

Reputation: 43 471

2

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.

G B

Posted 2018-05-28T08:05:15.000

Reputation: 11 099

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. – manatwork – 2018-05-28T10:40:56.360

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

– Kirill L. – 2018-05-28T13:11:57.833

1

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.

Mr. Xcoder

Posted 2018-05-28T08:05:15.000

Reputation: 39 774

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? – Comann Silvan – 2018-05-28T08:39:03.497

5

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.

– Mr. Xcoder – 2018-05-28T08:41:00.417

1

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)

Luis Mendo

Posted 2018-05-28T08:05:15.000

Reputation: 87 464

You could simply do x=1:10;disp(x'*x). Same amount of bytes, and more readable. – maxb – 2018-05-28T10:56:47.563

@maxb Good idea; updated – Luis Mendo – 2018-05-28T10:59:11.087

2... and more readable. That's almost a reason to not update the answer! – Stewie Griffin – 2018-05-28T21:47:49.563

1

R, 32 bytes

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

Try it online!

Giuseppe

Posted 2018-05-28T08:05:15.000

Reputation: 21 077

write(format(1:10%o%1:10),1,10) is shorter by one byte. – JayCe – 2018-05-28T15:45:51.407

1

Python 2, 64 55 bytes

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

Try it online!

Kirill L.

Posted 2018-05-28T08:05:15.000

Reputation: 6 693

0

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.
++++.>..<++++.>..<-------.+.>.<-.+++++.>.<----.--.>.<++.++.>.<--.++++++.>.<-----.-.>.<+.+++.>.<--.----.>>.<<
+++++.>..<----.-.>.<+.++++.>.<---.--.>.<++.+++.>.<--.---.>.<+++.++.>.<-.----.>.<++++.+.>.<.-----.>>.<<
++++++.>..<-----.+.>.<-.+++++++.>.<------.++.>.<-.---.>.<+++.+++.>.<--.--.>.<++.++++.>.<---.-.>.<++.------.>>.<<
+++++++.>..<------.+++.>.<--.-.>.<+.++++++.>.<-----.++.>.<-.--.>.<++.+++++.>.<----.+.>.<.---.>.<++++.-------.>>.<<
++++++++.>..<-------.+++++.>.<----.++.>.<-.-.>.<++.----.>.<++++.++++.>.<---.+.>.<.--.>.<+++.-----.>.<++++++.--------.>>.<<
+++++++++.>..<--------.+++++++.>.<------.+++++.>.<----.+++.>.<--.+.>.<.-.>.<++.---.>.<++++.-----.>.<++++++.-------.>.<++++++++.---------.>>.<<
+.-.>.<++.--.>.<+++.---.>.<++++.----.>.<+++++.-----.>.<++++++.------.>.<+++++++.-------.>.<++++++++.--------.>.<+++++++++.---------.>.<+.-..

The random guy

Posted 2018-05-28T08:05:15.000

Reputation: 1 262

1147 bytes. You were printing null bytes on the second line – Jo King – 2018-05-28T09:12:09.650

3There's got to be an algorithmic way of printing the numbers instead of kolmogorov'ing it – Jo King – 2018-05-28T09:13:31.047

0

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

Graham

Posted 2018-05-28T08:05:15.000

Reputation: 3 184

1

on Dyalog you can get 7 bytes

– dzaima – 2018-05-28T09:53:43.267

@dzaima Unfortunately I am running an ancient version of APL+WIN which does not have the ⍨ operator ;( – Graham – 2018-05-28T10:15:36.803

0

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%

Neil

Posted 2018-05-28T08:05:15.000

Reputation: 95 035

0

J, 9 bytes

*/~1+i.10

Try it online!

Galen Ivanov

Posted 2018-05-28T08:05:15.000

Reputation: 13 815

0

Red, 56 bytes

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

Try it online!

Galen Ivanov

Posted 2018-05-28T08:05:15.000

Reputation: 13 815

0

CJam, 24 23 bytes

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

Try it online!

Alternative without pretty printing:

A,:)_m*{:*}%A/:p

maxb

Posted 2018-05-28T08:05:15.000

Reputation: 5 754

0

JavaScript (Node.js), 79 bytes

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

Try it online!

Luis felipe De jesus Munoz

Posted 2018-05-28T08:05:15.000

Reputation: 9 639