# Triangular code, triangular output

For this challenge, submissions should be a program or function shaped like a right triangle, which produces a right triangle of the same size.

What is a right triangle?

For this challenge, a right triangle consists of 1 or more lines, each containing a number of characters (assume all non-newline characters are the same width) equal to that line number:

.
..
...
....


Trailing newlines are allowed.

The challenge:

Your code should form a right triangle, and output a right triangle with the same height made up of any non-newline characters.

As this is code golf, shortest answer in bytes per language wins.

• So, to be clear, 1 byte solutions outputting a single character are valid? Also, for longer solutions, must all characters in the output be the same, as in your example? And why are functions disallowed? Commented Apr 4, 2020 at 21:19
• @Shaggy Yes, 1 byte solutions would be valid (though for most golfing languages this is probably trivial anyway), characters do not need to be the same, and I actually think I'll revise the rules to allow functions. Commented Apr 4, 2020 at 21:21
• Based on "made up of any non-newline characters", a null byte is allowed, right? Commented Apr 4, 2020 at 21:41
• @S.S.Anne Yes, I'd say the only bytes not allowed would be \n and \r (0x0a and 0x0d) Commented Apr 4, 2020 at 21:52
• I have the feeling this challenge would have been better as a code-bowling challenge tbh :) Commented Apr 6, 2020 at 8:42

# R, 34 bytes

#
d=
cat
d(9^
(1:7)
,sep="
")#####


Try it online!

Outputs $$\9^1\$$ to $$\9^7\$$:

9
81
729
6561
59049
531441
4782969

• Nice answer! It appears the decimal representation of 9^n is n digits long up to n=21. Commented Apr 5, 2020 at 14:28

# C (gcc), 53 bytes

Fixed error pointed out by @S.S. Anne

\
m\
ain
(n){
9<pr\
intf(\
"%d\n",
n*=9)||\
main(n);}


Try it online!

# brainfuck, 1 byte

.


Outputs a single null byte.

Try it online!

• The fun thing with brainfuck is you can do this with nothing but dots over any number of lines! Commented Apr 5, 2020 at 22:49
• @Hand-E-Food Not quite. You'll need a 10 for the newline, so you can do it with +, <, >, and . Commented Apr 6, 2020 at 15:54
• It still won't work without loops, because if you have to print more bytes than you have dots, you will inevitably need to reuse them. Commented Apr 9, 2020 at 15:18

# JavaScript (ES7),  43  34 bytes

A triangle of 7 rows, filled with 0's.

f
=(
s=
)=>
s[7]?
'':0+s
+f(0+s)


Try it online!

### Commented

f   // f is a recursive function
=(   // taking
s=   // a string s initialized to
)=>   // a linefeed
s[7]?   // if s has more than 7 characters,
'':0+s   // stop recursion; otherwise append a 0, followed by s,
+f(0+s)   // followed by the result of a recursive call with 0 + s

• Different method (still 34 bytes), full program Commented Apr 5, 2020 at 19:47
• 19 byte quine? Is this valid?
– tsh
Commented Jul 24, 2020 at 5:45
• @tsh There's apparently no consensus anymore about that kind of quines. Commented Jul 24, 2020 at 15:26
• @Arnauld But this question does not require a proper quine.
– tsh
Commented Jul 25, 2020 at 13:33
• @tsh You're right, I did not remember this challenge accurately. Your solution looks valid indeed. I think you should post it as a new answer. Commented Jul 25, 2020 at 13:37

# Perl 5, 665453 19 bytes

#
##
use
####
Quine


I am submitting this as a joke because I did not even bother trying to write some optimized code. Instead, I am using a module from CPAN that causes the program to print itself.

The Quine.pm module can be found on CPAN and was released in January 2001, long before this question was posted here. So I assume that it is acceptable, as it seems to be the tradition in other Code Golf questions.

Edit 1: Saved 12 bytes by removing the semicolon after the use statement.

Edit 2: Thanks to Arnauld for pointing out that I did not need the final newline character, saving one byte.

Edit 3: Thanks to petStorm who made a great improvement by putting the use statement and the module name on separate lines, reducing the program to only 19 bytes. This exercise that started as a joke is now a very competitive entry. It would be difficult to do better in a non-obscure language (insert joke about Perl's readability here).

• So, can this work? I tried it and it didn't throw a syntax error.
– user92069
Commented Apr 5, 2020 at 4:41
• It does work and it produces the expected output without errors. Thanks a lot for the suggestion! Commented Apr 5, 2020 at 7:13

# Bash, 26 bytes

The dc command turns out to be quite useful in code-golfing.

\
d\
c \
-e{\
5..1\
0}*p \


Try it online!

# Python 2, 53 43 bytes

I think we are allowed to output different characters. Because of this, I simply printed $$\10^i\$$ at every iteration.

#
##
###
i=1;
exec\
"prin\
t i;i*\
=10;"*8\


Try it online!

u
=
(9
^)
++<8;
)echo(
10**$i- 1)."\n";  Try it online! Not that bad for PHP.. displays a triangle of "9" # Java 10, 76 bytes (11 rows) v -> {// var r=""; long i =0,s=1; for(;++i <12;r+=(s *=9)+"\n") ;return r;}  Inspired by all the other answers. Try it online. • I wonder why Java doesn't support backslash line continuers. Commented Apr 7, 2020 at 14:47 • @S.S.Anne I'm actually wondering why other languages do support backslash line continues. What's the purpose of it in regular programming, apart from benig able to split long strings to multiple lines without using "\n+"? Commented Apr 8, 2020 at 15:45 • C has macros that can't take up multiple lines without them. If you want to have a big macro, it won't be readable unless you use the backslash like continuation. The same applies to line-based languages, too. I guess Java is neither of those, so it doesn't need them. Commented Apr 8, 2020 at 16:30 # C (gcc), 53 bytes ; m\ ain (n){ 9/pr\ intf(\ "%d\n", n)&&mai\ n(n*10);}  Prints powers of 10. Try it online! # SQL (Oracle)88 76 Bytes ; /* ABC DEFG HIJ*/ SELECT LPAD(1, LEVEL,1) FROM DUAL CONNECT BY LEVEL < 12;  Try it out Output: 1 11 111 1111 11111 111111 1111111 11111111 111111111 1111111111 11111111111  Edit: Thanks @Math Junkie, by following the rules I actually cut off 12 bytes. • This is an interesting post, but unfortunately, it misses the point of the challenge. The triangle formed by the source code should be increasing in size, the same way as the output Commented Apr 8, 2020 at 15:28 • @mathjunkie I read the question like 3 times, it doesn't state that it needs to be increasing size, just that the code form a right triangle. It also doesn't specify that the output be the same direction as the code, only that it be the same height. The code and output triangles are the same (Just reflected across the X-axis) – Del Commented Apr 8, 2020 at 15:33 • In the section of the description describing a right triangle, it mentions that the number of characters on a line should be "equal to that line number" Commented Apr 8, 2020 at 15:50 • @mathjunkie You are definitely correct, I missed that. Shoot and I thought I found a loophole. – Del Commented Apr 8, 2020 at 16:03 • @mathjunkie Actually thank you for pointing out my error. When I reformatted, I was actually able to save 1 line and therefore 12 bytes. – Del Commented Apr 8, 2020 at 16:21 # SmileBASIC 4, 3 bytes \ ?0  0 OK  # Wolfram Language (Mathematica), 43 bytes \ "\ "<\ >".\ "~Ta\ ble~#\ &/@Ran\ ge [8*1]  Try it online! • I like the backslash making the triangle smoother Commented Mar 14, 2022 at 8:03 # Canvas, 1 byte Boring solution, just like everyone else. :) \  Try it here! # Canvas, 4 bytes Non-trivial attempt. ＼ ２＼  Try it here! # Explanation ＼ # Draw a diagonal, with nothing on the stack # Errors silently # Newline: A character not in the code page. # It basically does nothing. ２ # 2: Push 2 onto the stack ＼ # Draw a diagonal with a length of 2 # Implicit output  • I think the new-line might be equivalent to the pilcrow at byte 10 of the code-page, ¶, try it Commented Apr 5, 2020 at 15:39 # Bash + GNU utilities, 43 bytes \ s\ eq\ -f\ 'seq\ -s "\ " %f' \$[+8]|sh


Try it online!

I couldn't quite get this down to 7 rows; I needed to pad it by 4 bytes to fill out the 8-row triangle (that's why I have $[+8] in the code instead of just 8). BSD Challenge! If you take the same idea as the GNU solution above but use the BSD utility jot instead of seq, it's just one byte too long for a 7-row solution (which would be 34 bytes):  \ j\ ot\ -w\ 'jot\ -s "\ " ' 7|sh # This is one byte too long for 7 rows :( .  If someone can see how to shave just 1 byte off this BSD version, that would get it down to a 7-row 34-byte solution. Here's a TIO link to the BSD version if anybody wants to try their hand at eliminating that one last byte! This also works under OS X, if you have a Macintosh. (Obviously this version, like the GNU version in my main answer above, can be padded to be another 8-row 43-byte solution, but that's not as interesting.) • I have a 26-byte solution if you want. Commented Apr 5, 2020 at 19:20 • @dingledooper That's a nice one. It's completely different from mine; go ahead and post it yourself. Commented Apr 5, 2020 at 19:23 • Ok thanks, that's what I'll do. Commented Apr 5, 2020 at 19:26 • I love the smooth edge on your triangle! Commented Apr 6, 2020 at 23:07 • @Hand-E-Food Thank you\ Commented Apr 7, 2020 at 3:33 # Clojure, 10389 76 bytes ( ;; ;;; loop [i 9] (when( * i;;;; 99999999 )(println i)(recur(* i 9))));;;;  Try it online! Prints the first 11 powers of 9. Exits with an ArithmeticException: integer overflow when trying to multiply $$\9^{12}\$$ by $$\99999999\$$. # Bash, 64 bytes \ d\ at\ e +\ d%n%\ m%n%j\ %n%Y%n\ %R%n%:z\ %n%7Y%n%\ T%n%N%n%F\  Try it online! Not the shortest but kind of fancy. Displays current date in convinient format. Tried to do it locales intependent. Last byte for fancy too. # C (gcc), 53 bytes \ m\ ain (n){ for(; 9/n;n= printf( "%0*d\n" ,n,0));;}  Try it online! # C (gcc), 53 bytes \ m\ ain (n){ 9/n&& main(\ printf( "%0*d\n" ,n,0));;}  Try it online! # C Preprocessor, 65 bytes (10 lines) . .. ... .... ..... ...... ....... ........ ......... __TIME__//  Process with GCC/Clang with -E and -P options. (Thanks to Calculuswhiz for suggesting the -P option.) All the dots are ignored. The predefined __TIME__ macro expands to a string of the form "HH:MM:SS", meaning the last line stays the same length. • Try the -P option too. Commented Jul 25, 2020 at 2:39 • And just a style thing, but instead of ending with two spaces, how about //? Commented Jul 25, 2020 at 2:41 • @Calculuswhiz Thanks for the tip. I didn't know about the -P option. And you're right: the slashes are cleared than the spaces. I made both of the changes you suggested. Commented Jul 25, 2020 at 3:53 • No problem! I saw your answer, and I thought that surely there must a way to remove that stuff! After some Google attempts, I finally found an SO answer on it. Incidentally, those extra garbage lines are called "linemarkers". Commented Jul 25, 2020 at 4:38 • Although... maybe you want to mention the 1-byte . solution. Commented Jul 25, 2020 at 4:43 # Husk, 13 bytes ₁ ¶₂ ΘI₃ ↑3İ⁰  Try it online! No comment characters, so every element does something... ₁ # run function on line 1 (next line) ¶₂ # split output of function on line 2 (next line) ΘI₃ # prepend zero to identity of function on line 3 (next line) ↑3İ⁰ # first 3 elements of powers of 10  # Husk, 1 byte Trivial (but included for completeness) 1  Try it online! (there are many more like this... all equally uninteresting...) # TeX, 188 bytes Nothing out of the ordinary, but for completeness: % %% %%% %%%% %%%%% %%%%%% %%%%%%% %%%%%%%% \countdef ~=1%%%%%%% \countdef%% \i=26\loop%% \advance~1{%% \loop\advance% \i1.\ifnum\i<~% \repeat\endgraf} \ifnum~<18\repeat \bye%%%%%%%%%%%%%%  # PowerShell Core, 4 bytes Trivial power (up to 19 lines) 1 10  Try it online! # PowerShell Core, 4 bytes p–n junction (90 variants) 1 -1  Try it online! # PowerShell Core, 26 bytes Twinkle, twinkle, little star , '* *'* 6|%{ '*' * ++$ko}


Try it online!

# PowerShell Core, 26 bytes

The magic number in nuclear physics and the quark soup of 3 quarks $_ , 1+ 2.. 6|%{ "$_"*
$_};$_


Try it online!

# Pxem, Filename: 103 bytes + Content: 0 bytes = 103 bytes

Filename as follows (if your filesystem doesn't support LF, I'm sorry) (also requires to be ASCII-compatible):

x
.p
x.n
.o.n
.o04x
.-.t.z
.c.zxxx
.a.m.m.w
.sx.oab.-
.s.-.c.a.s
.c.o.c.$.mx .s.s.+.tao.- .s.m.a.dxxxxx  Content is empty. Try it online, with rpxem! ## Output x 10 120 xxxx xxxxx xxxxxx xxxxxxx xxxxxxxx xxxxxxxxx xxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxxx  ## With comments Note that every LF is replaced with '?'. x?.pXX.z # print'x', LF .a?x.nXX.z # print LF.ord # two-digits .a?.oXX.z # print LF .a.n?.sXX.z .a04x?.-.tXX.z # i = 4 .a.zXX.z # loop .a?.c.zxxx?.aXX.z # essentially nop .a.m.m.wXX.z # j = i; while j!=0 .a?.sx.oXX.z # print'x' .aab.-?.s.-.cXX.z # j-- .a.a.s?.c.oXX.z # end-while; print LF .a.c.$.mx?.s.s.+.tXX.z # i++
.aao.-?.s.mXX.z # break if !(14>i)
.a.a.dxxxxx # end-loop; end program

• I will admit, when I saw your username I expected INTERCAL Commented Mar 17, 2021 at 13:19

# Factor, 34 26 bytes (7 6 lines)

-8 bytes / 1 line thanks to Bubbler!


10
6
iota
n^v
stack.


Try it online!

Output:

1
10
100
1000
10000
100000


• I was able to reduce by one line using n^v (TIO). Using stack. is a nice idea. Commented Mar 24, 2021 at 0:59
• Wow, thank you @Bubbler ! Commented Mar 25, 2021 at 9:47

# Knight, 26 bytes

;
=a
0W>
6aO*
'.'=a
+1aQQQ


Try It Online!