Code Golf Asked on November 28, 2021
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.
0
Any digit works.
More interestingly,
j
, 4 bytes2
ʁ↵
2 # 2
ʁ # Range 0-1
↵ # 10 ** n (vectorised)
# (j flag) joined by newlines
Using powers of 10.
And finally,
3
ɾ×
*⁋,
3 # 3
ɾ # 1...3
×* # That many asterisks, for each
⁋ # Joined by newlines
, # Outputted.
Making an asterisk triangle.
Answered by emanresu A on November 28, 2021
Yep, it's another one of these.
Pick any one of these:
0123456789oOnNlLeEdDhHK+-*^s}r&|_ABmM?!ST<>=Y
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.
Answered by SjoerdPennings on November 28, 2021
:
T^
YP^
E==^
;%~f0
Outputs the file content.
Note:
echo off
Answered by T3RR0R on November 28, 2021
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.
x
10
120
xxxx
xxxxx
xxxxxx
xxxxxxx
xxxxxxxx
xxxxxxxxx
xxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxxx
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
Answered by nrgmsbki4spot1 on November 28, 2021
-8 bytes / 1 line thanks to Bubbler!
10
6
iota
n^v
stack.
Output:
1
10
100
1000
10000
100000
Answered by Michael Chatiskatzi on November 28, 2021
=
7^
(((
ROW(
1:6))
))&""
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}
Answered by Axuary on November 28, 2021
;
/*
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();}}}//
A triangular full program.
Answered by hakr14 on November 28, 2021
Nothing out of the ordinary, but for completeness:
%
%%
%%%
%%%%
%%%%%
%%%%%%
%%%%%%%
%%%%%%%%
countdef
~=1%%%%%%%
countdef%%
i=26loop%%
advance~1{%%
loopadvance%
i1.ifnumi<~%
repeatendgraf}
ifnum~<18repeat
bye%%%%%%%%%%%%%%
Answered by plante on November 28, 2021
o
Outputs
0
This is kind of cheating but I can't think of any other way to do it in Deadfish~.
Answered by emanresu A on November 28, 2021
Trivial power (up to 19 lines)
1
10
p–n junction (90 variants)
1
-1
Twinkle, twinkle, little star
,
'*
*'*
6|%{
'*' *
++$ko}
The magic number in nuclear physics and the quark soup of 3 quarks $_
,
1+
2..
6|%{
"$_"*
$_};$_
Answered by mazzy on November 28, 2021
#
##
$n`
=1;`
$n..`
7|%{(`
$n*=9)}
A Powershell port of the R solution by Robin Ryder, thanks!
Answered by Julian on November 28, 2021
?0
0
OK
Answered by BMF on November 28, 2021
₁
¶₂
ΘI₃
↑3İ⁰
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
Trivial (but included for completeness)
1
(there are many more like this... all equally uninteresting...)
Answered by Dominic van Essen on November 28, 2021
Any one of 0123456789@NQXY_adg‹›¾ÊËåð
will work. They will output one of 0123456789101120000000011
respectively.
Answered by Makonede on November 28, 2021
.
..
...
....
.....
......
.......
........
.........
__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.
Answered by RobertR on November 28, 2021
9 lines
=
T(
12*
34)&
REPT(
0,ROW(
OFFSET(
A1,,,9))
)&TEXT(,)
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.
Answered by Calculuswhiz on November 28, 2021
15 lines
=
T(
(--
CODE
("owo
(^_^;)
orz")))
&RIGHT(,
)&LEFT(,)
&MID(,1,)&
IF(LN(2),)&
ARRAYFORMULA
(JOIN(CHAR(10
),,REPT(0,ROW(
OFFSET(A1,,,15)
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.
""&
s, but that would be less entertaining.ArrayFormula
, so unless there's a way around that, I think we're stuck.Answered by Calculuswhiz on November 28, 2021
o
o
prints the value of the accumulator, which defaults to 0.
Bit boring, but what can you do about it.
Answered by TheOnlyMrCat on November 28, 2021
Answered by Dom Hastings on November 28, 2021
n*
30'
*30#
1_11_
1*3_1*
4_1*5_1
*6_1*7_1
*8_1*9_1*
10_1*11_1*
12_1*13_1*1
4_1*15_1*16_
1*17_1*18_1*1
9_1*20_1*21_1*
22_1*23_1*24_1*
25_1*26_1*27####
Output has double the amount of lines compared to the code. I hope that gives me some points too. :)
I did this in my own language, but yea, im not coding in any challenge-related things, my language stays the same most of the time, although I do add new commands sometimes, but no hardcoded output.
The output is
1
11
111
1111
11111
111111
1111111
11111111
111111111
1111111111
11111111111
111111111111
1111111111111
11111111111111
111111111111111
1111111111111111
11111111111111111
111111111111111111
1111111111111111111
11111111111111111111
111111111111111111111
1111111111111111111111
11111111111111111111111
111111111111111111111111
1111111111111111111111111
11111111111111111111111111
111111111111111111111111111
Sorry for not having a Try it online
, but im not that far with my lang.
Answered by Daniel H. on November 28, 2021
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 NOP
s so I could have hexadecimal BADBAD
in code.
Answered by anatolyg on November 28, 2021
2
mR
Run and debug it at staxlang.xyz!
Nontrivial solution. Prints "x01nx01x02", 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.
Answered by Khuldraeseth na'Barya on November 28, 2021
Answered by Dion on November 28, 2021
m
ain
(n){
for(;
9/n;n=
printf(
"%0*dn"
,n,0));;}
m
ain
(n){
9/n&&
main(
printf(
"%0*dn"
,n,0));;}
Answered by l4m2 on November 28, 2021
d
at
e +
d%n%
m%n%j
%n%Y%n
%R%n%:z
%n%7Y%n%
T%n%N%n%F
Not the shortest but kind of fancy. Displays current date in convinient format. Tried to do it locales intependent. Last byte for fancy too.
Answered by marcosm on November 28, 2021
0
Try it online ->
Very similar to other answers, shocked that no one posted this before.
Answered by HaxAddict1337 on November 28, 2021
:
r
ep
eat
7 {:
s+=A;:
<<<$s;}
After re-joining lines:
: # ':' is a no-op, just to fill space more visibly than ' '
repeat 7 {
:
s+=A
:
<<< $s
}
Answered by GammaFunction on November 28, 2021
;
/*
ABC
DEFG
HIJ*/
SELECT
LPAD(1,
LEVEL,1)
FROM DUAL
CONNECT BY
LEVEL < 12;
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.
Answered by Del on November 28, 2021
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
Answered by cardboard_box on November 28, 2021
v
->
{//
var
r="";
long i
=0,s=1;
for(;++i
<12;r+=(s
*=9)+"n")
;return r;}
Inspired by all the other answers.
Answered by Kevin Cruijssen on November 28, 2021
;
1;
for
(;$i
++<8;
)echo(
10**$i-
1)."n";
Not that bad for PHP.. displays a triangle of "9"
Answered by Kaddath on November 28, 2021
0
Not much to say I guess..
If this was code-bowling 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₄*»
Explanation:
1n11n111n... # 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)
Answered by Kevin Cruijssen on November 28, 2021
Answered by dingledooper on November 28, 2021
Outputs $ 9^k, 1 leq k leq 7 $.
v
8v
<v1
v< @
> 9*v
.^ >:
a,:|>1-
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
Answered by ovs on November 28, 2021
Answered by S.S. Anne on November 28, 2021
0
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:
Ọ
: prints null characterAnswered by L. F. on November 28, 2021
#
##
###
for
i in
range
(1, 9):
print(
'.' *i )#
Removed a line by pushing two lines into one last one, saved 11 bytes
Answered by Dion on November 28, 2021
;
t:
"*"
loop
( 9)[
print(
(( t)))
insert((
t))1 + 7]
;
s:
{t:
"*"
loop
(10)[p
rint(t)
insert(t
)"*"]}do
trim/all s
Answered by Galen Ivanov on November 28, 2021
#
##
###
for
i in
range(
9):####
print(#
10**i)###
Try it online! Similar to dingledooper's submission. I was also going to submit:
.
Try it online! But S.S.Anne beat me by a couple of hours :)
Answered by RGS on November 28, 2021
Fixed error pointed out by @S.S. Anne
m
ain
(n){
9<pr
intf(
"%dn",
n*=9)||
main(n);}
Answered by dingledooper on November 28, 2021
s
eq
-f
'seq
-s "
" %f'
$[+8]|sh
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.)
Answered by Mitchell Spector on November 28, 2021
f
()
->[
%%%%
%%%%%
string
:copies
("!", X)
++"n"||X
<- lists :
seq(1,11)].
Answered by user92069 on November 28, 2021
;
;;
int
s=1,
i;for
(;i++<
9;Print
(s*=9));
Simply prints the first several powers of 9.
Answered by the default. on November 28, 2021
Answered by user92069 on November 28, 2021
Answered by 79037662 on November 28, 2021
Boring solution, just like everyone else. :)
Non-trivial attempt.
\
2\
\ # 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
Answered by user92069 on November 28, 2021
(
;;
;;;
loop
[i 9]
(when(
* i;;;;
99999999
)(println
i)(recur(*
i 9))));;;;
Prints the first 11 powers of 9. Exits with an ArithmeticException: integer overflow
when trying to multiply $9^{12}$ by $99999999$.
Answered by math junkie on November 28, 2021
t
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
.
Answered by lyxal on November 28, 2021
B
1f
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.
Answered by Mitchell Spector on November 28, 2021
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).
Answered by Neil on November 28, 2021
`
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
Answered by math junkie on November 28, 2021
A triangle of 7 rows, filled with 0
's.
f
=(
s=`
`)=>
s[7]?
'':0+s
+f(0+s)
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
Answered by Arnauld on November 28, 2021
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
Answered by dingledooper on November 28, 2021
Z
In Pyth, Z
is initialized to 0. Output is implicit so 0 will be printed
Z
T;
T
is initialized to 10 and implicitly printed. ;
is a no-op in this context.
Answered by math junkie on November 28, 2021
#
d=
cat
d(9^
(1:7)
,sep="
")#####
Outputs $9^1$ to $9^7$:
9
81
729
6561
59049
531441
4782969
Answered by Robin Ryder on November 28, 2021
#
##
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).
Answered by RaphaelQuinet on November 28, 2021
Answered by Shaggy on November 28, 2021
u
=
(9
^)
<$>[
1..9]
main=
mapM_
print u
This prints the first 9 powers of 9.
Answered by Grain Ghost on November 28, 2021
Answered by ZaMoC on November 28, 2021
Answered by S.S. Anne on November 28, 2021
Answered by S.S. Anne on November 28, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP