Print a physical multiplication table
$begingroup$
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 code-golf, 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
code-golf ascii-art array-manipulation
$endgroup$
add a comment |
$begingroup$
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 code-golf, 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
code-golf ascii-art array-manipulation
$endgroup$
$begingroup$
Can we have extra row/columns of background characters in front, rather than at the end of the table?
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. sure, as long as the rows line up
$endgroup$
– dzaima
yesterday
$begingroup$
Nitpick: ∙ (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). :)
$endgroup$
– Andreas Rejbrand
13 hours ago
add a comment |
$begingroup$
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 code-golf, 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
code-golf ascii-art array-manipulation
$endgroup$
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 code-golf, 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
code-golf ascii-art array-manipulation
code-golf ascii-art array-manipulation
edited yesterday
dzaima
asked yesterday
dzaimadzaima
15.6k21959
15.6k21959
$begingroup$
Can we have extra row/columns of background characters in front, rather than at the end of the table?
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. sure, as long as the rows line up
$endgroup$
– dzaima
yesterday
$begingroup$
Nitpick: ∙ (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). :)
$endgroup$
– Andreas Rejbrand
13 hours ago
add a comment |
$begingroup$
Can we have extra row/columns of background characters in front, rather than at the end of the table?
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. sure, as long as the rows line up
$endgroup$
– dzaima
yesterday
$begingroup$
Nitpick: ∙ (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). :)
$endgroup$
– Andreas Rejbrand
13 hours ago
$begingroup$
Can we have extra row/columns of background characters in front, rather than at the end of the table?
$endgroup$
– Kirill L.
yesterday
$begingroup$
Can we have extra row/columns of background characters in front, rather than at the end of the table?
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. sure, as long as the rows line up
$endgroup$
– dzaima
yesterday
$begingroup$
@KirillL. sure, as long as the rows line up
$endgroup$
– dzaima
yesterday
$begingroup$
Nitpick: ∙ (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). :)
$endgroup$
– Andreas Rejbrand
13 hours ago
$begingroup$
Nitpick: ∙ (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). :)
$endgroup$
– Andreas Rejbrand
13 hours ago
add a comment |
21 Answers
21
active
oldest
votes
$begingroup$
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.
$endgroup$
$begingroup$
Thanks, BTW it can probably even be 30, if the extra blank row can be in front, rather than at the end.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Oh, can they? I missed that !
$endgroup$
– digEmAll
yesterday
add a comment |
$begingroup$
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!
$endgroup$
3
$begingroup$
This can be shortened with my old triangular number trick: 43 bytes
$endgroup$
– Ørjan Johansen
yesterday
1
$begingroup$
Just realized,flip(map.max)=mapM max
.
$endgroup$
– Ørjan Johansen
2 hours ago
$begingroup$
@ØrjanJohansen Whoa, how does that work? I think you could do with posting an answer of your own :-)
$endgroup$
– xnor
2 hours ago
$begingroup$
Set the monad to(->) b
, thenmapM :: (a -> b -> c) -> [a] -> b -> [c]
.
$endgroup$
– Ørjan Johansen
1 hour ago
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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+1choose 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$$
$endgroup$
$begingroup$
Not sure if it can save bytes, but the solution of that quadratic would be $frac{-3pmsqrt{17+8k}}2$, so there would be an integer root if $sqrtDelta$ is odd, that is, $Delta$ is an odd perfect square.
$endgroup$
– Erik the Outgolfer
yesterday
add a comment |
$begingroup$
Jelly, 8 bytes
1ẋⱮj0ȧþ`
Try it online!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 12 10 12 bytesSBCS
∘.×⍨∊,,⎕⍴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
∘.×⍨∊,,⎕⍴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
$endgroup$
1
$begingroup$
you could take input as⎕
to avoid the{
}
$endgroup$
– ngn
yesterday
add a comment |
$begingroup$
Wolfram Language (Mathematica), 41 bytes
a#&/@(a=Flatten@Array[{0,1~Table~#}&,#])&
Try it online!
$endgroup$
add a comment |
$begingroup$
PowerShell, 42 bytes
($r=1..$args[0])|%{"$($r|%{'#'*$_})`n"*$_}
Try it online!
$endgroup$
$begingroup$
40 bytes :)
$endgroup$
– mazzy
7 hours ago
$begingroup$
@mazzy, cleverly :)
$endgroup$
– Andrei Odegov
7 hours ago
add a comment |
$begingroup$
J, 17 bytes
[:*/~2=/[:I.2+i.
Try it online!
$endgroup$
$begingroup$
did not know about thisI.
trick!
$endgroup$
– Jonah
yesterday
$begingroup$
@Jonah I was waiting for an opportunity to use it :)
$endgroup$
– Galen Ivanov
22 hours ago
add a comment |
$begingroup$
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.
$endgroup$
$begingroup$
Why is this a community wiki?! If you want to waive rep you could just give it to Erik the Outgolfer
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
Do the 0 row and column help?
$endgroup$
– dfeuer
yesterday
1
$begingroup$
@dfeuer: yes, they save a byte. See the first version of my answer.
$endgroup$
– nimi
yesterday
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
Perl 6, 35 bytes
{[~](("
"~[~] *xx$_)xx$_)>>.say}
Try it online!
Anonymous code block that takes a number and prints the multiplication table with *
s and a leading newline.
$endgroup$
add a comment |
$begingroup$
Brain-Flak, 168 bytes
(({})<>){(({})<{({}[()]<(<>({})<>){(({})<{({}[(())])}>[()])}{}{}(([(()()()()())(){}]){})>)}{}(({}))>[()])}{}{}{}(){{}({}<>((((()()){}){}){}){})<>()}<>{({}<>)<>}<>{}
Try it online!
$endgroup$
add a comment |
$begingroup$
05AB1E, 9 bytes
Defines a program $ftexttt{: }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
$endgroup$
add a comment |
$begingroup$
Python 3, 88 bytes
n=range(int(input()))
for j in n:
l=''
for i in n:l+='#'*-~i+' '
print((l+'n')*-~j)
Try it online!
New contributor
$endgroup$
$begingroup$
You can save bytes by declaringl
outside the loop. Try it online!
$endgroup$
– Jo King
14 hours ago
$begingroup$
82 bytes.
$endgroup$
– Jonathan Frech
11 hours ago
add a comment |
$begingroup$
C# (.NET Core), 208 bytes, N+1 indexed.
using C=System.Console;class M{static void Main(){int i=int.Parse(C.ReadLine()),j=1,k=1;var l="";var t=l;for(;j<=i;j++,k=0)while(k++<j)l+=k<j?"#":" ";for(i=0;i<l.Length;t=l[i++]=='#'?l+"n":"n")C.Write(t);}}
Try it online!
I feel quite like this is not the best approach, but it's what I have at the moment, so here we are.
The program (ab)uses nested loops to build the initial string, and then reads through that string (since it's rectangular!) to determine whether to output the string or just a newline.
$endgroup$
$begingroup$
202?
$endgroup$
– ASCII-only
1 hour ago
$begingroup$
166, indexing fixed
$endgroup$
– ASCII-only
55 mins ago
$begingroup$
also *square not rectangular
$endgroup$
– ASCII-only
37 mins ago
$begingroup$
Squares are rectangles!
$endgroup$
– Stackstuck
27 mins ago
$begingroup$
also you should post this as your own answer, you did most of the work on it @ASCII-only.
$endgroup$
– Stackstuck
22 mins ago
add a comment |
$begingroup$
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
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181582%2fprint-a-physical-multiplication-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
21 Answers
21
active
oldest
votes
21 Answers
21
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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.
$endgroup$
$begingroup$
Thanks, BTW it can probably even be 30, if the extra blank row can be in front, rather than at the end.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Oh, can they? I missed that !
$endgroup$
– digEmAll
yesterday
add a comment |
$begingroup$
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.
$endgroup$
$begingroup$
Thanks, BTW it can probably even be 30, if the extra blank row can be in front, rather than at the end.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Oh, can they? I missed that !
$endgroup$
– digEmAll
yesterday
add a comment |
$begingroup$
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.
$endgroup$
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.
edited yesterday
answered yesterday
Kirill L.Kirill L.
5,5031525
5,5031525
$begingroup$
Thanks, BTW it can probably even be 30, if the extra blank row can be in front, rather than at the end.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Oh, can they? I missed that !
$endgroup$
– digEmAll
yesterday
add a comment |
$begingroup$
Thanks, BTW it can probably even be 30, if the extra blank row can be in front, rather than at the end.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Oh, can they? I missed that !
$endgroup$
– digEmAll
yesterday
$begingroup$
Thanks, BTW it can probably even be 30, if the extra blank row can be in front, rather than at the end.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Thanks, BTW it can probably even be 30, if the extra blank row can be in front, rather than at the end.
$endgroup$
– Kirill L.
yesterday
$begingroup$
Oh, can they? I missed that !
$endgroup$
– digEmAll
yesterday
$begingroup$
Oh, can they? I missed that !
$endgroup$
– digEmAll
yesterday
add a comment |
$begingroup$
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!
$endgroup$
3
$begingroup$
This can be shortened with my old triangular number trick: 43 bytes
$endgroup$
– Ørjan Johansen
yesterday
1
$begingroup$
Just realized,flip(map.max)=mapM max
.
$endgroup$
– Ørjan Johansen
2 hours ago
$begingroup$
@ØrjanJohansen Whoa, how does that work? I think you could do with posting an answer of your own :-)
$endgroup$
– xnor
2 hours ago
$begingroup$
Set the monad to(->) b
, thenmapM :: (a -> b -> c) -> [a] -> b -> [c]
.
$endgroup$
– Ørjan Johansen
1 hour ago
add a comment |
$begingroup$
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!
$endgroup$
3
$begingroup$
This can be shortened with my old triangular number trick: 43 bytes
$endgroup$
– Ørjan Johansen
yesterday
1
$begingroup$
Just realized,flip(map.max)=mapM max
.
$endgroup$
– Ørjan Johansen
2 hours ago
$begingroup$
@ØrjanJohansen Whoa, how does that work? I think you could do with posting an answer of your own :-)
$endgroup$
– xnor
2 hours ago
$begingroup$
Set the monad to(->) b
, thenmapM :: (a -> b -> c) -> [a] -> b -> [c]
.
$endgroup$
– Ørjan Johansen
1 hour ago
add a comment |
$begingroup$
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!
$endgroup$
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!
edited 2 hours ago
answered yesterday
xnorxnor
92.4k18188446
92.4k18188446
3
$begingroup$
This can be shortened with my old triangular number trick: 43 bytes
$endgroup$
– Ørjan Johansen
yesterday
1
$begingroup$
Just realized,flip(map.max)=mapM max
.
$endgroup$
– Ørjan Johansen
2 hours ago
$begingroup$
@ØrjanJohansen Whoa, how does that work? I think you could do with posting an answer of your own :-)
$endgroup$
– xnor
2 hours ago
$begingroup$
Set the monad to(->) b
, thenmapM :: (a -> b -> c) -> [a] -> b -> [c]
.
$endgroup$
– Ørjan Johansen
1 hour ago
add a comment |
3
$begingroup$
This can be shortened with my old triangular number trick: 43 bytes
$endgroup$
– Ørjan Johansen
yesterday
1
$begingroup$
Just realized,flip(map.max)=mapM max
.
$endgroup$
– Ørjan Johansen
2 hours ago
$begingroup$
@ØrjanJohansen Whoa, how does that work? I think you could do with posting an answer of your own :-)
$endgroup$
– xnor
2 hours ago
$begingroup$
Set the monad to(->) b
, thenmapM :: (a -> b -> c) -> [a] -> b -> [c]
.
$endgroup$
– Ørjan Johansen
1 hour ago
3
3
$begingroup$
This can be shortened with my old triangular number trick: 43 bytes
$endgroup$
– Ørjan Johansen
yesterday
$begingroup$
This can be shortened with my old triangular number trick: 43 bytes
$endgroup$
– Ørjan Johansen
yesterday
1
1
$begingroup$
Just realized,
flip(map.max)=mapM max
.$endgroup$
– Ørjan Johansen
2 hours ago
$begingroup$
Just realized,
flip(map.max)=mapM max
.$endgroup$
– Ørjan Johansen
2 hours ago
$begingroup$
@ØrjanJohansen Whoa, how does that work? I think you could do with posting an answer of your own :-)
$endgroup$
– xnor
2 hours ago
$begingroup$
@ØrjanJohansen Whoa, how does that work? I think you could do with posting an answer of your own :-)
$endgroup$
– xnor
2 hours ago
$begingroup$
Set the monad to
(->) b
, then mapM :: (a -> b -> c) -> [a] -> b -> [c]
.$endgroup$
– Ørjan Johansen
1 hour ago
$begingroup$
Set the monad to
(->) b
, then mapM :: (a -> b -> c) -> [a] -> b -> [c]
.$endgroup$
– Ørjan Johansen
1 hour ago
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
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
edited yesterday
answered yesterday
SueverSuever
9,8021446
9,8021446
add a comment |
add a comment |
$begingroup$
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+1choose 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$$
$endgroup$
$begingroup$
Not sure if it can save bytes, but the solution of that quadratic would be $frac{-3pmsqrt{17+8k}}2$, so there would be an integer root if $sqrtDelta$ is odd, that is, $Delta$ is an odd perfect square.
$endgroup$
– Erik the Outgolfer
yesterday
add a comment |
$begingroup$
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+1choose 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$$
$endgroup$
$begingroup$
Not sure if it can save bytes, but the solution of that quadratic would be $frac{-3pmsqrt{17+8k}}2$, so there would be an integer root if $sqrtDelta$ is odd, that is, $Delta$ is an odd perfect square.
$endgroup$
– Erik the Outgolfer
yesterday
add a comment |
$begingroup$
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+1choose 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$$
$endgroup$
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+1choose 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$$
edited yesterday
answered yesterday
ArnauldArnauld
78.8k795327
78.8k795327
$begingroup$
Not sure if it can save bytes, but the solution of that quadratic would be $frac{-3pmsqrt{17+8k}}2$, so there would be an integer root if $sqrtDelta$ is odd, that is, $Delta$ is an odd perfect square.
$endgroup$
– Erik the Outgolfer
yesterday
add a comment |
$begingroup$
Not sure if it can save bytes, but the solution of that quadratic would be $frac{-3pmsqrt{17+8k}}2$, so there would be an integer root if $sqrtDelta$ is odd, that is, $Delta$ is an odd perfect square.
$endgroup$
– Erik the Outgolfer
yesterday
$begingroup$
Not sure if it can save bytes, but the solution of that quadratic would be $frac{-3pmsqrt{17+8k}}2$, so there would be an integer root if $sqrtDelta$ is odd, that is, $Delta$ is an odd perfect square.
$endgroup$
– Erik the Outgolfer
yesterday
$begingroup$
Not sure if it can save bytes, but the solution of that quadratic would be $frac{-3pmsqrt{17+8k}}2$, so there would be an integer root if $sqrtDelta$ is odd, that is, $Delta$ is an odd perfect square.
$endgroup$
– Erik the Outgolfer
yesterday
add a comment |
$begingroup$
Jelly, 8 bytes
1ẋⱮj0ȧþ`
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 8 bytes
1ẋⱮj0ȧþ`
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 8 bytes
1ẋⱮj0ȧþ`
Try it online!
$endgroup$
Jelly, 8 bytes
1ẋⱮj0ȧþ`
Try it online!
answered yesterday
Erik the OutgolferErik the Outgolfer
32.5k429105
32.5k429105
add a comment |
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
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!
answered yesterday
xnorxnor
92.4k18188446
92.4k18188446
add a comment |
add a comment |
$begingroup$
APL (Dyalog Unicode), 12 10 12 bytesSBCS
∘.×⍨∊,,⎕⍴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
∘.×⍨∊,,⎕⍴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
$endgroup$
1
$begingroup$
you could take input as⎕
to avoid the{
}
$endgroup$
– ngn
yesterday
add a comment |
$begingroup$
APL (Dyalog Unicode), 12 10 12 bytesSBCS
∘.×⍨∊,,⎕⍴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
∘.×⍨∊,,⎕⍴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
$endgroup$
1
$begingroup$
you could take input as⎕
to avoid the{
}
$endgroup$
– ngn
yesterday
add a comment |
$begingroup$
APL (Dyalog Unicode), 12 10 12 bytesSBCS
∘.×⍨∊,,⎕⍴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
∘.×⍨∊,,⎕⍴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
$endgroup$
APL (Dyalog Unicode), 12 10 12 bytesSBCS
∘.×⍨∊,,⎕⍴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
∘.×⍨∊,,⎕⍴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
edited yesterday
answered yesterday
Sherlock9Sherlock9
8,06411860
8,06411860
1
$begingroup$
you could take input as⎕
to avoid the{
}
$endgroup$
– ngn
yesterday
add a comment |
1
$begingroup$
you could take input as⎕
to avoid the{
}
$endgroup$
– ngn
yesterday
1
1
$begingroup$
you could take input as
⎕
to avoid the {
}
$endgroup$
– ngn
yesterday
$begingroup$
you could take input as
⎕
to avoid the {
}
$endgroup$
– ngn
yesterday
add a comment |
$begingroup$
Wolfram Language (Mathematica), 41 bytes
a#&/@(a=Flatten@Array[{0,1~Table~#}&,#])&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 41 bytes
a#&/@(a=Flatten@Array[{0,1~Table~#}&,#])&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 41 bytes
a#&/@(a=Flatten@Array[{0,1~Table~#}&,#])&
Try it online!
$endgroup$
Wolfram Language (Mathematica), 41 bytes
a#&/@(a=Flatten@Array[{0,1~Table~#}&,#])&
Try it online!
edited yesterday
answered yesterday
shrapshrap
1013
1013
add a comment |
add a comment |
$begingroup$
PowerShell, 42 bytes
($r=1..$args[0])|%{"$($r|%{'#'*$_})`n"*$_}
Try it online!
$endgroup$
$begingroup$
40 bytes :)
$endgroup$
– mazzy
7 hours ago
$begingroup$
@mazzy, cleverly :)
$endgroup$
– Andrei Odegov
7 hours ago
add a comment |
$begingroup$
PowerShell, 42 bytes
($r=1..$args[0])|%{"$($r|%{'#'*$_})`n"*$_}
Try it online!
$endgroup$
$begingroup$
40 bytes :)
$endgroup$
– mazzy
7 hours ago
$begingroup$
@mazzy, cleverly :)
$endgroup$
– Andrei Odegov
7 hours ago
add a comment |
$begingroup$
PowerShell, 42 bytes
($r=1..$args[0])|%{"$($r|%{'#'*$_})`n"*$_}
Try it online!
$endgroup$
PowerShell, 42 bytes
($r=1..$args[0])|%{"$($r|%{'#'*$_})`n"*$_}
Try it online!
edited 5 hours ago
answered 8 hours ago
Andrei OdegovAndrei Odegov
46926
46926
$begingroup$
40 bytes :)
$endgroup$
– mazzy
7 hours ago
$begingroup$
@mazzy, cleverly :)
$endgroup$
– Andrei Odegov
7 hours ago
add a comment |
$begingroup$
40 bytes :)
$endgroup$
– mazzy
7 hours ago
$begingroup$
@mazzy, cleverly :)
$endgroup$
– Andrei Odegov
7 hours ago
$begingroup$
40 bytes :)
$endgroup$
– mazzy
7 hours ago
$begingroup$
40 bytes :)
$endgroup$
– mazzy
7 hours ago
$begingroup$
@mazzy, cleverly :)
$endgroup$
– Andrei Odegov
7 hours ago
$begingroup$
@mazzy, cleverly :)
$endgroup$
– Andrei Odegov
7 hours ago
add a comment |
$begingroup$
J, 17 bytes
[:*/~2=/[:I.2+i.
Try it online!
$endgroup$
$begingroup$
did not know about thisI.
trick!
$endgroup$
– Jonah
yesterday
$begingroup$
@Jonah I was waiting for an opportunity to use it :)
$endgroup$
– Galen Ivanov
22 hours ago
add a comment |
$begingroup$
J, 17 bytes
[:*/~2=/[:I.2+i.
Try it online!
$endgroup$
$begingroup$
did not know about thisI.
trick!
$endgroup$
– Jonah
yesterday
$begingroup$
@Jonah I was waiting for an opportunity to use it :)
$endgroup$
– Galen Ivanov
22 hours ago
add a comment |
$begingroup$
J, 17 bytes
[:*/~2=/[:I.2+i.
Try it online!
$endgroup$
J, 17 bytes
[:*/~2=/[:I.2+i.
Try it online!
answered yesterday
Galen IvanovGalen Ivanov
7,16211034
7,16211034
$begingroup$
did not know about thisI.
trick!
$endgroup$
– Jonah
yesterday
$begingroup$
@Jonah I was waiting for an opportunity to use it :)
$endgroup$
– Galen Ivanov
22 hours ago
add a comment |
$begingroup$
did not know about thisI.
trick!
$endgroup$
– Jonah
yesterday
$begingroup$
@Jonah I was waiting for an opportunity to use it :)
$endgroup$
– Galen Ivanov
22 hours ago
$begingroup$
did not know about this
I.
trick!$endgroup$
– Jonah
yesterday
$begingroup$
did not know about this
I.
trick!$endgroup$
– Jonah
yesterday
$begingroup$
@Jonah I was waiting for an opportunity to use it :)
$endgroup$
– Galen Ivanov
22 hours ago
$begingroup$
@Jonah I was waiting for an opportunity to use it :)
$endgroup$
– Galen Ivanov
22 hours ago
add a comment |
$begingroup$
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.
$endgroup$
$begingroup$
Why is this a community wiki?! If you want to waive rep you could just give it to Erik the Outgolfer
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
add a comment |
$begingroup$
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.
$endgroup$
$begingroup$
Why is this a community wiki?! If you want to waive rep you could just give it to Erik the Outgolfer
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
add a comment |
$begingroup$
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.
$endgroup$
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.
answered yesterday
community wiki
ais523
$begingroup$
Why is this a community wiki?! If you want to waive rep you could just give it to Erik the Outgolfer
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
add a comment |
$begingroup$
Why is this a community wiki?! If you want to waive rep you could just give it to Erik the Outgolfer
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– Jonathan Allan
yesterday
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
Why is this a community wiki?! If you want to waive rep you could just give it to Erik the Outgolfer
$endgroup$
– Jonathan Allan
yesterday
$begingroup$
Why is this a community wiki?! If you want to waive rep you could just give it to Erik the Outgolfer
$endgroup$
– Jonathan Allan
yesterday
1
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– ais523
yesterday
$begingroup$
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.
$endgroup$
– Jonathan Allan
yesterday
$begingroup$
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.
$endgroup$
– Jonathan Allan
yesterday
1
1
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
$begingroup$
I 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.
$endgroup$
– ais523
yesterday
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
add a comment |
$begingroup$
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!
$endgroup$
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!
answered yesterday
MooseOnTheRocksMooseOnTheRocks
1914
1914
add a comment |
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
Do the 0 row and column help?
$endgroup$
– dfeuer
yesterday
1
$begingroup$
@dfeuer: yes, they save a byte. See the first version of my answer.
$endgroup$
– nimi
yesterday
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
Do the 0 row and column help?
$endgroup$
– dfeuer
yesterday
1
$begingroup$
@dfeuer: yes, they save a byte. See the first version of my answer.
$endgroup$
– nimi
yesterday
add a comment |
$begingroup$
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
$endgroup$
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
edited yesterday
answered yesterday
niminimi
32.4k32389
32.4k32389
$begingroup$
Do the 0 row and column help?
$endgroup$
– dfeuer
yesterday
1
$begingroup$
@dfeuer: yes, they save a byte. See the first version of my answer.
$endgroup$
– nimi
yesterday
add a comment |
$begingroup$
Do the 0 row and column help?
$endgroup$
– dfeuer
yesterday
1
$begingroup$
@dfeuer: yes, they save a byte. See the first version of my answer.
$endgroup$
– nimi
yesterday
$begingroup$
Do the 0 row and column help?
$endgroup$
– dfeuer
yesterday
$begingroup$
Do the 0 row and column help?
$endgroup$
– dfeuer
yesterday
1
1
$begingroup$
@dfeuer: yes, they save a byte. See the first version of my answer.
$endgroup$
– nimi
yesterday
$begingroup$
@dfeuer: yes, they save a byte. See the first version of my answer.
$endgroup$
– nimi
yesterday
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
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
edited yesterday
answered yesterday
GrahamGraham
2,52678
2,52678
add a comment |
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
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
answered yesterday
NeilNeil
81.7k745178
81.7k745178
add a comment |
add a comment |
$begingroup$
Perl 6, 35 bytes
{[~](("
"~[~] *xx$_)xx$_)>>.say}
Try it online!
Anonymous code block that takes a number and prints the multiplication table with *
s and a leading newline.
$endgroup$
add a comment |
$begingroup$
Perl 6, 35 bytes
{[~](("
"~[~] *xx$_)xx$_)>>.say}
Try it online!
Anonymous code block that takes a number and prints the multiplication table with *
s and a leading newline.
$endgroup$
add a comment |
$begingroup$
Perl 6, 35 bytes
{[~](("
"~[~] *xx$_)xx$_)>>.say}
Try it online!
Anonymous code block that takes a number and prints the multiplication table with *
s and a leading newline.
$endgroup$
Perl 6, 35 bytes
{[~](("
"~[~] *xx$_)xx$_)>>.say}
Try it online!
Anonymous code block that takes a number and prints the multiplication table with *
s and a leading newline.
answered yesterday
Jo KingJo King
24.9k359128
24.9k359128
add a comment |
add a comment |
$begingroup$
Brain-Flak, 168 bytes
(({})<>){(({})<{({}[()]<(<>({})<>){(({})<{({}[(())])}>[()])}{}{}(([(()()()()())(){}]){})>)}{}(({}))>[()])}{}{}{}(){{}({}<>((((()()){}){}){}){})<>()}<>{({}<>)<>}<>{}
Try it online!
$endgroup$
add a comment |
$begingroup$
Brain-Flak, 168 bytes
(({})<>){(({})<{({}[()]<(<>({})<>){(({})<{({}[(())])}>[()])}{}{}(([(()()()()())(){}]){})>)}{}(({}))>[()])}{}{}{}(){{}({}<>((((()()){}){}){}){})<>()}<>{({}<>)<>}<>{}
Try it online!
$endgroup$
add a comment |
$begingroup$
Brain-Flak, 168 bytes
(({})<>){(({})<{({}[()]<(<>({})<>){(({})<{({}[(())])}>[()])}{}{}(([(()()()()())(){}]){})>)}{}(({}))>[()])}{}{}{}(){{}({}<>((((()()){}){}){}){})<>()}<>{({}<>)<>}<>{}
Try it online!
$endgroup$
Brain-Flak, 168 bytes
(({})<>){(({})<{({}[()]<(<>({})<>){(({})<{({}[(())])}>[()])}{}{}(([(()()()()())(){}]){})>)}{}(({}))>[()])}{}{}{}(){{}({}<>((((()()){}){}){}){})<>()}<>{({}<>)<>}<>{}
Try it online!
answered 21 hours ago
MegaTomMegaTom
3,5421424
3,5421424
add a comment |
add a comment |
$begingroup$
05AB1E, 9 bytes
Defines a program $ftexttt{: }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
$endgroup$
add a comment |
$begingroup$
05AB1E, 9 bytes
Defines a program $ftexttt{: }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
$endgroup$
add a comment |
$begingroup$
05AB1E, 9 bytes
Defines a program $ftexttt{: }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
$endgroup$
05AB1E, 9 bytes
Defines a program $ftexttt{: }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
answered 18 hours ago
AdnanAdnan
35.7k562225
35.7k562225
add a comment |
add a comment |
$begingroup$
Python 3, 88 bytes
n=range(int(input()))
for j in n:
l=''
for i in n:l+='#'*-~i+' '
print((l+'n')*-~j)
Try it online!
New contributor
$endgroup$
$begingroup$
You can save bytes by declaringl
outside the loop. Try it online!
$endgroup$
– Jo King
14 hours ago
$begingroup$
82 bytes.
$endgroup$
– Jonathan Frech
11 hours ago
add a comment |
$begingroup$
Python 3, 88 bytes
n=range(int(input()))
for j in n:
l=''
for i in n:l+='#'*-~i+' '
print((l+'n')*-~j)
Try it online!
New contributor
$endgroup$
$begingroup$
You can save bytes by declaringl
outside the loop. Try it online!
$endgroup$
– Jo King
14 hours ago
$begingroup$
82 bytes.
$endgroup$
– Jonathan Frech
11 hours ago
add a comment |
$begingroup$
Python 3, 88 bytes
n=range(int(input()))
for j in n:
l=''
for i in n:l+='#'*-~i+' '
print((l+'n')*-~j)
Try it online!
New contributor
$endgroup$
Python 3, 88 bytes
n=range(int(input()))
for j in n:
l=''
for i in n:l+='#'*-~i+' '
print((l+'n')*-~j)
Try it online!
New contributor
New contributor
answered 16 hours ago
KerosenicKerosenic
1
1
New contributor
New contributor
$begingroup$
You can save bytes by declaringl
outside the loop. Try it online!
$endgroup$
– Jo King
14 hours ago
$begingroup$
82 bytes.
$endgroup$
– Jonathan Frech
11 hours ago
add a comment |
$begingroup$
You can save bytes by declaringl
outside the loop. Try it online!
$endgroup$
– Jo King
14 hours ago
$begingroup$
82 bytes.
$endgroup$
– Jonathan Frech
11 hours ago
$begingroup$
You can save bytes by declaring
l
outside the loop. Try it online!$endgroup$
– Jo King
14 hours ago
$begingroup$
You can save bytes by declaring
l
outside the loop. Try it online!$endgroup$
– Jo King
14 hours ago
$begingroup$
82 bytes.
$endgroup$
– Jonathan Frech
11 hours ago
$begingroup$
82 bytes.
$endgroup$
– Jonathan Frech
11 hours ago
add a comment |
$begingroup$
C# (.NET Core), 208 bytes, N+1 indexed.
using C=System.Console;class M{static void Main(){int i=int.Parse(C.ReadLine()),j=1,k=1;var l="";var t=l;for(;j<=i;j++,k=0)while(k++<j)l+=k<j?"#":" ";for(i=0;i<l.Length;t=l[i++]=='#'?l+"n":"n")C.Write(t);}}
Try it online!
I feel quite like this is not the best approach, but it's what I have at the moment, so here we are.
The program (ab)uses nested loops to build the initial string, and then reads through that string (since it's rectangular!) to determine whether to output the string or just a newline.
$endgroup$
$begingroup$
202?
$endgroup$
– ASCII-only
1 hour ago
$begingroup$
166, indexing fixed
$endgroup$
– ASCII-only
55 mins ago
$begingroup$
also *square not rectangular
$endgroup$
– ASCII-only
37 mins ago
$begingroup$
Squares are rectangles!
$endgroup$
– Stackstuck
27 mins ago
$begingroup$
also you should post this as your own answer, you did most of the work on it @ASCII-only.
$endgroup$
– Stackstuck
22 mins ago
add a comment |
$begingroup$
C# (.NET Core), 208 bytes, N+1 indexed.
using C=System.Console;class M{static void Main(){int i=int.Parse(C.ReadLine()),j=1,k=1;var l="";var t=l;for(;j<=i;j++,k=0)while(k++<j)l+=k<j?"#":" ";for(i=0;i<l.Length;t=l[i++]=='#'?l+"n":"n")C.Write(t);}}
Try it online!
I feel quite like this is not the best approach, but it's what I have at the moment, so here we are.
The program (ab)uses nested loops to build the initial string, and then reads through that string (since it's rectangular!) to determine whether to output the string or just a newline.
$endgroup$
$begingroup$
202?
$endgroup$
– ASCII-only
1 hour ago
$begingroup$
166, indexing fixed
$endgroup$
– ASCII-only
55 mins ago
$begingroup$
also *square not rectangular
$endgroup$
– ASCII-only
37 mins ago
$begingroup$
Squares are rectangles!
$endgroup$
– Stackstuck
27 mins ago
$begingroup$
also you should post this as your own answer, you did most of the work on it @ASCII-only.
$endgroup$
– Stackstuck
22 mins ago
add a comment |
$begingroup$
C# (.NET Core), 208 bytes, N+1 indexed.
using C=System.Console;class M{static void Main(){int i=int.Parse(C.ReadLine()),j=1,k=1;var l="";var t=l;for(;j<=i;j++,k=0)while(k++<j)l+=k<j?"#":" ";for(i=0;i<l.Length;t=l[i++]=='#'?l+"n":"n")C.Write(t);}}
Try it online!
I feel quite like this is not the best approach, but it's what I have at the moment, so here we are.
The program (ab)uses nested loops to build the initial string, and then reads through that string (since it's rectangular!) to determine whether to output the string or just a newline.
$endgroup$
C# (.NET Core), 208 bytes, N+1 indexed.
using C=System.Console;class M{static void Main(){int i=int.Parse(C.ReadLine()),j=1,k=1;var l="";var t=l;for(;j<=i;j++,k=0)while(k++<j)l+=k<j?"#":" ";for(i=0;i<l.Length;t=l[i++]=='#'?l+"n":"n")C.Write(t);}}
Try it online!
I feel quite like this is not the best approach, but it's what I have at the moment, so here we are.
The program (ab)uses nested loops to build the initial string, and then reads through that string (since it's rectangular!) to determine whether to output the string or just a newline.
answered 4 hours ago
StackstuckStackstuck
1277
1277
$begingroup$
202?
$endgroup$
– ASCII-only
1 hour ago
$begingroup$
166, indexing fixed
$endgroup$
– ASCII-only
55 mins ago
$begingroup$
also *square not rectangular
$endgroup$
– ASCII-only
37 mins ago
$begingroup$
Squares are rectangles!
$endgroup$
– Stackstuck
27 mins ago
$begingroup$
also you should post this as your own answer, you did most of the work on it @ASCII-only.
$endgroup$
– Stackstuck
22 mins ago
add a comment |
$begingroup$
202?
$endgroup$
– ASCII-only
1 hour ago
$begingroup$
166, indexing fixed
$endgroup$
– ASCII-only
55 mins ago
$begingroup$
also *square not rectangular
$endgroup$
– ASCII-only
37 mins ago
$begingroup$
Squares are rectangles!
$endgroup$
– Stackstuck
27 mins ago
$begingroup$
also you should post this as your own answer, you did most of the work on it @ASCII-only.
$endgroup$
– Stackstuck
22 mins ago
$begingroup$
202?
$endgroup$
– ASCII-only
1 hour ago
$begingroup$
202?
$endgroup$
– ASCII-only
1 hour ago
$begingroup$
166, indexing fixed
$endgroup$
– ASCII-only
55 mins ago
$begingroup$
166, indexing fixed
$endgroup$
– ASCII-only
55 mins ago
$begingroup$
also *square not rectangular
$endgroup$
– ASCII-only
37 mins ago
$begingroup$
also *square not rectangular
$endgroup$
– ASCII-only
37 mins ago
$begingroup$
Squares are rectangles!
$endgroup$
– Stackstuck
27 mins ago
$begingroup$
Squares are rectangles!
$endgroup$
– Stackstuck
27 mins ago
$begingroup$
also you should post this as your own answer, you did most of the work on it @ASCII-only.
$endgroup$
– Stackstuck
22 mins ago
$begingroup$
also you should post this as your own answer, you did most of the work on it @ASCII-only.
$endgroup$
– Stackstuck
22 mins ago
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
add a comment |
$begingroup$
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
$endgroup$
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
edited 4 hours ago
answered 4 hours ago
12Me2112Me21
5,61711336
5,61711336
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181582%2fprint-a-physical-multiplication-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Can we have extra row/columns of background characters in front, rather than at the end of the table?
$endgroup$
– Kirill L.
yesterday
$begingroup$
@KirillL. sure, as long as the rows line up
$endgroup$
– dzaima
yesterday
$begingroup$
Nitpick: ∙ (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). :)
$endgroup$
– Andreas Rejbrand
13 hours ago