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.

• A couple of quite similar questions. I'm not slamming my dup hammer just yet - I'll see what others have to say. – Digital Trauma Dec 5 '16 at 17:22
• If the dimension is 10, how would one display the number 100 when there are only 2 digits in 99? – darrylyeo Dec 5 '16 at 17:25
• I suggest you allow a somewhat flexible output format. Having to conform to a very specific format is usually boring – Luis Mendo Dec 5 '16 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. – Digital Trauma Dec 5 '16 at 17:49
• Your example addition table is wrong. The top-left entry should be 2, for example. – Greg Martin Dec 6 '16 at 7:55

# 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,"*":

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

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


# 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


# 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. – Shaggy Jan 31 '18 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 '18 at 12:59
• In that case, your solution is invalid as it's not outputting what the challenge specifies it should. – Shaggy Jan 31 '18 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. – mbomb007 Jan 31 '18 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 '18 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  # 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.

# 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


## 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')


# 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!

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

# 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!