Addition/Multiplication table generator

5

2

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.

Dave Jones

Posted 2016-12-05T16:40:14.613

Reputation: 465

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 – 2016-12-05T17:22:08.743

1If the dimension is 10, how would one display the number 100 when there are only 2 digits in 99? – darrylyeo – 2016-12-05T17:25:35.943

7I suggest you allow a somewhat flexible output format. Having to conform to a very specific format is usually boring – Luis Mendo – 2016-12-05T17:41:34.130

4Also input format checking is generally considered to be a tedious task that doesn't really add anything to a [tag: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 – 2016-12-05T17:49:08.287

Could you add an example of output for (4,+)? – steenbergh – 2016-12-05T18:17:49.383

Sorry for the mistakes. Please leave this question alone. – Dave Jones – 2016-12-05T18:22:24.117

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.

– trichoplax – 2016-12-05T22:37:22.873

1Your example addition table is wrong. The top-left entry should be 2, for example. – Greg Martin – 2016-12-06T07:55:43.163

I not see why limitate the number of operators in "•The program should not return anything if the operation input is not + or *." Until they are 2 arg to 1: Where is the problem? – RosLuP – 2018-01-31T12:04:18.837

Answers

1

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

enter image description here

Greg Martin

Posted 2016-12-05T16:40:14.613

Reputation: 13 940

4

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

Digital Trauma

Posted 2016-12-05T16:40:14.613

Reputation: 64 644

1

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

Magic Octopus Urn

Posted 2016-12-05T16:40:14.613

Reputation: 19 422

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.

RosLuP

Posted 2016-12-05T16:40:14.613

Reputation: 3 036

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 – 2018-01-31T12:22:48.767

@Shaggy the table + start from 0, where the table x start from 1 (in the examples of question)... Here start all from 1 – RosLuP – 2018-01-31T12:59:03.657

In that case, your solution is invalid as it's not outputting what the challenge specifies it should. – Shaggy – 2018-01-31T14:56:08.350

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 – 2018-01-31T17:33:27.503

@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? – RosLuP – 2018-01-31T17:47:11.953

@RosLuP I took it as each function should have the values of n across each axis. I guess that's not required. – mbomb007 – 2018-01-31T17:56:24.583

1

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

Robert Benson

Posted 2016-12-05T16:40:14.613

Reputation: 1 339

0

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,'+'));

user41805

Posted 2016-12-05T16:40:14.613

Reputation: 16 320

0

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  

Gurupad Mamadapur

Posted 2016-12-05T16:40:14.613

Reputation: 1 791

0

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.

Neil

Posted 2016-12-05T16:40:14.613

Reputation: 95 035

0

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

Cormac

Posted 2016-12-05T16:40:14.613

Reputation: 101

0

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!

Luis felipe De jesus Munoz

Posted 2016-12-05T16:40:14.613

Reputation: 9 639

0

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.

JayCe

Posted 2016-12-05T16:40:14.613

Reputation: 2 655

0

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!

l4m2

Posted 2016-12-05T16:40:14.613

Reputation: 5 985