| Bytes | Lang | Time | Link |
|---|---|---|---|
| 026 | Setanta | 240810T050551Z | bb94 |
| 030 | Forth gforth | 240808T133257Z | reffu |
| nan | 240808T075532Z | RARE Kpo | |
| 010 | Pyt | 230405T185014Z | Kip the |
| 012 | Zsh | 230405T174717Z | GammaFun |
| 024 | Nim | 230405T055538Z | xigoi |
| 004 | Vyxal | 210529T035935Z | emanresu |
| 072 | Haskell | 150209T023726Z | Jmac |
| 035 | PostScript | 150208T235731Z | Ben Jack |
| 087 | C | 150205T073617Z | feersum |
| 010 | Brainfuck | 150208T224514Z | randomra |
| 023 | Golflua | 150208T212523Z | Brian Bo |
| nan | 150205T102202Z | user3237 | |
| 014 | ><> | 150206T014530Z | Sp3000 |
| 117 | TSQL | 150205T153453Z | bmarks |
| 020 | STATA | 150205T215953Z | bmarks |
| 021 | /// | 150205T184154Z | Martin E |
| 012 | Prelude | 150205T100053Z | Martin E |
| 008 | Pyth | 150205T152548Z | Optimize |
| 011 | Brainfuck | 150205T151745Z | feersum |
| 019 | Dyalog APL | 150205T121142Z | Zgarb |
| 016 | Brainfuck | 150205T121339Z | alephalp |
| 027 | GML | 150205T121156Z | Timtech |
| 091 | Java | 150205T114112Z | cygnusv |
| 026 | ActionScript 27 / | 150205T102536Z | user3237 |
| 014 | Perl | 150205T105517Z | grc |
| 004 | TECO | 150205T062201Z | feersum |
| 006 | CJam | 150205T092514Z | Martin E |
| 020 | Python 2 | 150205T084415Z | feersum |
| 022 | Python 2 | 150205T063902Z | xnor |
| 009 | CJam | 150205T061057Z | Optimize |
Forth (gforth), 31 30 bytes
1 depth 2* 1 [do] 1. .r [loop]
Explanation
Inspired by Ben Jackson's Postscript answer, and uses effectively the same logic. Each copy leaves 1 extra item on the stack, then prints 2N-1 characters. This leads to the sum of odd-numbers trick causing us to output N^2 characters
Code Explanation
1 \ place the number 1 on the stack
depth \ get the count of items on the stack
2* \ duplicate the count of items
1 [do] \ start a loop from 1 to (2*count)-1
1. .r \ output the char '1' right-aligned in a space of size 0 (trick to avoid a space afterwards)
[loop] \ end the counted loop
here's an awk function that easily matches and occasionally even beats perl5's built-in string repetition operator (x) :
Here I'm requesting
103,307^2, or10,672,336,249 (10.67 bn), copies of the equal sign (=)
( time ( echo '103307 =' | mawk2ultra '
function _____(___,__,_,____) {
if ((___+=_++)<++_+_)
return ___--<_+!(_=__) ? (___?____:_) : \
(_=(_)_ (_)_)+--___ ? (_ _)__:_
_ = sprintf("%.*d", (____=(_*_*_)^_^_<___) \
? int(sqrt(___--)) : ___, !_)
gsub(/./,__,_)
if (____ && gsub(//,_,_))_ = substr(_, length(_)-___)
gsub(/./,_,_)
return _
} {
print _____($1,$2,RS=_) }' ) | pvE9 ) | xxh128sum | gcat -b
out9: 9.94GiB 0:00:03 [2.72GiB/s] [2.72GiB/s] [<=>]
( echo '103307 =' | mawk2ultra ; )
0.22s user 1.88s system 57% cpu 3.677 total
1 3ca9d5133bee48cba24e96f61c833f8c stdin
( time ( echo '103307 =' |
perl5 -anle 'sub __ { @_[0] x @_[1]**2 }
print __(@F[1], @F[0])' ) | pvE9 ) | xxh128sum | gcat -b
out9: 9.94GiB 0:00:04 [2.44GiB/s] [2.44GiB/s] [<=> ]
( echo '103307 =' | perl5 -anle ; )
0.72s user 1.77s system 60% cpu 4.085 total
1 3ca9d5133bee48cba24e96f61c833f8c stdin
Pyt, 10 bytes
0ǰąŁ⁻√⁺²⑴ǰ
0 pushes 0
ǰ ǰoins stack with no delimiter
ą convert to ąrray of characters
Ł get the Łength
⁻ decrement
√ square root
⁺ increment
² square
⑴ array of (top of stack) 1s
ǰ ǰoin array with no delimiters (implicitly prints at end of code)
Zsh, 12 bytes
<<<$n
n+=..
Same \$ n^2 = \sum_{k=1}^n 2k + 1 \$ as has been used elsewhere. The TIO test has to do some extra work because naively capturing the output in $(f) removes trailing newlines, and would output 0, 3, 8, 15, etc. Instead, we insert a printf : to add a character after the last newline, then ${output:0:-1} to remove it.
Nim, 24 bytes
var s=""
echo s
s&="ss"#
Attempt This Online!Attempt This Online!Attempt This Online!Attempt This Online!Attempt This Online!
Haskell, 72
putStr$let a="1";aputStr=(\n->take(n^2)$show n++cycle" ").(+1).read in a
Explanation
The apply operator $ acts as if you place surrounding parentheses around the rest of the line (there are exceptions to this, but it works in this case). aputStr is a function that takes a string with the format "abc ...", where "abc" is the square root of the length of the string, including abc. It will parse the string as an integer, and return a string starting with abc+1 and having that length squared. Because of the $ operator, this will get called recursively on "1" N times.
PostScript, 35 chars
count dup 2 mul 1 add string print
Each pass "leaks" one thing on the stack, so count goes up by 1 each time. Then it justs uses the sum of odd numbers trick.
The bytes output are all \000 because that's the initial value of strings.
C, 87 bytes
#if!__COUNTER__
#include __FILE__
main(a){a=__COUNTER__-1;printf("%*d",a*a,0);}
#endif
This uses two magic macros. __COUNTER__ is a macro that expands to 0 the first time it is used, 1 the second, etc. It is a compiler extension, but is available in both gcc, clang, and Visual Studio at least. __FILE__ is the name of the source file. Including a file in C/C++ is literally the same as pasting it directly into your source code, so it was a little tricky to make use of.
It would still be possible to use this technique without __COUNTER__. In that case, the standard guard against using code twice could be used for the #if statement, and __LINE__ could be used to count the number of characters needed.
Brainfuck, 10 chars
Both previous Brainfuck solutions were waaay too long (16 and 11 chars) so here is a shorter one:
+[.->+<]>+
In the n-th block it prints out 2*n-1 characters (with codepoints from 2*n-1 to 1)
Golflua, 23 bytes
X=2+(X|-2)w(S.t("&",X))
outputs a combination of & and \n characters.
Equivalent Lua code
X = 2 + (X or -2) -- initialize X to 0 the first time, add 2 ever other time
print(string.rep("&", X))
Each time the code snippet runs it produces 2 more characters of output than the last time, starting with 1 character. The print function appends a newline, so I initialize X to 0 instead of 1.
Java - 59 / 44 (depending on requirements)
static String n="1";
static{System.out.print(n);n+="11";}//
Apparently we're allowed to assume code runs in a class.
If it can go inside a main method:
String n="1";
System.out.print(n);n+="11";//
><>, 14 bytes
1:na*a*';'10p!
Uses the "sum of consecutive odd integers starting from 1" idea. It starts off with 1 and multiplies it by 100 each time, increasing the length of the output progressively by increments of 2.
For example, appending 5 copies gives
1100100001000000100000000
I tested by piping the output to a file, and didn't see a trailing newline.
Breakdown
1 Push 1, skipped by ! every time except the first
:n Copy top of stack and output as num
a*a* Multiply by 10 twice
';'10p Modify the source code so that the first : becomes a ; for termination
! Skip the next 1
T-SQL 117
IF OBJECT_ID('tempdb..#')IS NULL CREATE TABLE #(A INT)INSERT INTO # VALUES(1)SELECT REPLICATE('a',COUNT(*)*2-1)FROM #
Note the trailing space to ensure that the if condition is properly checked every time.
Uses the odd numbers approach. Not sure if there's a newline on select statements.
Not sure if there's a shorter way to create a table if it doesn't exist.
STATA 20
di _n($a)
gl a=$a+2
There is a trailing new line to make sure that the display (di) statement works. First display the current number in $a newlines (and one additional from the default of display). Then add 2 to $a.
Uses the even numbers approach (i.e. odd numbers approach minus 1) with an extra newline every time.
///, 21 bytes
I'm sure there is a really short and twisted way to solve this in /// but I couldn't find anything, beyond the "straightforward" way yet:
1/1\//112\///2\//1\//
This is based on the approach of printing consecutive odd numbers. The snippet consists of a 1 at the start which is printed, and two replacements which add two more 1s to that first part of each consecutive copy of the snippet. Let's go through this for N = 3. The following should be read in groups of 3 or more lines: 1. the current code, 2. the processed token(s), 3. (and following) a comment what the above token does.
1/1\//112\///2\//1\//1/1\//112\///2\//1\//1/1\//112\///2\//1\//
1
is printed
/1\//112\///2\//1\//1/1\//112\///2\//1\//1/1\//112\///2\//1\//
/1\//112\//
replaces all occurrences of 1/ with 112/. This affects the starts of all further snippets
but not the substitution commands, because the slashes in those are always escaped.
It is necessary to put a 2 in there, because otherwise the interpreter goes into an infinite
loop replacing the resulting 1/ again and again.
/2\//1\//112/1\//112\///2\//1\//112/1\//112\///2\//1\//
/2\//1\//
Replace all occurrences of 2/ with 1/, so the the next snippets substitution works again.
111/1\//112\///2\//1\//111/1\//112\///2\//1\//
111
is printed
/1\//112\///2\//1\//111/1\//112\///2\//1\//
/1\//112\//
add two 1s again
/2\//1\//11112/1\//112\///2\//1\//
/2\//1\//
turn the 2 into a 1 again
11111/1\//112\///2\//1\//
11111
print 11111
/1\//112\///2\//1\//
the last two substitutions have nothing to substitute so they do nothing
Interestingly, it works just as well if we move the 1 to the end:
/1\//112\///2\//1\//1
Prelude, 18 12 bytes
^1+(9!1-)#2+
This prints N2 tabs. It assumes a standard-compliant interpreter which prints characters instead of numbers, so if you use the Python interpreter you'll need to set NUMERIC_OUTPUT to False.
The idea is simply to use the top of the stack (which is initially 0) as 2(N-1), and print 2N-1 tabs, then increment the top of the stack by 2. Hence each repetition prints the next odd number of tabs.
Pyth, 8 bytes
*d*2Z~Z1
This relies on the fact that N2 is equal to the sum of N odd numbers. Now Pyth auto prints an new line, so I have to just print Z * 2 characters in each code where Z goes from 0 to N - 1.
Code Expansion:
*d "Print d whose value is a space character"
*2Z "2 * Z times where Z's initial value is 0"
~Z1 "Increment the value of Z";
Brainfuck, 11
I saw the first Brainfuck answer and thought it's way too long :)
[.<]>[.>]+.
The output may be easier to see if you replace the plus with a lot more pluses.
On the Nth iteration, each loop outputs N - 1 copies of the character with ASCII value 1, and then one more with +..
Dyalog APL, 20 19 bytes
A matrix based solution.
{⍺≢⍵:⍵⍪⍵,⍺⋄∊⍺}⍨⍪'a'
Try it here. Returns a string of N2 repetitions of a. Explanation by explosion for N = 2:
{⍺≢⍵:⍵⍪⍵,⍺⋄∊⍺}⍨⍪'a'{⍺≢⍵:⍵⍪⍵,⍺⋄∊⍺}⍨⍪'a'
⍪'a' Wrap 'a' into a 1x1 matrix.
'a'{ }⍨ Binary function: bind 'a' to ⍵ and the matrix to ⍺.
⍺≢⍵: The arguments are not identical,
⍵⍪⍵,⍺ so add to the matrix 1 column and 1 row of 'a's.
⍪ Identity function for a matrix.
{ }⍨ Unary function: bind the matrix to both ⍵ and ⍺.
⍺≢⍵: The arguments are identical,
∊⍺ so flatten the matrix into the string 'aaaa'.
Brainfuck, 17 16 bytes
[>+>-..+<<-]-.>+
You can test it here. Just use the fact that n2+2n+1=(n+1)2.
GML, 27
a=''show_message(a)a+='xx'a
Java - 91 bytes
{String s=System.getProperty("a","");System.out.println(s);System.setProperty("a","xx"+s);}
This solution is equivalent to this other one in Python. It surely won't win, but it was fun :)
ActionScript - 27 / 26 bytes
var n=""
trace(n);n+="11"//
or
var n=1
trace(n);n+="11"//
How it works:
var n=""
trace(n);n+="11"//var n=""
trace(n);n+="11"//
It simply comments out the first line. Note: trace adds a newline. Or maybe all the IDE's I use do that automatically.
Perl, 14 bytes
print;s//__/;
This needs to be run with Perl's -l command switch, which causes print to append new lines.
It prints the default variable $_, then prepends two underscores via substitution.
Example:
$ perl -le 'print;s//__/;print;s//__/;print;s//__/;print;s//__/;'
__
____
______
TECO, 4 bytes
V1\V
V prints the contents of the current line in the text buffer. 1\ inserts the string representation of the number 1 at the current position.
So on the Nth iteration of the program, the first V will output N - 1 copies of the character 1, then add another 1 to the text, then output N 1s.
CJam, 6 bytes
LLS+:L
Uses the fact that n2 + n + (n+1) = (n+1)2.
L "Push L. Initially this is an empty string, but its length increases by 1 with each copy
of the snippet.";
L "Push another L.";
S+ "Add a space to the second copy.";
:L "Store the lengthened string in L for the next copy of the snippet.";
Python 2, 20 bytes
g=0
print'g'*g;g+=2#
Python 2, 22
a='';print a;a+='xx';a
Prints the empty string, then two x's, then x' four and so on. With the newline after each string, this comes out to n*n characters.
One copy: "\n" (1 char)
Two copies: "\nxx\n" (4 chars)
Three copies: "\nxx\nxxxx\n" (9 chars)
In order to stop the initial variable a from being reinitialized each run, I end the code with a ;a, which is benign on its own, but combined with the next loop to create the scapegoat aa to be assigned instead. This trick isn't mine; I saw it in a previous answer. I'd appreciate if someone could point me so I could give credit.
CJam, 10 9 bytes
],)_S*a*~
This prints N2 spaces where N is the number of copies of the code.
Code eexpansion:
], "Wrap everything on stack and take length";
)_ "Increment and take copy";
S* "Get that length space string";
a* "Wrap that space string in an array and create that many copies";
~ "Unwrap so that next code can use to get length";