Challenge
Print the numbers:
1
22
333
4444
55555
666666
7777777
88888888
999999999
In that order.
I/O
Takes no input. The numbers can have any delimiters desired (or none). That includes lists, cell arrays, .jpeg, etc.... Example outputs:
122333444455555666666777777788888888999999999
[1,22,333,4444,55555,666666,7777777,88888888,999999999]
etc....
Code Example
This is an un-golfed example that may perhaps act as algorithm guide (or maybe not):
Turing Machine Code, 535 bytes
0 * 1 r L
L * _ r 2
2 * 2 r a
a * 2 r M
M * _ r 3
3 * 3 r b
b * 3 r c
c * 3 r N
N * _ r 4
4 * 4 r d
d * 4 r e
e * 4 r f
f * 4 r O
O * _ r 5
5 * 5 r g
g * 5 r h
h * 5 r i
i * 5 r j
j * 5 r P
P * _ r 6
6 * 6 r k
k * 6 r l
l * 6 r m
m * 6 r n
n * 6 r o
o * 6 r Q
Q * _ r 7
7 * 7 r p
p * 7 r q
q * 7 r r
r * 7 r s
s * 7 r t
t * 7 r u
u * 7 r R
R * _ r 8
8 * 8 r v
v * 8 r w
w * 8 r x
x * 8 r y
y * 8 r z
z * 8 r A
A * 8 r B
B * 8 r S
S * _ r 9
9 * 9 r C
C * 9 r D
D * 9 r E
E * 9 r F
F * 9 r G
G * 9 r H
H * 9 r I
I * 9 r J
J * 9 r halt
This prints out the numbers with a space delimiter:
1 22 333 4444 55555 666666 7777777 88888888 999999999
Challenge Type
code-golf, so shortest answer in bytes (by language) wins.
Based on a submission in the sandbox.
-
2\$\begingroup\$ Can the delimeters be numbers? \$\endgroup\$Wheat Wizard– Wheat Wizard ♦2020年03月17日 17:16:30 +00:00Commented Mar 17, 2020 at 17:16
-
\$\begingroup\$ @AdHocGarfHunter, No. Good catch. Edit: Actually, I think '0' should be acceptable. \$\endgroup\$ouflak– ouflak2020年03月17日 17:17:23 +00:00Commented Mar 17, 2020 at 17:17
-
\$\begingroup\$ Could you verify that they "strange delimiters" version of this answer, is valid? It definitely seems cheaty. \$\endgroup\$Wheat Wizard– Wheat Wizard ♦2020年03月17日 17:32:14 +00:00Commented Mar 17, 2020 at 17:32
-
\$\begingroup\$ Honestly I think it's a clever 'outside-of-the-box' solution. I'd upvote, but I'm out of votes until tomorrow. \$\endgroup\$ouflak– ouflak2020年03月17日 17:35:49 +00:00Commented Mar 17, 2020 at 17:35
-
4\$\begingroup\$ @ouflak Thanks for the algorithm guide! How did you know I always write my prototypes with Turing Machines :p \$\endgroup\$AviFS– AviFS2020年03月19日 01:01:33 +00:00Commented Mar 19, 2020 at 1:01
122 Answers 122
Batch, 74 bytes
@set s=
@for /l %%i in (1,1,9)do @call set s=%%s%%0&call echo %%s:0=%%i%%
Outputs on separate lines. Extends the string length by 1 and replaces the character with the current digit for each output.
The call commands are required so that the variable s is processed inside the loop instead of before parsing the loop.
naz, 36 bytes
1a1o1a2o1a3o1a4o1a5o1a6o1a7o1a8o1a9o
Outputs all the numbers with no extra delimiter, using the same functionality described in this answer.
Red, 30 bytes
repeat a 9[repeat b a[prin a]]
Well, the most obvious way to do it is the shortest in Red.
IBM/Lotus Notes Formula Language, 56 bytes
@For(x:=1;x<10;x:=x+1;@Set("o";o:@Repeat(@Text(x);x)));o
Formula in a multi value form field, once again showing that the only real use for @For in Notes is Code Golf!
PHP, (削除) 37 (削除ここまで) (削除) 36 (削除ここまで) 35 bytes
for(;10>$j=++$i;)for(;$j--;)echo$i;
-1 byte thanks to @Night2
-
\$\begingroup\$ Do you need the input? \$\endgroup\$ouflak– ouflak2020年03月17日 11:19:17 +00:00Commented Mar 17, 2020 at 11:19
-
1\$\begingroup\$ Nope, removed thanks. I'm so used to using the -F option, I did it without thinking. \$\endgroup\$Guillermo Phillips– Guillermo Phillips2020年03月17日 11:21:47 +00:00Commented Mar 17, 2020 at 11:21
-
-
\$\begingroup\$ Interesting lesson in precedence rules. \$\endgroup\$Guillermo Phillips– Guillermo Phillips2020年03月18日 12:55:08 +00:00Commented Mar 18, 2020 at 12:55
SNOBOL4 (CSNOBOL4), 47 bytes
O OUTPUT =DUPL(X,X)
X =LT(X,9) X + 1 :S(O)
END
Prints with a leading newline.
Batch, 85 Bytes
@for /L %%A in (1 1 9)do @For /L %%B in (1 1 %%A)do @Call Set O=%%O%%%%A
@Echo(%O%
TIO not available.
-
\$\begingroup\$ even longer than hardcode
@echo 122333444455555666666777777788888888999999999\$\endgroup\$l4m2– l4m22020年04月22日 08:36:58 +00:00Commented Apr 22, 2020 at 8:36
Whitespace, 109 bytes
[S S S T S S T N
_Push_9][N
S S N
_Create_Label_LOOP][S N
S _Duplicate_top][N
S S S N
_Create_Label_INNER_LOOP][S S S T N
_Push_1][T S S T _Subtract][S N
S _Duplicate_top][N
T S T N
_If_0_Jump_to_Label_DONE_INNER_LOOP][S T S S T N
_Copy_0-based_1st][S N
T _Swap_top_two][N
S N
S N
_Jump_to_Label_INNER_LOOP][N
S S T N
_Create_Label_DONE_INNER_LOOP][S N
N
_Discard_top][S N
S _Duplicate_top][S S S T N
_Push_1][T S S T _Subtract][S N
S _Duplicate_top][N
T S S S N
_If_0_Jump_to_Label_DONE_LOOP][N
S N
N
_Jump_to_Label_LOOP][N
S S S S N
_Create_Label_DONE_LOOP][S N
N
_Discard_top][N
S S S T N
_Create_Label_PRINT_LOOP][T N
S T _Print_top_as_integer][N
S N
S T N
_Jump_to_Label_PRINT_LOOP]
Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.
Try it online (with raw spaces, tabs and new-lines only).
Explanation in pseudo-code:
Push n=9
Start LOOP:
Duplicate top (Integer i = n)
Start INNER_LOOP
i = i - 1
if(i == 0):
Jump to DONE_INNER_LOOP
Copy n (0-based index 1)
Swap
Go to next iteration of INNER_LOOP
DONE_INNER_LOOP:
Discard i
n = n - 1
if(n == 0):
Jump to DONE_LOOP
Go to next iteration of LOOP
DONE_LOOP:
Discard n
Start PRINT_LOOP:
Print top as integer
Go to next iteration of PRINT_LOOP
Funny thing is, is that outputting with the additional single leading 0 as the challenge in the Sandbox initially had, this could have been 11 bytes shorter: try it online. xD
-
\$\begingroup\$ I'm still mulling over some kind of challenge that involves an index.... \$\endgroup\$ouflak– ouflak2020年03月19日 15:51:05 +00:00Commented Mar 19, 2020 at 15:51
Gol><>, 8 bytes
aFLRL|D;
aF | For Loop, from 0 to 10 excluding 10
L Push the current loop iteration to the stack
RL Pop the current iteration and repeat pushing the loopiterator value so many times
D; Print the entire stack content as numeric values with the debug operation and halt
-
\$\begingroup\$ You can save 2 bytes with :
for(x=0;x++<9;){console.log((x+'').repeat(x))}\$\endgroup\$Hedi– Hedi2022年01月01日 11:45:43 +00:00Commented Jan 1, 2022 at 11:45
C++ (gcc), (削除) 93 (削除ここまで), 65 bytes
int a;void n(){int b=a;while(b){std::cout<<a;b--;}if(a++!=9)n();}
The loop work is essentially the same as the previous version, but instead its own function.
It sets the starting digit -> 1 -> counter then it prints digits until the counter reaches 0. At that point it increments the digit and recalls the function until 9.
C++ (gcc), 93 bytes
int main(int n){static int a=1;int b=a;if(b>9)return 0;while(b){std::cout<<a;b--;}main(a++);}
Don't mind the main...
-
\$\begingroup\$ 60 bytes \$\endgroup\$ceilingcat– ceilingcat2020年04月18日 03:05:57 +00:00Commented Apr 18, 2020 at 3:05
-
-
vJASS, 126 bytes
//! zinc
library q{function onInit(){integer x,y;string s="";for(1<=x<10){for(0<=y<x){s+=I2S(x);}}BJDebugMsg(s);}}
//! endzinc
MathGolf, (削除) 6 (削除ここまで) 5 bytes
9{îÄí
-1 byte thanks to @maxb.
Explanation:
9{ # Loop 9 times:
î # Push the 1-based loop index
Ä # Do an inner loop that many times, using a single command:
í # Push the total number iterations of the outer loop
# (after the loops, the entire stack is joined together and output implicitly)
-
-
\$\begingroup\$ @maxb Ah, nice golf. Out of curiosity, why doesn't it work when the
Äis{? \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年04月22日 09:46:02 +00:00Commented Apr 22, 2020 at 9:46 -
\$\begingroup\$ @maxb It does seem to work if I add two trailing
}: try9{î{í}}online. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年04月22日 09:52:10 +00:00Commented Apr 22, 2020 at 9:52 -
\$\begingroup\$ There are some issues with implicit block closure with nested loops. you can omit the last
}at the end of the script if you have a single loop, but not if you have two or more nested loop blocks which are opened by{. \$\endgroup\$maxb– maxb2020年04月22日 11:53:10 +00:00Commented Apr 22, 2020 at 11:53
Python 3, 38 bytes
print([str(x)*x for x in range(1,10)])
C++ (gcc), 59 bytes
for(int i=1;i<10;++i){for(int x=1;x<=i;++x){std::cout<<i;}}
Started learning c++ an hour ago, tried codegolf :)
-
\$\begingroup\$ @ceilingcat so all the imports and func names must be in the code? Thanks for pointing this out! \$\endgroup\$Dion– Dion2020年05月04日 15:44:17 +00:00Commented May 4, 2020 at 15:44
-
\$\begingroup\$ 67 bytes \$\endgroup\$ceilingcat– ceilingcat2020年05月04日 16:43:39 +00:00Commented May 4, 2020 at 16:43
Fortran (GFortran), (削除) 38 (削除ここまで) 28 bytes
print*,(10**i/9*i,i=1,9)
end
Edit: Turns out the program statement is optional, saving 10 bytes!
-
1\$\begingroup\$ I'm glad somebody did a Fortran answer. \$\endgroup\$ouflak– ouflak2020年03月22日 12:59:19 +00:00Commented Mar 22, 2020 at 12:59
C# (Visual C# Interactive Compiler), 45 bytes
for(int j,i=0;i++<9;)for(j=i;j-->0;Write(i));
MAWP 0.1, (削除) 22 (削除ここまで) 20 bytes
[![~!:~1A]%!9A?.%1M]
Explanation:
[ start of loop
! duplicates top of stack
[ start of loop
~!:~ prints bottom stack value
1A subtracts 1
] end of loop
% removes top of stack (0 from the counter in the previous loop)
!9A diffeence between top value and 9
?. if top 0, then terminate program
% removes top value
1M adds 1 to top value
] end of loop
Clojure, 32 bytes
(for[i(range 1 10)](repeat i i))
This results in a lists of numbers:
((1) (2 2) (3 3 3) (4 4 4 4) (5 5 5 5 5) (6 6 6 6 6 6) (7 7 7 7 7 7 7) (8 8 8 8 8 8 8 8) (9 9 9 9 9 9 9 9 9))
International Phonetic Esoteric Language, 21 bytes
{A}1ɑee0ɑbue1søɒe1søɒ
Prints 122333444455555666666777777788888888999999999.
Explanation:
{A}1ɑee0ɑbue1søɒe1søɒ
{A}1 (push loop bounds, 1 to 10)
ɑ (start loop)
e (push the current index)
e0 (push loop bounds for current digit, 0 to index)
ɑ (start loop)
bu (DUP and CPRINT current digit)
e1sø (increment index)
ɒ (end loop)
e1sø (increment index)
ɒ (end loop)
-
\$\begingroup\$ You can get your answer formatted in the standard format used on this site, over here. Just put your code in and click on the link symbol above. That will take you to another page where you will find the Code Golf format option (pasted into your clipboard). You can then just paste that into your answer. \$\endgroup\$ouflak– ouflak2020年06月25日 16:28:45 +00:00Commented Jun 25, 2020 at 16:28
-
\$\begingroup\$ Ok, thanks! I'll remember that. \$\endgroup\$user95943– user959432020年06月25日 19:44:35 +00:00Commented Jun 25, 2020 at 19:44
brainfuck, 50 bytes
+[>+<+++++]+++++++++[>-->+[<.>>+<-]>[<+>-]<<+++<-]
Assumes byte cells; does not go out of bounds to left.
Poetic, 222 bytes
THE NUMERICAL I/O RULES
i saw a digit,o yes
o,again,i saw a number;too many
i do the I/O rules,i get a repeat
i now count to zeros
o,from one right to nine,i do digits,e.t.c
seldom a newline,since i know i am swayed not to
Rust, (削除) 77 (削除ここまで) (削除) 75 (削除ここまで) (削除) 73 (削除ここまで) (削除) 72 (削除ここまで) (削除) 57 (削除ここまで) 56 bytes
fn main(){for k in 1..10{for j in 0..k{print!("{}",k)}}}
And I thought Lua was verbose!
Thanks to ovs for saving 15 bytes.
-
\$\begingroup\$ You don't need
i,0..kandprintln!("{}",i)works just fine. Then you can use a closure instead of a full program like this:||for i in 1..10{for j in 0..k{println!("{}",k)}}. \$\endgroup\$ovs– ovs2020年10月08日 19:54:24 +00:00Commented Oct 8, 2020 at 19:54 -
1
-
\$\begingroup\$ @ovs, Wow. This is the first time I've coded anything in Rust (we've just adopted it at my workplace), so it's clear I've got a lot to learn. I'm still trying to wrap my head around how the closures work. I'm using
print!instead. That seems to work. \$\endgroup\$ouflak– ouflak2020年10月08日 19:59:20 +00:00Commented Oct 8, 2020 at 19:59