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

2
\$\begingroup\$

dc, 4 bytes

B
1f

Try it online!

Explanation:

B   Push 11 on the stack.
1   Push 1 on the stack.
f   Print the items on the stack, starting at the top, each one with a terminal newline.
\$\endgroup\$
2
\$\begingroup\$

Python 3, 53 bytes

#
##
###
for\
i in\
range(
9):####
 print(#
10**i)###

Try it online! Similar to dingledooper's submission. I was also going to submit:

brainfuck, 1 byte!!

.

Try it online! But S.S.Anne beat me by a couple of hours :)

\$\endgroup\$
2
\$\begingroup\$

HQ9+, 1 byte

Q

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Jelly, 1 byte

0

Try it online!


The vast majority of atoms can be used for this, printing 0 or 1. The few exceptions are listed as follows: (incomplete, didn't check past in the Atoms page)

  • ³⁴⁵⁹: prints more than one digit

  • ½°: prints 0.0

  • ~: prints -1

  • : prints \n

  • ⁸IRḶṬẈ: prints nothing

  • MTZ: errors

  • Ɠƈɠ: interacts with STDIN

Borderline:

\$\endgroup\$
1
  • \$\begingroup\$ The null character is fine. See my brainfuck answer. \$\endgroup\$
    – S.S. Anne
    Apr 5, 2020 at 18:21
2
\$\begingroup\$

Io, 43 bytes

A lot more "content" than my previous submissions.

\
 \
  \
for(
i,1,8
,(9**i
)     \
println)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Readability improvement: Try it online! \$\endgroup\$ Apr 6, 2020 at 10:51
  • \$\begingroup\$ @mypronounismonicareinstate Nice modification. However, I'm not going to accept it, because in my opinion, a one-liner in a case like this is much more readable than a line-breaked program. \$\endgroup\$
    – user92069
    Apr 6, 2020 at 10:58
2
\$\begingroup\$

dirt, 43 bytes

x
xx
xx|
|'a'
(a*'a
)**{1|
}.*|"11
11111"|x

run with with empty string as input:

>dirt triangle.dirt -i ""
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
\$\endgroup\$
2
\$\begingroup\$

Stax, 4 bytes

2
mR

Run and debug it at staxlang.xyz!

Nontrivial solution. Prints "\x01\n\x01\x02", so you won't see any output, but it's there. One byte is easy but boring.

2     Push 2
mR    For n in [1,2], print [1..n] as a string

Another 4-byter that feels like cheating:

0     Do nothing
|?    Source of program, implicit print.
\$\endgroup\$
2
\$\begingroup\$

Perl 5, 53 bytes

A triangular quine.

$
_=
q{$
_='$
_='."
q{$_}"
.";eva"
.l;print
###};eval

Try it online!

\$\endgroup\$
2
\$\begingroup\$

x86 machine code (32-bit), 43 bytes

As symbols in codepage 437:

`
!Q
!j
!Y_j
Q░x≤¬
'YX¬A╢
Ç∙ëq∞Çε
ê7af║█¡├

Hexdump:

600a
21510a
216a010a
21595f6a0a
51b078f3aa0a
275958aa41b60a
80f98971ec80ee0a
88376166badbadc3

A fastcall function which receives the pointer to output buffer in ecx. It fills the buffer with a triangle containing the letter x:

x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx
xxxxxxxx

Disassembly:

10013FF0 60                   pushad  
10013FF1 0A 21                or          ah,byte ptr [ecx]  
10013FF3 51                   push        ecx  
10013FF4 0A 21                or          ah,byte ptr [ecx]  
10013FF6 6A 01                push        1  
10013FF8 0A 21                or          ah,byte ptr [ecx]  
10013FFA 59                   pop         ecx  
10013FFB 5F                   pop         edi  
10013FFC 6A 0A                push        0Ah  
10013FFE 51                   push        ecx  
10013FFF B0 78                mov         al,78h  
10014001 F3 AA                rep stos    byte ptr es:[edi]  
10014003 0A 27                or          ah,byte ptr [edi]  
10014005 59                   pop         ecx  
10014006 58                   pop         eax  
10014007 AA                   stos        byte ptr es:[edi]  
10014008 41                   inc         ecx  
10014009 B6 0A                mov         dh,0Ah  
1001400B 80 F9 89             cmp         cl,89h  
1001400E 71 EC                jno         again (10013FFCh)  
10014010 80 EE 0A             sub         dh,0Ah  
10014013 88 37                mov         byte ptr [edi],dh  
10014015 61                   popad  
10014016 66 BA DB AD          mov         dx,0ADDBh  
1001401A C3                   ret  

I used various instructions to insert a new-line byte 0A into the machine code:

10013FF1 0A 21                or          ah,byte ptr [ecx]  
10014009 B6 0A                mov         dh,0Ah  
10014010 80 EE 0A             sub         dh,0Ah  

At the end of the program, I needed to pad it with 4 bytes. I used a harmless MOV command instead of 4 NOPs so I could have hexadecimal BADBAD in code.

\$\endgroup\$
2
\$\begingroup\$

PowerShell Core, 103 53 34 bytes

#
##
$n`
=1;`
$n..`
7|%{(`
$n*=9)}

A Powershell port of the R solution by Robin Ryder, thanks!

Try it online!

Previous attempt 2

Previous attempt 1

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 1 byte

Any one of 0123456789@NQXY_adg‹›¾ÊËåð will work. They will output one of 0123456789101120000000011 respectively.

\$\endgroup\$
2
\$\begingroup\$

BRASCA, 1 byte

Yep, it's another one of these.

Pick any one of these:

0123456789oOnNlLeEdDhHK+-*^s}r&|_ABmM?!ST<>=Y

Explanation

TL;DR: Most instructions work because an empty stack consists of infinite zeroes.

0-9       - Pushes the digit, implicit output.
oOnN      - Prints the top/bottom of the stack, which is 0x00 because the stack is empty.
lLeEdDhHK - Pushes 10, 13, 26, 32, 48, 65, 97, 100 and 1000 respectively, implicit output.
+-*^s}    - Operate on zeroes because of an empty stack. (ex. + on an empty stack is 0+0).
r         - Pushes a range between 0 and 0, which is just a single 0.
&|_       - Bitwise operators on zeroes because, once again, of an empty stack.
AB        - Registers are 0 by default.
mM        - Shifting an empty stack pushes a 0.
?         - Pushes a random number between 0 and 0.
!         - Pushes the stack length, which is 0.
S         - Concatenates 0 with 0, creating 0.
T         - Sets implicit output to only output the top of the stack, which is empty, so it's a 0.
<>=Y      - Compares 0 with 0 in a bunch of ways.
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 13 bytes

F
E⁺
.¦.
×⊕λ.

Try it online! Explanation: In Charcoal, newlines, which I will represent below with ¶, are simply parts of string literals.

F¶

Loop over the characters in the string literal .

E⁺¶.¦.¶

Concatenate the string literals ¶. and , loop over the characters, and print each result on its own line.

×⊕λ.

Repeat . by one more than the character index (0-indexed).

\$\endgroup\$
1
\$\begingroup\$

Keg, 1 byte

t

Try it online!

Sorry, it's a simple literal answer. But then again, all answers in Keg are going to be simple literal answers. Works because characters auto-push and auto-print.

Here's a script that will generate a triangular source code for any given n.

\$\endgroup\$
1
\$\begingroup\$

Python 3, 64 53 bytes

#
##
###
for\
i in\
range\
(1, 9):
 print(\
'.' *i )#

Try it online!

Removed a line by pushing two lines into one last one, saved 11 bytes

\$\endgroup\$
1
\$\begingroup\$

Red, 53 bytes

;
t:
"*"
loop
( 9)[
print(
(( t)))
insert((
t))1 + 7]

Try it online!

Red, 76 64 bytes

;
s:
{t:
 "*"
 loop
(10)[p
rint(t)
insert(t
)"*"]}do
trim/all s

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Zsh, 34 bytes

:
r\
ep\
eat\
 7 {:
s+=A;:
<<<$s;}

Try it online!

After re-joining lines:

:           # ':' is a no-op, just to fill space more visibly than ' '
repeat 7 {
    :
    s+=A
    :
    <<< $s
}
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 1 byte

0

Try it online ->
Very similar to other answers, shocked that no one posted this before.

\$\endgroup\$
1
\$\begingroup\$

MAWP 0.1, 1 byte

:

Outputs top of stack, which is predefined as 1

\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 134

15 lines

=
T(
(--
CODE
("owo
(^_^;)
orz")))
&RIGHT(,
)&LEFT(,)
&MID(,1,)&
IF(LN(2),)&
ARRAYFORMULA
(JOIN(CHAR(10
),,REPT(0,ROW(
OFFSET(A1,,,15)

How It Works

Stall until we have a long enough line for ARRAYFORMULA, then catch up by generating 15 rows of an increasing number of 0's using REPT() and ROW(OFFSET()) and joining them together with JOIN(). Google Sheets will auto-close the missing parens.

Notes

  • Hey, look! No duplicate literals, no duplicate function calls, and no spaces. I could have strung together a bunch of ""&s, but that would be less entertaining.
  • The bottleneck here is ArrayFormula, so unless there's a way around that, I think we're stuck.
\$\endgroup\$
1
\$\begingroup\$

Excel Online, 53

9 lines

=
T(
12*
34)&
REPT(
0,ROW(
OFFSET(
A1,,,9))
)&TEXT(,)

How It Works

Kind of like my Google Sheets answer, but the bottleneck is the REPT() instead.

Sadly, I cannot break the function name from the open paren. Also, Excel Online does not auto-close, so we're stuck with an extra line. However, some nice things that did help were that Excel Online detects Array Formulas automatically, and it displays Array elements as separate rows, so I get a free line break.

\$\endgroup\$
1
\$\begingroup\$

Java 8, 208 bytes

;
/*
who
they
are*/
public
class//
Main{//h
public //
static//14
void main//
(String[]ar)
{for(int i=0;
i<19;i++){for(
int j=0;j<i+1;j
++)  System.out.
//crucial comment
print('*');System.
out.println();}}}//

Try it online!

A triangular full program.

\$\endgroup\$
1
\$\begingroup\$

Excel, 26 bytes

=
7^
(((
ROW(
1:6))
 ))&""

Spreadsheet

Below is 17 bytes but Excel removes the line feeds in the array after I hit enter and the output is right justified.

=
3^
{2;
4;6;
8;10}
\$\endgroup\$
1
\$\begingroup\$

batch 63 23 bytes

:
T^
YP^
E==^
;%~f0

Outputs the file content.

Note:

  • TIO not available, run it from the command line after turning echo off
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 1 byte

0

Try it Online!

Any digit works.

More interestingly,

Vyxal j, 4 bytes

2
ʁ↵

Try it Online!

2   # 2
 ʁ  # Range 0-1
  ↵ # 10 ** n (vectorised)
    # (j flag) joined by newlines

Using powers of 10.

And finally,

Vyxal, 8 bytes

3
ɾ×
*⁋,

Try it Online!

3      # 3
 ɾ     # 1...3
  ×*   # That many asterisks, for each
    ⁋  # Joined by newlines
     , # Outputted. 

Making an asterisk triangle.

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

Japt, 1 byte

Q

Try it out here

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

Retina, 1 byte

`

Try it online!

The backtick sets the configuration, which in this case is empty. By default, Retina outputs the number of matches of the regex on the input. In this case that regex is the empty string, so the output is 1

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

Pyth, 1 byte

Z

Try it online!

In Pyth, Z is initialized to 0. Output is implicit so 0 will be printed


4 bytes

Z
T;

Try it online!

T is initialized to 10 and implicitly printed. ; is a no-op in this context.

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

33, 1 byte

o

Try it online!

o prints the value of the accumulator, which defaults to 0.

Bit boring, but what can you do about it.

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

Deadfish~, 1 byte

o

Outputs

0

This is kind of cheating but I can't think of any other way to do it in Deadfish~.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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