30
\$\begingroup\$

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.

\$\endgroup\$
14
  • 8
    \$\begingroup\$ 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? \$\endgroup\$
    – Shaggy
    Apr 4, 2020 at 21:19
  • \$\begingroup\$ @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. \$\endgroup\$ Apr 4, 2020 at 21:21
  • \$\begingroup\$ Based on "made up of any non-newline characters", a null byte is allowed, right? \$\endgroup\$
    – S.S. Anne
    Apr 4, 2020 at 21:41
  • \$\begingroup\$ @S.S.Anne Yes, I'd say the only bytes not allowed would be \n and \r (0x0a and 0x0d) \$\endgroup\$ Apr 4, 2020 at 21:52
  • 1
    \$\begingroup\$ I have the feeling this challenge would have been better as a code-bowling challenge tbh :) \$\endgroup\$ Apr 6, 2020 at 8:42

61 Answers 61

20
\$\begingroup\$

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
\$\endgroup\$
1
  • 4
    \$\begingroup\$ Nice answer! It appears the decimal representation of 9^n is n digits long up to n=21. \$\endgroup\$ Apr 5, 2020 at 14:28
14
\$\begingroup\$

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!

\$\endgroup\$
0
11
\$\begingroup\$

brainfuck, 1 byte

.

Outputs a single null byte.

Try it online!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ The fun thing with brainfuck is you can do this with nothing but dots over any number of lines! \$\endgroup\$ Apr 5, 2020 at 22:49
  • 2
    \$\begingroup\$ @Hand-E-Food Not quite. You'll need a 10 for the newline, so you can do it with +, <, >, and . \$\endgroup\$
    – S.S. Anne
    Apr 6, 2020 at 15:54
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Apr 9, 2020 at 15:18
11
\$\begingroup\$

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
\$\endgroup\$
5
  • \$\begingroup\$ Different method (still 34 bytes), full program \$\endgroup\$ Apr 5, 2020 at 19:47
  • \$\begingroup\$ 19 byte quine? Is this valid? \$\endgroup\$
    – tsh
    Jul 24, 2020 at 5:45
  • \$\begingroup\$ @tsh There's apparently no consensus anymore about that kind of quines. \$\endgroup\$
    – Arnauld
    Jul 24, 2020 at 15:26
  • \$\begingroup\$ @Arnauld But this question does not require a proper quine. \$\endgroup\$
    – tsh
    Jul 25, 2020 at 13:33
  • \$\begingroup\$ @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. \$\endgroup\$
    – Arnauld
    Jul 25, 2020 at 13:37
9
\$\begingroup\$

Perl 5, 66 54 53 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).

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

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!

\$\endgroup\$
8
\$\begingroup\$

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!

\$\endgroup\$
6
\$\begingroup\$

Haskell, 76 64 53 bytes

u
 =
 (9
 ^) 
 <$>[
 1..9]
main=  
 mapM_  
 print u 

Try it online!

This prints the first 9 powers of 9.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 43 bytes with the help of explicit layout: Try it online! \$\endgroup\$
    – Laikoni
    Jan 4, 2021 at 13:44
6
\$\begingroup\$

Ruby, 34 bytes

(
##
1..
7)##
.map{
|x|p(#
9**x)}#

Try it online!

\$\endgroup\$
5
\$\begingroup\$

C# (Visual C# Interactive Compiler), 64 53 43 bytes

;
;;
int
s=1,
i;for
(;i++<
9;Print
(s*=9));

Simply prints the first several powers of 9.

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Befunge-98 (FBBI), 34 bytes

Outputs \$ 9^k, 1 \leq k \leq 7 \$.

v
8v
<v1
v< @
> 9*v
.^ >\:
a,:|>1-

Try it online!

The first three lines

v
8v
<v1

push 8 (loop counter) and 1 (\$ 9^0 \$). The main loop can be written in a single line as

9*\1-a,:!#@_\:.
9*               multiply by 9
  \              swap to loop counter
   1-            subtract 1
     a,          output newline
       :!        duplicate and invert loop counter
         #@_     quit if non-zero, continue east otherwise
            \    swap to 9^k
             :.  duplicate and output 9^k

Try it online!

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 1 bytes

0

Try it online.

Not much to say I guess..


If this was instead, we could do something like this (can be made as arbitrary large as you'd want it to, as long as the last three bytes are ₄*»):

1
11
111
1111
11111
111111
1111111
11111111
111111111
1111111₄*»

Try it online.

Explanation:

1\n11\n111\n...  # Push all these numbers one by one to the stack
111...111        # Push the number of the last line
₄*               # Multiply it by 1000 to increase its size by 3
»                # Join everything on the stack by newlines
                 # (after which it is output implicitly as result)
\$\endgroup\$
4
\$\begingroup\$

PHP, 43 bytes

;
1;
for
(;$i
++<8;
)echo(
10**$i-
1)."\n";

Try it online!

Not that bad for PHP.. displays a triangle of "9"

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ I wonder why Java doesn't support backslash line continuers. \$\endgroup\$
    – S.S. Anne
    Apr 7, 2020 at 14:47
  • \$\begingroup\$ @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+"? \$\endgroup\$ Apr 8, 2020 at 15:45
  • \$\begingroup\$ 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. \$\endgroup\$
    – S.S. Anne
    Apr 8, 2020 at 16:30
4
\$\begingroup\$

C (gcc), 53 bytes

;
m\
ain
(n){
9/pr\
intf(\
"%d\n",
n)&&mai\
n(n*10);}

Prints powers of 10.

Try it online!

\$\endgroup\$
0
4
\$\begingroup\$

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Apr 8, 2020 at 15:28
  • \$\begingroup\$ @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) \$\endgroup\$
    – Del
    Apr 8, 2020 at 15:33
  • \$\begingroup\$ 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" \$\endgroup\$ Apr 8, 2020 at 15:50
  • \$\begingroup\$ @mathjunkie You are definitely correct, I missed that. Shoot and I thought I found a loophole. \$\endgroup\$
    – Del
    Apr 8, 2020 at 16:03
  • \$\begingroup\$ @mathjunkie Actually thank you for pointing out my error. When I reformatted, I was actually able to save 1 line and therefore 12 bytes. \$\endgroup\$
    – Del
    Apr 8, 2020 at 16:21
4
\$\begingroup\$

SmileBASIC 4, 3 bytes

\
?0

0
OK
\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 43 bytes

\
"\
"<\
>".\
"~Ta\
ble~#\
&/@Ran\
ge [8*1]

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I like the backslash making the triangle smoother \$\endgroup\$
    – CreaZyp154
    Mar 14, 2022 at 8:03
3
\$\begingroup\$

Canvas, 1 byte

Boring solution, just like everyone else. :)

\

Try it here!

Canvas, 4 bytes

Non-trivial attempt.

\
2\

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    # 2: Push 2 onto the stack
  \  # Draw a diagonal with a length of 2
      # Implicit output
\$\endgroup\$
1
  • \$\begingroup\$ I think the new-line might be equivalent to the pilcrow at byte 10 of the code-page, , try it \$\endgroup\$ Apr 5, 2020 at 15:39
3
\$\begingroup\$

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.)

\$\endgroup\$
5
  • \$\begingroup\$ I have a 26-byte solution if you want. \$\endgroup\$ Apr 5, 2020 at 19:20
  • \$\begingroup\$ @dingledooper That's a nice one. It's completely different from mine; go ahead and post it yourself. \$\endgroup\$ Apr 5, 2020 at 19:23
  • \$\begingroup\$ Ok thanks, that's what I'll do. \$\endgroup\$ Apr 5, 2020 at 19:26
  • \$\begingroup\$ I love the smooth edge on your triangle! \$\endgroup\$ Apr 6, 2020 at 23:07
  • \$\begingroup\$ @Hand-E-Food Thank you\ \$\endgroup\$ Apr 7, 2020 at 3:33
3
\$\begingroup\$

Clojure, 103 89 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\$.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Try the -P option too. \$\endgroup\$ Jul 25, 2020 at 2:39
  • 1
    \$\begingroup\$ And just a style thing, but instead of ending with two spaces, how about //? \$\endgroup\$ Jul 25, 2020 at 2:41
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – RobertR
    Jul 25, 2020 at 3:53
  • 1
    \$\begingroup\$ 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". \$\endgroup\$ Jul 25, 2020 at 4:38
  • 2
    \$\begingroup\$ Although... maybe you want to mention the 1-byte . solution. \$\endgroup\$ Jul 25, 2020 at 4:43
3
\$\begingroup\$

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...)

\$\endgroup\$
3
\$\begingroup\$

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%%%%%%%%%%%%%%
\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
1
  • 2
    \$\begingroup\$ I will admit, when I saw your username I expected INTERCAL \$\endgroup\$ Mar 17, 2021 at 13:19
3
+100
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I was able to reduce by one line using n^v (TIO). Using stack. is a nice idea. \$\endgroup\$
    – Bubbler
    Mar 24, 2021 at 0:59
  • \$\begingroup\$ Wow, thank you @Bubbler ! \$\endgroup\$ Mar 25, 2021 at 9:47
3
+100
\$\begingroup\$

Knight, 26 bytes

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

Try It Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.