Print a physical multiplication table

40

2

Rectangles have this nice property - an \$n \times m\$ rectangle consists of exactly \$n \times m\$ characters!

A.. more interesting property is that the rectangles can be aligned nicely in a multiplication table - for example, a \$3 \times 3\$ table:

# ## ###

# ## ###
# ## ###

# ## ###
# ## ###
# ## ###

Your challenge is to, given a number \$n\$ (\$ n > 1\$), output a formatted \$n \times n\$ multiplication table.

Rules

  • You can take the input one above or below \$n\$
  • Default I/O rules apply
  • You can choose any non-whitespace character to represent the blocks; every other character (though newlines are special) is considered whitespace. The chosen character can be different for different inputs, but must be the same throughout the input
  • The result can have unneeded characters, as long as the table aligns up and there are no occurrences of the chosen character that aren't part of the required output
  • The separators must be 1 character wide/tall, and the rectangles must be packed (i.e. no separators between their characters)
  • The empty lines can be empty, padding isn't required
  • The result can be a string, matrix, vector of lines, array of character arrays, or anything 2Dish
  • You may alternatively output a matrix/vector-of-vectors/anything 2Dish of numbers, but the background & foreground must be 2 distinct numbers (which can vary input to input, but not throughout an output) and no other numbers can be present. Extra surrounding characters are allowed with this format too (though they must match the background number)
  • This is , shortest answer in bytes, per-language, wins!

Examples

For the input 2, a valid ascii-art output, with the character , is:

        ∙ ∙∙

Result: ∙ ∙∙.
        ∙ ∙∙

yes the period is there just to confuse you
Another valid answer as a number matrix, with 2 being the background number and 9 the foreground:

[[9,2,9,9,2,2],
 [2,2,2,2,2,2],
 [9,2,9,9,2,2],
 [9,2,9,9,2,2]]

An invalid output example would be

#  # #


#  # #

#  # #

as the rectangles have separators in between them.

Example outputs for \$4 \times 4\$:

# ## ### ####

# ## ### ####
# ## ### ####

# ## ### ####
# ## ### ####
# ## ### ####

# ## ### ####
# ## ### ####
# ## ### ####
# ## ### ####


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

dzaima

Posted 2019-03-16T13:48:00.360

Reputation: 19 048

Can we have extra row/columns of background characters in front, rather than at the end of the table? – Kirill L. – 2019-03-16T16:04:08.240

@KirillL. sure, as long as the rows line up – dzaima – 2019-03-16T16:05:55.560

2Nitpick: ∙ (U+2219: BULLET OPERATOR) isn't present in the ASCII character set. Nor is • (U+2022: BULLET) or ⋅ (U+22C5: DOT OPERATOR) or · (U+00B7: MIDDLE DOT). :) – Andreas Rejbrand – 2019-03-17T12:55:40.250

Answers

10

Haskell, 43 bytes

f n=map=<<flip(map.max)$show.(10^)=<<[1..n]

Try it online!

A clever approach by Ørjan Johansen outputting with 0's and 1's, generating each 10...00 part as the string representation of a power of 10.

111111111
101001000
111111111
101001000
101001000
111111111
101001000
101001000
101001000

Haskell, 49 bytes

f n=map=<<flip(map.max)$[0^i|x<-[1..n],i<-[0..x]]

Try it online!

Generates a pattern like [1,0,1,0,0,1,0,0,0,...], then makes a 2D by taking the min of pairs. The pointfree weirdness saves 2 bytes over the more readable:

f n|l<-do x<-[1..n];0:(1<$[1..x])=[[a*b|a<-l]|b<-l]

Try it online!

xnor

Posted 2019-03-16T13:48:00.360

Reputation: 115 687

3

This can be shortened with my old triangular number trick: 43 bytes

– Ørjan Johansen – 2019-03-16T22:49:10.193

1Just realized, flip(map.max)=mapM max. – Ørjan Johansen – 2019-03-18T00:18:53.243

@ØrjanJohansen Whoa, how does that work? I think you could do with posting an answer of your own :-) – xnor – 2019-03-18T00:26:31.493

Set the monad to (->) b, then mapM :: (a -> b -> c) -> [a] -> b -> [c]. – Ørjan Johansen – 2019-03-18T00:31:03.160

@xnor you forgot to change flip(map.max) to mapM max – ASCII-only – 2019-03-18T03:35:52.997

9

R, 56 54 43 36 30 bytes

x=!!sequence(2:scan())-1;x%o%x

Try it online!

Takes input one above \$n\$ (so returns 3x3 matrix for \$n = 4\$). Returns a matrix of \$1s\$ (foreground), and \$0s\$ (background) with an extra row/column of zeroes in front.

Thanks to digEmAll for -7 bytes.

Kirill L.

Posted 2019-03-16T13:48:00.360

Reputation: 6 693

Thanks, BTW it can probably even be 30, if the extra blank row can be in front, rather than at the end. – Kirill L. – 2019-03-16T16:02:56.023

Oh, can they? I missed that ! – digEmAll – 2019-03-16T16:27:27.487

6

JavaScript (ES6),  73 72  69 bytes

Returns a string made of 1's, spaces and line feeds.

n=>(g=s=>n-->0?g(s+`${p+=1} `):s[~n]?(+s[~n]?s:'')+`
`+g(s):'')(p='')

Try it online!


JavaScript (ES7),  87 83  82 bytes

Saved 3 bytes thanks to @dzaima

Returns a binary matrix, which is built cell by cell.

n=>[...Array(n*(n+3)/2)].map((_,y,a)=>a.map(h=(z,x)=>(17+8*x)**.5%1&&(z||h(1,y))))

Try it online!

How?

The width \$w\$ of the matrix is given by:

$$w=T_n+n-1={n+1\choose 2}+n-1=\frac{n(n + 3)}{2}-1$$

(NB: As allowed by the challenge rules, we output a matrix of width \$w+1\$ instead.)

Similarly, the cell located at \$(X,Y)\$ is empty if the following quadratic admits an integer root for either \$k=X\$ or \$k=Y\$:

$$\frac{x(x+3)}{2}-1-k=0\\ x^2+3x-2-2k=0 $$

whose determinant is:

$$\Delta=9-4(-2-2k)=17+8k$$

Arnauld

Posted 2019-03-16T13:48:00.360

Reputation: 111 334

Not sure if it can save bytes, but the solution of that quadratic would be $\frac{-3\pm\sqrt{17+8k}}2$, so there would be an integer root if $\sqrt\Delta$ is odd, that is, $\Delta$ is an odd perfect square. – Erik the Outgolfer – 2019-03-16T19:18:17.883

5

MATL, 14 10 bytes

:"@:Fv]g&*

This answer uses 1 for the blocks and 0 for the background

Try it at MATL Online

Explanation

     % Implicitly grab the input as an integer, N
     %   STACK: { 3 }
:    % Create an array from 1...N
     %   STACK: { [1, 2, 3] }
"    % For each element M in this array
  @: % Create an array from 1...M
     %   STACK (for 1st iteration): { [1] }
     %   STACK (for 2nd iteration): { [1; 0], [1, 2] }
     %   STACK (for 3rd iteration): { [1; 0; 1; 2; 0], [1, 2, 3] }
  F  % Push a zero to the stack
     %   STACK (for 1st iteration): { [1], 0 }
     %   STACK (for 2nd iteration): { [1; 0], [1, 2], 0 }
     %   STACK (for 3rd iteration): { [1; 0; 1; 2; 0], [1, 2, 3], 0 }
  v  % Vertically concatenate everything on the stack
     %   STACK (for 1st iteration): { [1; 0] }
     %   STACK (for 2nd iteration): { [1; 0; 1; 2; 0] }
     %   STACK (for 3rd iteration): { [1; 0; 1; 2; 0; 1; 2; 3; 0] }
] 
g    % Convert everything to be boolean (turns all non-zeros to 1)
     %   STACK: { [1; 0; 1; 1; 0; 1; 1; 1; 0] }
&*   % Perform element-wise multiplication to expand this array out into the 2D grid
     % Implicitly display the result

Suever

Posted 2019-03-16T13:48:00.360

Reputation: 10 257

4

Jelly, 8 bytes

1ẋⱮj0ȧþ`

Try it online!

Erik the Outgolfer

Posted 2019-03-16T13:48:00.360

Reputation: 38 134

4

APL (Dyalog Unicode), 12 10 12 bytesSBCS

∘.×⍨∊,\0,⎕⍴1

Try it online!

Edit: -2 bytes from ngn. +2 bytes because the previous answers were invalid (with idea thanks to ngn and dzaima).

Explanation

∘.×⍨∊,\0,⎕⍴1

         ⎕    Take input.
           ⍴1  An array of 1s of length our input. Example: 1 1 1
       0,      Prepend a 0. Example: 0 1 1 1
     ,\        Take all the prefixes and concatenate them. Example: 0  0 1  0 1 1  0 1 1 1
    ∊          Flatten the list. Example: 0 0 1 0 1 1 0 1 1 1
∘.×⍨           Turn the above list into a multiplication table of 0s and 1s
               by multiplying the list with itself.

The output should look like:

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

Sherlock9

Posted 2019-03-16T13:48:00.360

Reputation: 11 664

1you could take input as to avoid the { } – ngn – 2019-03-16T17:31:24.277

4

Jelly, 7 bytes

‘RÄṬ|þ`

Try it online!

Outputs a digit matrix, using \$0\$ for the rectangles and \$1\$ for the padding between them. The TIO link contains a footer which formats a digit matrix in a human-readable way by lining up the rows and columns.

Explanation

‘RÄṬ|þ`
 R       Take a range from 1 to
‘          {the input} plus 1
  Ä      Cumulative sum; produces the first {input}+1 triangular numbers
   Ṭ     Produce an array with 1s at those indexes, 0s at other indexes
     þ   Create a table of {the array}
      `    with itself
    |      using bitwise OR

The number at cell \$(x,y)\$ of the resulting table will be \$1\$ if either \$x\$ or \$y\$ is a triangular number, or \$0\$ otherwise (because bitwise OR works like logical OR on 0 and 1). (We use R, range from 1, because Jelly uses 1-based indexing, so we don't have to worry about column 0 being incorrectly full of 0s; we have to add 1 to the input because the array produced by stops at the largest element given in the input, so we need to draw a line on the right-hand side and the bottom.) The gaps between triangular numbers are the consecutive integers, so the rectangular blocks formed by the gaps between the lines end up as the sizes requested by the question; and the use of an OR operation (in this case, bitwise) allows the lines to cross each other correctly.

ais523

Posted 2019-03-16T13:48:00.360

Reputation: 11

Why is this a community wiki?! If you want to waive rep you could just give it to Erik the Outgolfer – Jonathan Allan – 2019-03-16T22:09:04.343

1I CW all my answers (unless I think they might get a bounty, in which case I use a temporary account for them). Aiming for a high reputation normally means aiming to make the site worse (I once repcapped every day for a week to prove that it was possible; it wasn't particularly difficult, and yet it involved a lot of shallow questions/answers that didn't really contribute to the site much). Additionally, gaining reputation is mostly a negative on the account because it makes the site nag you into doing moderation work; and gaining privileges increases the risk of accidentally gaining badges. – ais523 – 2019-03-16T22:19:02.430

Also, I mostly disagree with the concept of ownership of posts on SE (although mostly with questions rather than answers, but you can't CW a question without moderator help). A CW marker very clearly says "if something is wrong here, feel free to edit it"; so I'd be applying a CW marker to everything even if it didn't waive reputation. SE is meant to be part wiki, after all, but people don't always use it as that. – ais523 – 2019-03-16T22:21:21.833

RE "aiming for a high reputation" if you don't want that then just post when you think it is meaningful and avoid what you term "shallow" q&a. Either you feel this answer does add something to the site, in which case post it without CW, or you think it is "shallow", in which case just don't post it. RE "I mostly disagree with the concept of ownership of posts on SE" - well you're simply on the wrong site then. – Jonathan Allan – 2019-03-16T22:35:16.560

2I think the answer adds something to the site, but if it's not CW, I feel compelled to post in a way that would gain me reputation rather than posting what I think would be interesting; back when I was a 20k user, I ended up really hating the site and almost got turned away from code golf in general because of it, so I deleted my account as a result. When I returned, I used to delete my account with every answer I posted (creating a new one for the next answer), but someone else pointed out that CWing every answer would have a similar effect, so nowadays I do that instead. – ais523 – 2019-03-16T22:39:27.290

4

05AB1E, 9 bytes

Defines a program \$f\texttt{: }\color{purple}{\texttt{Nat}} →\color{purple}{\texttt{List}}\texttt{[}\color{purple}{\texttt{List}}\texttt{[}\color{purple}{\texttt{Nat}}\texttt{]]}\$.

Code:

$L×0ýSDδ*

Uses the 05AB1E-encoding. Try it online! or use the pretty-printed version.


Explanation:

$              # Push the number 1 and the input n
 L             # Create the list [1, 2, 3, ..., n]
  ×            # Vectorized string multiplication: 1 × [1, 2, 3, ..., n]
                 This would result in ["1", "11", "111", ..., "1" × n]
   0ý          # Join the resulting list with '0', resulting in "10110111011110111110..."
     S         # Split into single digits: [1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, ...]
      Dδ*      # Multiplication table with itself

Adnan

Posted 2019-03-16T13:48:00.360

Reputation: 41 965

4

C# (Visual C# Interactive Compiler), 96 95 bytes

-1 byte thanks to Embodiment of Ignorance

n=>{var l="";for(;n>0;)l=new string('#',n--)+' '+l;for(;n<l.Length;)WriteLine(l[n++]>32?l:"");}

Try it online!

ASCII-only

Posted 2019-03-16T13:48:00.360

Reputation: 4 687

not sure if input via argument but output via stdout is allowed – ASCII-only – 2019-03-18T03:26:28.423

3Mixing IO formats is fine – Jo King – 2019-03-18T03:27:02.093

1Why not add the n-- in the new string section? – Embodiment of Ignorance – 2019-03-18T03:30:09.750

3

Python 2, 67 bytes

s='';n=input()
while n:s='#'*n+' '+s;n-=1
for c in s:print(c>' ')*s

Try it online!

Prints blank lines for empty lines, which the challenge allows.

Same length (with input one above n):

r=range(input())
for n in r:print(' '.join(i*'#'for i in r)+'\n')*n

Try it online!

xnor

Posted 2019-03-16T13:48:00.360

Reputation: 115 687

3

Wolfram Language (Mathematica), 41 bytes

a#&/@(a=Flatten@Array[{0,1~Table~#}&,#])&

Try it online!

shrap

Posted 2019-03-16T13:48:00.360

Reputation: 126

3

Charcoal, 18 bytes

≔⪫EN×#⊕ι θEθ⭆θ⌊⟦ιλ

Try it online! Link is to verbose version of code. Explanation:

   N                Input number
  E                 Map over implicit range
       ι            Current value
      ⊕             Incremented
    ×               Repetitions of
     #              Literal `#`
 ⪫                  Join with spaces
≔        θ          Assign to variable
           θ        Retrieve variable
          E         Map over characters
             θ      Retrieve variable
            ⭆      Replace characters with
              ⌊     Minimum of
               ⟦    List of
                ι   Row character
                 λ  Column character
                    Implicitly print each row on its own line

Neil

Posted 2019-03-16T13:48:00.360

Reputation: 95 035

3

PowerShell, 42 40 bytes

-2 bytes thanks to mazzy.

($r=1.."$args")|%{"$($r|%{'#'*$_})
"*$_}

Try it online!

Andrei Odegov

Posted 2019-03-16T13:48:00.360

Reputation: 939

40 bytes :) – mazzy – 2019-03-17T18:46:16.420

@mazzy, cleverly :) – Andrei Odegov – 2019-03-17T19:10:53.003

3

C# (.NET Core), 208 155 bytes

class M{static void Main(string[]a){int i=int.Parse(a[0]);var l="";for(;i>0;)l=new string('#',i--)+' '+l;for(;;)System.Console.WriteLine(l[i++]>32?l:"");}}

Try it online!

A much revised version thanks to various helpful people (see the comments).

Stackstuck

Posted 2019-03-16T13:48:00.360

Reputation: 209

1202? – ASCII-only – 2019-03-18T01:18:12.343

1166, indexing fixed – ASCII-only – 2019-03-18T01:32:52.897

also *square not rectangular – ASCII-only – 2019-03-18T01:50:42.207

Squares are rectangles! – Stackstuck – 2019-03-18T02:00:39.420

also you should post this as your own answer, you did most of the work on it @ASCII-only. – Stackstuck – 2019-03-18T02:05:24.770

...good...use of crashing... – Stackstuck – 2019-03-18T03:00:41.207

1@EmbodimentofIgnorance invalid, does not work for n >= 10... – ASCII-only – 2019-03-18T03:17:06.027

Is crashing the program like that even valid? – Stackstuck – 2019-03-18T03:18:35.700

2@Stackstuck Yes. A program is allowed to terminate by crashing – ASCII-only – 2019-03-18T03:23:08.290

3

Java 11, 109 bytes

n->{var l="";for(;n>0;)l="x".repeat(n--)+" "+l;for(;n<l.length();)System.out.println(l.charAt(n++)>32?l:"");}

Port of @ASCII-only's C# .NET answer.

Try it online.

n->{                       // Method with integer parameter and no return-type
  var l="";                //  Line-String, starting empty
  for(;n>0;)               //  Loop until `n` is 0:
    l=...+l;               //   Prepend to `l`:
       "x".repeat(n--)+" " //    Repeat "x" `n` amount of times, appended with a space
                           //    And decrease `n` by 1 afterwards with `n--`
    for(;n<l.length();)    //   Inner loop as long as `n` is smaller than the length of `l`:
      System.out.println(  //    Print with trailing newline:
        l.charAt(n++)>32?  //     If the `n`'th character of the line-String is NOT a space:
                           //     And increase `n` by 1 afterwards with `n++`
         l                 //      Print the line-String
        :                  //     Else:
         "");}             //      Print nothing (so only the newlines)

Kevin Cruijssen

Posted 2019-03-16T13:48:00.360

Reputation: 67 575

It's Java 11, not 8, because of the repeat method. – Olivier Grégoire – 2019-03-18T15:52:46.430

@OlivierGrégoire Oops.. Fixed – Kevin Cruijssen – 2019-03-18T16:11:01.033

Though, is it acceptable to have a lambda that throws an exception? I thought it was ok for a full program, but not for function/exceptions – Olivier Grégoire – 2019-03-18T16:34:34.623

@OlivierGrégoire As long as it still outputs the expected result, I don't see why not tbh. – Kevin Cruijssen – 2019-03-18T16:35:18.413

1

This is linked to this discussion. The answer seems to be if REPL are accepted (which they aren't by default), then it's fine to print on stderr or throw an exception, but if REPL isn't accepted then no std / exception are allowed.

– Olivier Grégoire – 2019-03-18T16:45:08.680

@OlivierGrégoire Hmm, seems you're indeed right. I've fixed my answer. – Kevin Cruijssen – 2019-03-18T16:55:23.753

2

APL+WIN, 29 bytes

m/⍉(m←¯1↓∊(⍳n),¨¯1)/(n,n←⎕)⍴1

Explanation:

(n,n←⎕)⍴1 prompt for integer n and create a nxn matrix of 1s

(m←¯1↓∊(⍳n) replicate the columns by 1,2,.....n and insert 0s between each replication

m/⍉ repeat replication and 0 insertion for the rows from above 

Example:

⎕:
3
1 0 1 1 0 1 1 1
0 0 0 0 0 0 0 0
1 0 1 1 0 1 1 1
1 0 1 1 0 1 1 1
0 0 0 0 0 0 0 0
1 0 1 1 0 1 1 1
1 0 1 1 0 1 1 1
1 0 1 1 0 1 1 1

Graham

Posted 2019-03-16T13:48:00.360

Reputation: 3 184

2

J, 17 bytes

[:*/~2=/\[:I.2+i.

Try it online!

Galen Ivanov

Posted 2019-03-16T13:48:00.360

Reputation: 13 815

did not know about this I. trick! – Jonah – 2019-03-17T01:53:07.143

@Jonah I was waiting for an opportunity to use it :) – Galen Ivanov – 2019-03-17T03:54:37.230

2

Perl 6, 35 33 bytes

{((\*Xx$_+1)~"
"Xx$_+1)>>.say}o^*

Try it online!

Anonymous Callable that takes a number and prints the multiplication table with *s with a trailing newline.

Explanation:

{                             }o^* # Change the input to the range 0..n-1
  (\*Xx$_+1)    # Cross string multiply '*' by all of range 1..n
                # This creates the string "* ** *** ****" etc.
            ~"\n"                 # Append a newline
                 Xx$_+1           # Cross string multiply again
 (                     )>>.say    # And print all the lines

Jo King

Posted 2019-03-16T13:48:00.360

Reputation: 38 234

2

Mouse-2002, 79 bytes

Abusing Mouse's macros to repeat functionality.

?1+n:#P,n,j,k,b#P,j,y,k,#P,n,i,' ,#P,i,x,35;;;;$P0 2%:(1%.2%.-^4%3%!'2%.1+2%:)@

Try it online!

MooseOnTheRocks

Posted 2019-03-16T13:48:00.360

Reputation: 191

2

Brain-Flak, 170 bytes

(({})<>){(({})<{({}[()]<(<>({})<>){(({})<{({}[(())])}>[()])}{}{}(([(()()()()())(){}]){})>)}{}(({}))>[()])}{}{}{}([]){{}({}<>((((()()){}){}){}){})<>([])}{}<>{({}<>)<>}<>{}

Try it online!

MegaTom

Posted 2019-03-16T13:48:00.360

Reputation: 3 787

2

Ruby, 55 bytes

->n{(s=(1..n).map{|x|?#*x}*' ').chars.map{|c|c<?!?c:s}}

Try it online!

How?

First, create the first line, then iterate through its chars. Print the whole line if the character is '#', otherwise print the single character (which is a space)

G B

Posted 2019-03-16T13:48:00.360

Reputation: 11 099

1

Haskell, 69 68 bytes

(a#b)0=[]
(a#b)n=(a#b)(n-1)++b:(a<$[1..n])
f n=((1#0)n#(0<$(1#0)n))n

Returns a matrix of numbers.

Try it online!

Variants of f with the same byte count:

f n=((#)<*>(0<$)$(1#0)n)n
f n|l<-(1#0)n=(l#(0<$l))n

nimi

Posted 2019-03-16T13:48:00.360

Reputation: 34 639

Do the 0 row and column help? – dfeuer – 2019-03-16T21:16:46.020

1@dfeuer: yes, they save a byte. See the first version of my answer. – nimi – 2019-03-16T22:18:10.930

1

Stax, 7 bytes

é╫▐§╘←╘

Run and debug it

The chosen character is backtick. (How do you code format that in markdown?)

That means a and b are considered extraneous whitespace.

recursive

Posted 2019-03-16T13:48:00.360

Reputation: 8 616

1

Ink, 151 152 151 bytes

VAR k=0
=t(n)
~k=n
-(u)
~n--
{n+1:->s(k-n)->u}->->
=r(c)
->g(c)->
{k-c:<>->r(c+1)}->->
=g(n)
{n>1:@<>->g(n-1)}@->->
=s(n)
{n:
->r(1)->
->s(n-1)
}
.->->

Try it online!

Good thing the rules allow excess characters.

Edit: +1: Fixed spacing. Also, display using @ (which doesn't need escaping) instead of # (which does)

Edit: -1: Apparently that fix also meant I no longer needed a trailing period to force a newline on the non-empty lines. Neat.

Sara J

Posted 2019-03-16T13:48:00.360

Reputation: 2 576

Ohh, I actually didn't even notice those spaces. I'll see if I can do something about them... – Sara J – 2019-03-19T15:24:47.457

1

C++, 170 156 bytes

Thanks to dzaima

#include<string>
using v=std::string;v f(int a){v s;for(int i=1;i<=a;++i,s+='\n')for(int k=0;k<i;++k,s+='\n')for(int j=1;j<=a;++j)s+=v(j,'#')+' ';return s;}

HatsuPointerKun

Posted 2019-03-16T13:48:00.360

Reputation: 1 891

138 bytes – ceilingcat – 2019-03-19T19:51:29.553

1

MathGolf, 20 bytes

╒ÉÄ10;]h\■mÆε*╣¡§y╠n

Try it online!

MathGolf really needs to get some more functionality for splitting lists and creating 2D lists.

Explanation

╒                      range(1,n+1)
 É                     start block of length 3
  Ä                    start block of length 1
   1                   push 1
    0                  push 0
     ;                 discard TOS
      ]                end array / wrap stack in array
                       the stack now contains the list [1, 0, 1, 1, 0, 1, 1, 1, 0, 1, ...]
       h               length of array/string without popping (used for splitting string)
        \              swap top elements
         ■             cartesian product with itself for lists, next collatz item for numbers
          m            explicit map
           Æ           start block of length 5
            ε*         reduce list by multiplication (logical AND)
              ╣¡       push the string " #"
                §      get character at index 0 or 1 based on logical AND value
                       block ends here, stack is now ['#',' ','#','#',' ','#',...]
                 y     join array without separator to string
                  ╠    pop a, b, push b/a (divides the string using the length of a single line)
                   n   join array of strings with newlines

maxb

Posted 2019-03-16T13:48:00.360

Reputation: 5 754

0

Python 3, 88 83 bytes

-5 bytes from Jo King

n,l=range(int(input())),''
for i in n:l+='#'*-~i+' '
for j in n:print((l+'\n')*-~j)

Try it online!

Kerosenic

Posted 2019-03-16T13:48:00.360

Reputation: 1

You can save bytes by declaring l outside the loop. Try it online!

– Jo King – 2019-03-17T11:52:18.830

@JonathanFrech There's an extra space in the newline string – Jo King – 2019-03-18T02:31:33.570

0

SmileBASIC, 83 77 bytes

Graphical output. Input is N-1

INPUT N
FOR J=0TO N
X=0FOR I=0TO N
GFILL X,Y,X+I,Y+J
X=X+I+2NEXT
Y=Y+J+2NEXT

12Me21

Posted 2019-03-16T13:48:00.360

Reputation: 6 110

0

C (gcc), 108 104 bytes

-4 bytes thanks to ceilingcat

f(n,j){char*s,u[n*n],*t=s=u,i=0;for(;i++<n;*s++=32)for(j=i;j--;)*s++=35;for(s=t;*t;)puts(*t++>32?s:"");}

Try it online!

ASCII-only

Posted 2019-03-16T13:48:00.360

Reputation: 4 687

0

Perl 6, 63 bytes

{(1..$_).map(((1..$_).map("#"x*).join(" ")~"\n")x*).join("\n")}

bb94

Posted 2019-03-16T13:48:00.360

Reputation: 1 831

1.join(' ') does nothing, '#' can be \* instead, the newline strings can use literal newlines, both l.map(str x*) can be (str Xx l) instead. 38 bytes – Jo King – 2019-03-19T05:38:28.390