Given a number and either "+" or "*", return an addition or multiplication table with the dimensions of that number.

## Specifications

• The input will be formatted as dimension, operation.
• The program should not return anything if the operation input is not + or *.
• The program should not return anything if the dimension input is less than 1.

(4,*):
1 2 3 4
2 4 6 8
3 6 9 12
4 8 1216

(4,+):
1234
2456
3567
4678


The spaces between the numbers in the table should be equal to one less than the number of digits in dimension^2 for multiplication, and for addition one less than the number of digits in dimension * 2.

• If the dimension is 10, how would one display the number 100 when there are only 2 digits in 99? Dec 5, 2016 at 17:25
• I suggest you allow a somewhat flexible output format. Having to conform to a very specific format is usually boring Dec 5, 2016 at 17:41
• Also input format checking is generally considered to be a tedious task that doesn't really add anything to a code-golf challenge. The program should not return anything if the operation input is not + or *. The program should not return anything if the dimension input is less than 1. Dec 5, 2016 at 17:49
• Mistakes are natural. The question is just on hold to give time for them to be cleared up, then it can be reopened. I find it useful to post my challenge ideas in the sandbox first to get feedback and spot any mistakes I made. I recommend it for future challenges. Dec 5, 2016 at 22:37
• Your example addition table is wrong. The top-left entry should be 2, for example. Dec 6, 2016 at 7:55

# Bash + BSD Utils, 62 bytes

l=$[$1*$1-1] eval echo \$[{1..$1}$2{1..$1}]|rs -g${#l} $[$1+1]


This calculates the number of spaces as per "number of digits in (dimension^2)-1", and not as shown in the example output, as currently written.

Try it online!

This answer is very similar to this one.

Works out-of-the-box on macOS. rs may need to be installed on Linux systems:

sudo apt-get install rs


# Excel, 74 bytes

Worksheet funtion that takes input of dimension, operation from A1, B1 and output a square array starting at the calling cell. Error handling accounts for a large portion of this response, and it is likely that large optimizations may be made to it.

=IfError(Let(r,Sequence(A1),c,Transpose(r),Ifs(B1="*",r*c,B1="+",r+c)),"")


# 05AB1E, 22 bytes

FN>¹L².VvyDg¹ngs-ú}J,}


Try it online!

Also works with -, ^, m and many more. See here for a list of commands this could work with by searching for "pop a,b".

Output (4,+):

 2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8


Output (4,*):

 1 2 3 4
2 4 6 8
3 6 912
4 81216


Output (4,^) [XOR]:

 0 3 2 5
3 0 1 6
2 1 0 7
5 6 7 0


Output (4,c) [nCr]:

 1 2 3 4
0 1 3 6
0 0 1 4
0 0 0 1


Output (4,Q) [a == b]:

 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

• This breaks rule 2. Mar 25, 2021 at 2:35

# APL (Dyalog Classic), 11 bytes

{(⍳∘.⍺⍺⍳)⍵}


Try it online!

# APL NARS, 22 bytes, 11 chars

{(⍳∘.⍺⍺⍳)⍵}


test:

  h←{(⍳∘.⍺⍺⍳)⍵}
×h 4
1 2  3  4
2 4  6  8
3 6  9 12
4 8 12 16
+h 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
*h 7
1  1   1    1     1      1      1
2  4   8   16    32     64    128
3  9  27   81   243    729   2187
4 16  64  256  1024   4096  16384
5 25 125  625  3125  15625  78125
6 36 216 1296  7776  46656 279936
7 49 343 2401 16807 117649 823543



even if {(⍳∘.⍺⍺⍳)⍵} seems to be ok, {(⍳∘.⍵⍵⍳)⍺} it seems not ok

  f←{(⍳∘.⍵⍵⍳)⍺}
4 f×
SYNTAX ERROR


Possible spaces are not as the examples in the question too...

h with g h n is the operator generator of table nxn For each diadic function g. In particular =h n is unit matrix for nxn.

• Can you add a TIO (or equivalent). The output in your tests is incorrect but, without being able to test it, I don't know if it's due to an error in your solution or human error in pasting the wrong data into the solution. Jan 31, 2018 at 12:22
• @Shaggy the table + start from 0, where the table x start from 1 (in the examples of question)... Here start all from 1
– user58988
Jan 31, 2018 at 12:59
• In that case, your solution is invalid as it's not outputting what the challenge specifies it should. Jan 31, 2018 at 14:56
• The output for exponents *h 7 shouldn't be included. It doesn't include the exponent values as the top row like it should. Jan 31, 2018 at 17:33
• @mbomb007 *h 7 first row 1^1 .. 1^7 2th row 2^1..2^7 3th row 3^1 3^2..3^7 where is the problem?
– user58988
Jan 31, 2018 at 17:47

# AWK, 111 bytes

{s=$2=="*"?2:$2=="+"?1:0;for(i=s-2;s&&++i<=$1;print"")for(j=s-2;++j<=$1;)printf("%"(s==1?2:$1)"s",s>1?i*j:i+j)}  Try it online! Output formatting may be slightly different than requested. Input: 4,+ 4,* 6,+ Output: 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 0 1 2 3 4 5 6 1 2 3 4 5 6 7 2 3 4 5 6 7 8 3 4 5 6 7 8 9 4 5 6 7 8 910 5 6 7 8 91011 6 7 8 9101112  # C (gcc), 123 bytes -Dp(i,c)for(i==0;i++<n;c) -Df(c)if(x===*#c)p(i,puts(""))p(j,printf("%d ",i c j)); n;x;main(i,j){scanf("%d%c",&n,&x);f(+)f(*)}  Try it online! # Japt-R, 22 bytes +17 bytes for the input validation and output formatting! :o õ ïV ú f@©"*+"øVÃòU mq  Try it # Mathematica, 46 bytes Grid@Outer[If[#2=="+",Plus,Times],r=Range@#,r]&  Unnamed function taking two arguments, a number and one of the characters "+" or "*". Sample output for the arguments 12,"*": • Does this actually meet the requirements? it looks like would output a multiplication table for any non + input, rather than only outputing a multiplication table on * and a addition table on + Mar 25, 2021 at 2:16 • @Taylor You are right! This answer seems to be invalid. Mar 25, 2021 at 2:34 # JavaScript, 137 bytes f=(n,s,b="")=>{for(i=0;i++<n;b+="\n")for(j=0;j++<n;b+=(r=s=='*'?i*j:s=='+'?i+j:"")+" ".repeat((n**2+"a").length-(r+"").length));return b}  Creates function f that returns the string. This calculates the number of spaces as per number of digits in (dimension^2)-1, and not as shown in the example output, as currently written. Outputs nothing if there is something other than + or *. I'm open to golfing tips. ### Explanation f=(n,s,b="")=>{ for(i=0;i++<n;b+="\n") for(j=0;j++<n;b+=(r=s=='*'?i*j:s=='+'?i+j:"")+" ".repeat((n**2+"a").length-(r+"").length)); return b }  I like when most of my code is just in my for-loops :) n is the number; s is the operator; b is the string that contains the output; r contains the actual value of the table cells This is a system of two nested for-loops, I add b by i*j or i+j depending on the operator (r=s=='*'?i*j:s=='+'?i+j:""), and then add the spaces afterward " ".repeat((n**2+"a").length-(r+"").length). At the end of the j-for-loop, I concatenate a newline to b. ### Snack Snippet f=(n,s,b="")=>{for(i=0;i++<n;b+="\n")for(j=0;j++<n;b+=(r=s=='*'?i*j:s=='+'?i+j:"")+" ".repeat((n**2+"a").length-(r+"").length));return b} console.log(f(10,'+')); ## JavaScript (ES6), 160 bytes (n,s,w=${s==*?n*n:n+n}.length)=>[...Array(n)].map((_,i,a)=>a.map((_,j)=>((s==*?++j*i+j:s==+?i+j+2-!(i&&j):)+ .repeat(w)).slice(0,w)).join).join\n


Where \n represents the literal newline character. Takes care to space the grid properly for both + and - versions, and ensures the first row and column are the dimensions and not the sum.

• You might want to replace \n with an actual new line character, considering js thinks it ok to have a backtick string span multiple lines, that saves another character Jul 20, 2021 at 12:26
• @Luke_ As my description states, the byte count already takes that into account.
– Neil
Jul 20, 2021 at 17:31

# Python 3 - 154 133 127 bytes

def s(d,o):R,s=range(1,d+1),' '*len(str((d,2)[o=='+']*d-1));return''.join([str((i*j,i+j)[o=='+'])+s+'\n'*(i==d)for j in R for i in R])


Edit : Changed spacing between number according to the new changes in the question.

Input :
8 +
Output :
2  3  4  5  6  7  8  9
3  4  5  6  7  8  9  10
4  5  6  7  8  9  10  11
5  6  7  8  9  10  11  12
6  7  8  9  10  11  12  13
7  8  9  10  11  12  13  14
8  9  10  11  12  13  14  15
9  10  11  12  13  14  15  16


# R, 48 bytes

function(n,o)write(format(outer(1:n,1:n,o)),1,n)


Try it online!

Same I/O as the accepted answer.

• This breaks rule 2. Mar 25, 2021 at 2:36

# Vyxal, 18 bytes

ɾ?\vdpĖ:vvS$fGL↲Ṡ⁋  Try it Online! Without strict IO formats, the whole :vvS$fGL↲Ṡ⁋ could be omitted

## Python 3 - 140 bytes

a=lambda d,o:print(*map(lambda f:(' '*~-len(str([d,2][o>'*']*d))).join([str([i*f,i+f][o>'*'])for i in range(1,d+1)]),range(1,d+1)),sep='\n')

• This breaks rule 2. Mar 25, 2021 at 2:38

# JavaScript (Node.js), 91 bytes

(n,_)=>[...'.'.repeat(n)].map((a,i)=>Array.from({length:n},(v,k)=>_='*'?(k+1)*(i+1):k+i+1))


Try it online!

• This breaks rule 2. Mar 25, 2021 at 2:36
• It needs to be a string an not an array Jul 20, 2021 at 7:50