20
\$\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\$
  • 5
    \$\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 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\$ – Redwolf Programs Apr 4 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 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\$ – Redwolf Programs Apr 4 at 21:52
  • 1
    \$\begingroup\$ I have the feeling this challenge would have been better as a code-bowling challenge tbh :) \$\endgroup\$ – Kevin Cruijssen Apr 6 at 8:42

38 Answers 38

13
\$\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
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Nice answer! It appears the decimal representation of 9^n is n digits long up to n=21. \$\endgroup\$ – Redwolf Programs Apr 5 at 14:28
11
\$\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!

| improve this answer | |
\$\endgroup\$
10
\$\begingroup\$

brainfuck, 1 byte

.

Outputs a single null byte.

Try it online!

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ The fun thing with brainfuck is you can do this with nothing but dots over any number of lines! \$\endgroup\$ – Hand-E-Food Apr 5 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 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\$ – my pronoun is monicareinstate Apr 9 at 15:18
9
\$\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
| improve this answer | |
\$\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!

| improve this answer | |
\$\endgroup\$
7
\$\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).

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ So, can this work? I tried it and it didn't throw a syntax error. \$\endgroup\$ – Λ̸̸ Apr 5 at 4:41
  • \$\begingroup\$ It does work and it produces the expected output without errors. Thanks a lot for the suggestion! \$\endgroup\$ – RaphaelQuinet Apr 5 at 7:13
7
\$\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!

| improve this answer | |
\$\endgroup\$
5
\$\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.

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

Ruby, 34 bytes

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

Try it online!

| improve this answer | |
\$\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!

| improve this answer | |
\$\endgroup\$
4
\$\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!

| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I wonder why Java doesn't support backslash line continuers. \$\endgroup\$ – S.S. Anne Apr 7 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\$ – Kevin Cruijssen Apr 8 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 at 16:30
3
\$\begingroup\$

Wolfram Language (Mathematica), 43 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I think the new-line might be equivalent to the pilcrow at byte 10 of the code-page, , try it \$\endgroup\$ – Jonathan Allan Apr 5 at 15:39
  • \$\begingroup\$ @JonathanAllan However, you made the program not visually a triangle. \$\endgroup\$ – Λ̸̸ Apr 6 at 12:46
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.)

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

| improve this answer | |
\$\endgroup\$
3
\$\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)
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Erlang (escript), 76 bytes

f
()
->[
%%%%
%%%%%
string
:copies
("!", X)
++"\n"||X
<- lists :
seq(1,11)].

Try it online!

| improve this answer | |
\$\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 :)

| improve this answer | |
\$\endgroup\$
2
\$\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"

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Io, 43 bytes

A lot more "content" than my previous submissions.

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

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Readability improvement: Try it online! \$\endgroup\$ – my pronoun is monicareinstate Apr 6 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\$ – Λ̸̸ Apr 6 at 10:58
2
\$\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!

| improve this answer | |
\$\endgroup\$
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
| improve this answer | |
\$\endgroup\$
2
\$\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.

| improve this answer | |
\$\endgroup\$
  • 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\$ – math junkie Apr 8 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 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\$ – math junkie Apr 8 at 15:50
  • \$\begingroup\$ @mathjunkie You are definitely correct, I missed that. Shoot and I thought I found a loophole. \$\endgroup\$ – Del Apr 8 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 at 16:21
2
\$\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.

| improve this answer | |
\$\endgroup\$
1
\$\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

| improve this answer | |
\$\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).

| improve this answer | |
\$\endgroup\$
1
\$\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.

| improve this answer | |
\$\endgroup\$
1
\$\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.
| improve this answer | |
\$\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

| improve this answer | |
\$\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.