| Bytes | Lang | Time | Link |
|---|---|---|---|
| 006 | Japt | 240910T112711Z | Shaggy |
| 010 | Zsh | 190908T094233Z | GammaFun |
| 070 | C gcc | 180202T161123Z | gastropn |
| 030 | JavaScript ES6 | 180202T122427Z | ETHprodu |
| 044 | shortC | 180205T045934Z | MD XF |
| 068 | SNOBOL4 CSNOBOL4 | 180202T152711Z | Giuseppe |
| 074 | BrainFlak | 180203T213602Z | Nitrodon |
| 112 | tinylisp | 180204T045601Z | DLosc |
| 002 | Pyth | 180202T122437Z | Mr. Xcod |
| 018 | Ruby | 180202T224936Z | m-chrzan |
| 034 | brainfuck | 180202T232302Z | Jo King |
| 029 | Julia 0.6 | 180202T215402Z | gggg |
| 022 | Python 2 | 180202T204227Z | xnor |
| 068 | Haskell | 180202T155408Z | Laikoni |
| 028 | Python 2 | 180202T123209Z | Rod |
| 030 | Python 2 | 180202T131029Z | ovs |
| 044 | R | 180202T142445Z | Giuseppe |
| 006 | Charcoal | 180202T132756Z | Neil |
| 025 | Perl 5 | 180202T131551Z | Dom Hast |
| 002 | 05AB1E | 180202T122845Z | Mr. Xcod |
Japt, 6 bytes
P±QOpP
P±QOpP
P :Initially empty string
± :Append and reassign result
Q : Quotation mark
OpP :Output P with trailing newline
Zsh, 10 bytes
s+=0
<<<$s
...yeah, this is a bit better. Append to string N times, then print N times. Turns out <<<foo<<<foo works just fine.
Zsh, 64 bytes
Character used: (space).
f(){printf '%*s\n' $1}
:<<'E'
E
repeat $[i=LINENO/3];f $i
exit
The midpoint is between the second E and the newline following it. A heredoc will end when there is an E on a line by itself, which happens right in the middle of the code.
C (gcc), 170 168 96 80 72 70 bytes
Much shorter version. Still wish I could find a solution without the preprocessor.
i;main(n){for(;i++<n;)printf
#if 0
#endif
(" %*c",n=__LINE__/4, 10);}
Old 168 byte version:
#ifndef A
#define A p(c){putchar(c);}j,n;main(i){for(
#else
#define A n++,
#endif
A
#ifndef B
#define B i=++n;i--;p(10))for(j=n;j--;)p(64);}
#else
#define B
#endif
B
JavaScript (ES6), 42 32 30 bytes
s=[this.s]+0; console.log(s);
Second iteration:
s=[this.s]+0; s=[this.s]+0; console.log(s);console.log(s);
This works by appending a 0 to s each time the first half of the code is run, and printing s itself each time the second half is run. Takes advantage of four quirks of JavaScript:
- The current environment can be referred to with
this. This allows us to dothis.sin place ofs. - When accessing a property that has not been defined on an object, instead of throwing an error, JavaScript returns
undefined. - An array plus a number returns a string.
[1,2,3] + 4 === "1,2,34" - When stringifying an array,
undefinedis converted to the empty string, which means that[undefined] + 0 === "0".
Put together, this means that we can express the first half (generating a string of zeroes) in just 13 bytes. If using alert instead of console.log is allowed, we can save 4 more bytes by shortening the second half.
shortC, 56 44 bytes
-12 bytes: wait duh I'm using shortC why not use some shortened C stuff
s[];A
strcat(s,"@");//
Js);/*filling space*/
I would've used standard C, but that requires a } at the end which messes with replication. shortC inserts it at EOF implicitly.
SNOBOL4 (CSNOBOL4), 130 68 bytes
Now with no comments! See the edit history for an explanation of the old algorithm.
X =X + 1
A =ARRAY(X,DUPL(1,X));
I I =I + 1
OUTPUT =A<I> :S(I)
END
Explanation:
X =X + 1 ;* increment X
A =ARRAY(X,DUPL(1,X)); ;* create an x-length array with 1 repeated x times for each element
I I =I + 1 ;* for i < x
OUTPUT =A<I> :S(I) ;* output a[i]
END
Because an END label is required and anything after the first END label is ignored, we get two advantages for this challenge:
- operations in the first half of the program are repeated
Xtimes for theXrepetitions - there will (to the interpreter) only exist one copy of the second half, including labels.
This suggests that we use the repetition for the first half, and then we can use a more "conventional" labeling approach to repeat the output X times.
The first half is
X =X + 1
A =ARRAY(X,DUPL(1,X));
which, when repeated, increments X the appropriate number of times, and creates an ARRAY A with indices from 1 to X and where each element of A is the string 1 repeated X times.
Then no matter how many times the program is repeated, the interpreter only sees:
I I =I + 1
OUTPUT =A<I> :S(I)
END
which is a typical SNOBOL program that prints out the elements of A one at a time until the index goes out of bounds, then terminates the program.
; is an optional line terminator usually reserved for one-line EVAL or CODE statements that quite neatly brings the byte count to 68 and marks the halfway point, allowing the code to be appended there.
Brain-Flak, 74 bytes
(((((()()()){}){}){}){})((()()()()()<>){})<>([]){({}[()]<(({})<>)<>>)}{}<>
Explanation
(((((()()()){}){}){}){}) # push 48 ("0") onto first stack
((()()()()()<>){}) # push 10 (\n) onto second stack
<>([]){({}[()]< # a number of times equal to the height of the first stack:
(({})<>)<> # copy the top of the first stack to the second stack
>)}{}<> # cleanup and return to second stack
The break point is in the middle of the <> in the "push 10" section. Breaking this up will leave a 5 on the third stack until we reach the corresponding second half, at which point pushing 10 will resume right where it left off.
While it is possible to push a printable ASCII value (space) in 22 bytes, this would make the central <> be executed after pushing 5. By adding two more bytes, I was able to move the <> so that all of the progress toward pushing 10 was on the third stack. As a bonus, this also made the resulting square more aesthetically pleasing.
tinylisp, 112 bytes
(load library) (d N((q((x)(i x(inc x)1)))(v(h(t(t(h(t(q())))))))))(join(repeat-val(string(repeat-val 42 N))N)nl)
Try it online! Also doubled and fivefold.
The "build a string in the first half, print it in the second half" approach that a lot of languages are taking won't work in tinylisp, since there are no mutable variables. Instead, we do some serious code nesting.
When a second copy of the code is inserted, it is placed inside the (q()), which wraps it in a list. Then (h(t(t(h(t(...)))))) drills into that list to the part after (d N. (v(...)) evaluates it; then we pass it to the unnamed function (q((x)(i x(inc x)1))), which increments the resulting value if it's a number and returns 1 if it's the empty list. The final result in the outermost nested version of the code is assigned to N. In essence, we've set up a weird sort of recursion that counts the number of nesting levels.
The second half of the code then creates a string of N asterisks, then a list of N such strings, then joins the list on newlines. The result is displayed with a trailing newline.
Pyth, 2 bytes
5
Try it online! Also Try it doubled, tripled!
How does that work?
\n is the command that prints its argument with a trailing newline, while returning it simultaneously. So, each time you make an insertion, you turn the integer literal 5 into a number containing N copies of 5 concatenated, and the leading newlines basically make sure it's printed the appropriate number of times, thus keeping it square.
brainfuck, 44 34 bytes
crossed out 44 is still regular 44 ;(
,>-[<+>---]++++++++++[<]>[.>]<----
Try it doubled, tripled. Look, no padding!
Prints squares of U. It splits right down the middle of the 10 +s.
Julia 0.6, 29 bytes
All my ideas were longer than adapting xnor's clever python solution.
i=0;i+=1; i
println("0"^i)
Becomes
i=0;i+=1; ii=0;i+=1; i
println("0"^i)
println("0"^i)
Python 2, 22 bytes
i=0;i+=1; i
print'*'*i
Doubled:
i=0;i+=1; ii=0;i+=1; i
print'*'*i
print'*'*i
Note that the second half starts with a newline character.
Haskell, 68 bytes
let s@(z:n)="0\n"in case lines<$>[]of(h:t):_->(h:h:t)>>(z:h++n);_->s
Try it online once, twice or thrice.
Because of Haskell's laziness an expression like the one above counts as a function which takes no arguments, as per this Meta question.
Python 2, 42 38 28 bytes
id='%s@'%id ;print id[22:];
Try it online!. You can also try the 2nd and 3rd iterations
Python 2, 30 bytes
False+=1 ;print'*'*False;
Try it online!, 2nd and 3rd iteration
This makes use of the fact that bools in Python are basically ints and the names False and True were reassignable in Python 2.
Python 1, 32 bytes
exit=exit+'*' ;print exit[30:];
Try it online!, 2nd and 3rd iteration
In Python 1 the builtin strings exit and quit existed to inform the user of the interactive shell how to exit it. The default value is "Use Ctrl-D (i.e. EOF) to exit.".
R, 44 bytes
F=F+1;T=TRUE*TRUE+12;
write(strrep(1,F),"");
Prints with a trailing newline. The T=TRUE*TRUE+12 is just to pad the length.
Charcoal, 6 bytes
⊞υωLυ⸿
Try it online! Explanation:
ω Predefined empty string (any variable would do here)
υ Predefined initially empty list
⊞ Push
υ ends up with a length of the number of repetitions.
υ List
L Length
Implicitly print as a row of `-`s
⸿ Move to start of next line
Perl 5, 25 bytes
24 bytes code + 1 for -p.
Appreciate that you might not want to allow commandline flags, please let me know if this is not valid.
$@=0 x++$n;;$_="$@
"x$n;