28
\$\begingroup\$

Task

Your task is to print this exact text:

A
BCD
EFGHI
JKLMNOP
QRSTUVWXY
ZABCDEFGHIJ
KLMNOPQRSTUVW
XYZABCDEFGHIJKL
MNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLM
NOPQRSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL
MNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXY
ZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOP
QRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI
JKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCD
EFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA
BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ

Specs

  • You may do it in all-lowercase instead of all-uppercase.
  • Trailing newlines at the end of the triangle is allowed.
  • Trailing spaces after each line is allowed.
  • You must print to STDOUT instead of outputting an array of strings.

Scoring

This is . Program with lowest byte-count wins.

emanresu A
46.2k5 gold badges111 silver badges257 bronze badges
asked Aug 21, 2016 at 11:07
\$\endgroup\$
5
  • 1
    \$\begingroup\$ What do you mean by "strikes again"? Was there another challenge you made like this? \$\endgroup\$ Commented Aug 21, 2016 at 13:46
  • \$\begingroup\$ @Peanut codegolf.stackexchange.com/questions/87496/alphabet-triangle \$\endgroup\$ Commented Aug 21, 2016 at 13:58
  • 1
    \$\begingroup\$ Seems fairly trivial do we really need (another) alphabet challenge? \$\endgroup\$ Commented Aug 21, 2016 at 21:44
  • 2
    \$\begingroup\$ It is a good challenge, but I think we have outstripped saturation of these alphabet challenges, nothing personal. \$\endgroup\$ Commented Aug 21, 2016 at 21:45
  • \$\begingroup\$ Actually looking for an alphabet challenge that the letter at a position cannot be calculated by simple expressions from its coordinates involving the mod function. May make one myself if I have time. \$\endgroup\$ Commented Jan 31, 2018 at 20:17

61 Answers 61

1
\$\begingroup\$

C, 60 bytes

i;main(j){for(;j<27;j*j^++i||puts("",j++))putchar(i%26+65);}
answered Aug 22, 2016 at 23:59
\$\endgroup\$
2
  • \$\begingroup\$ puts only takes one argument. (Some undefined behaviour is permitted in codegolf normally but this is a bit too far outside the usual lanes) \$\endgroup\$ Commented Aug 23, 2016 at 1:13
  • \$\begingroup\$ @M.M Undefined behaviour is exploited all the time. The rule is that a submission is valid as long as it works in some compiler, otherwise we would have to explicitly rule out a long list of exceptions. This code works in gcc so it's a valid submission. \$\endgroup\$ Commented Aug 23, 2016 at 1:38
1
\$\begingroup\$

C++, 111 bytes

void a(){int c=65,i,j;for(i=0;i<26;i++){for(j=0;j<=2*i;j++){std::cout<<(char)c;c++;if(c==91)c=65;}std::cout<<'\n';}}

First try at one of these. Uses an int "c" to record which letter it needs to print at any given time. Once "c" passes 90 ('Z') it gets reset to 65 ('A'). Prints the pyramid using for loops.

\$\endgroup\$
2
  • \$\begingroup\$ Nice answer! You could do if(c<92)c=65 to take one byte off, and you might also be able to do int a() instead of void a(), but I'm not positive if that works without the return. Other than that, I think you need to include #include <iostream> in your byte count. \$\endgroup\$ Commented Aug 23, 2016 at 6:05
  • \$\begingroup\$ I believe you meant if(c>90)c=65, but thanks for the suggestion, it's a good idea. Also, I guess I'll include it, thanks. \$\endgroup\$ Commented Aug 26, 2016 at 8:02
1
\$\begingroup\$

PHP, (削除) 76 (削除ここまで) 69 bytes

for(;$i<26&&$a.=join(range(A,Z));)echo substr($a,$i**2,1+2*$i++)."
";
  • Create 26 alphabet (more than enough) and contactenate them in $a
  • loop for i < 26
  • display $a substring start i^2, end 2*i+1
answered Aug 23, 2016 at 6:42
\$\endgroup\$
1
\$\begingroup\$

C++, (削除) 122 (削除ここまで) 108 bytes

void a(){int a=65,l,i;for(l=1;l<53;l+=2){for(i=0;i<l;i++){std::putchar(a);a>90?a=65:a++;}std::cout<<'\n';}}

First time trying my hand at one of these, so it'll probably be super inefficient.

It creates an integer a, then enters a for loop with l representing the number of characters in a line (stopping when reaching the maximum amount, 53) Inside the loop, it enters another loop based on the value of l, which prints a to the console, then either increments it or sets it back to A. After completing that loop, it prints a newline to extend the pyramid.

EDIT: Improved by changing a from a char to an int, which eliminates the need for (int)a++ among other things and saves precious bytes. Also replaced the while statement with an equivalent for.

\$\endgroup\$
1
  • \$\begingroup\$ Hi, and welcome to PPCG! Nice first post! \$\endgroup\$ Commented Aug 23, 2016 at 2:00
1
\$\begingroup\$

Perl 6: 51 bytes

say .join for (|("A".."Z")xx*).rotor(1,3...51)[^26]
answered Aug 23, 2016 at 14:59
\$\endgroup\$
1
  • \$\begingroup\$ 47 bytes \$\endgroup\$ Commented Aug 4, 2024 at 21:09
1
\$\begingroup\$

Perl 6: 50 bytes

Regex based solution:

.put for (("A".."Z").join x 26)~~m:g/.**{1+2*$++}/
answered Aug 23, 2016 at 15:26
\$\endgroup\$
1
\$\begingroup\$

C#, 153 Bytes

using b=System.Console;class a{static void Main(){int i=0,j=0,k=0;while(i<26){b.Write((char)(65+j));j++;j%=26;k++;if(2*i+1==k){b.WriteLine();i++;k=0;}}}}

Compile using Microsoft .NET Framework 2.0 or later. (Written using Visual Studio 2015 Update 3.)

answered Aug 23, 2016 at 21:29
\$\endgroup\$
1
\$\begingroup\$

Dyalog APL, (削除) 31 (削除ここまで) (削除) 25 (削除ここまで) 23 bytes

Requires ⎕IO←0, which is default on many systems.

↑(∊1, ̈0⍴ ×ばつ⍳26)⊂676⍴⎕A

676⍴⎕A cycle the letters to get 676 of them

(...)⊂ split on ones in

⍳26 zero through 25; {0, 1, 2, 3, ..., 25}

×ばつ multiply them by 2, {0, 2, 4, 6, ..., 50}

0⍴ ̈⍨ generate zero-sequences of such lengths; {}, {0}, {0, 0}, etc.

1, ̈ prepend one to each sequence; {1}, {1, 0}, {1, 0, 0}, etc.

flatten; {1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, ..., 0}

stack the strings

TryAPL online!

-6 thanks to @jimmy23013

answered Aug 21, 2016 at 13:28
\$\endgroup\$
2
  • \$\begingroup\$ 26 51⍴(,↑1⍴¨⍨¯1+2×⍳26)676円⍴⎕A \$\endgroup\$ Commented Aug 22, 2016 at 13:29
  • \$\begingroup\$ ↑(∊1,1,¨0⍴¨⍨2×⍳25)⊂676⍴⎕A \$\endgroup\$ Commented Aug 22, 2016 at 13:39
1
\$\begingroup\$

Actually, (削除) 27 (削除ここまで) (削除) 26 (削除ここまで) (削除) 22 (削除ここまで) (削除) 20 (削除ここまで) 17 bytes

Takes the lowercase alphabet 26 times, prints 1,3,5,...,(2*n+1) at a time. Golfing suggestions welcome. Try it online!

Edit: Many thanks and -4 bytes to Leaky Nun and his suggestions.

úlú*iúl`╜u╟Σ)2╖`n

Ungolfing:

úl Pushes 26 (the length of the lowercase alphabet).
ú* 26 * the lowercase alphabet.
i Flatten the string.
úl Pushes 26.
 ` Start function.
 ╜u Push register 0 (default: 0) and increment (call it len, from now on)
 ╟ Take len elements from the stack
 Σ sum() into a string.
 ) Rotate the string to the bottom of the stack.
 2╖ Add 2 to register 0.
 ` End function.
n Run the above function (26) times.
answered Aug 21, 2016 at 18:22
\$\endgroup\$
0
1
\$\begingroup\$

Racket 160 bytes

(let p((n 65)(t 0)(x 0))(when(> n 90)(set! n 65))(when(> x t)(set! t(+ 2 t))
(set! x 0)(displayln""))(when(< t 51)(display(integer->char n))(p(+ 1 n)t(+ 1 x))))

Ungolfed:

(define (f)
 (let loop ((n 65)
 (t 0)
 (x 0))
 (when (> n 90)
 (set! n 65))
 (when (> x t)
 (set! t (+ 2 t))
 (set! x 0)
 (displayln ""))
 (when (< t 51)
 (display (integer->char n))
 (loop (add1 n) 
 t 
 (add1 x)))))

Testing:

(f)

Output:

A
BCD
EFGHI
JKLMNOP
QRSTUVWXY
ZABCDEFGHIJ
KLMNOPQRSTUVW
XYZABCDEFGHIJKL
MNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLM
NOPQRSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL
MNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXY
ZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOP
QRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI
JKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCD
EFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA
BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ
answered Oct 30, 2016 at 16:45
\$\endgroup\$
1
\$\begingroup\$

05AB1E, (削除) 14 (削除ここまで) (削除) 13 (削除ここまで) 11 bytes

×ばつ26L·<£»

Try it online!

Explanation:

×ばつ # Push the alphabet repeated 27 times
 26L # Push [1, 2, ..., 26]
 · # Multiply by 2 [2, 4, ..., 52]
 < # Subtract 1 [1, 3, ..., 51]
 £ # Repeated slice
 » # Join by newlines
 # Implicit print
answered Oct 30, 2016 at 17:42
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Using external library - Enumerable) ((削除) 90 (削除ここまで) (削除) 88 (削除ここまで) (削除) 86 (削除ここまで) 80 bytes)

Link to lib: https://github.com/mvegh1/Enumerable

(z=_.Range)(0,26).WriteLine(x=>z(x*x,2*x+1).Write("",y=>(y%26+10).toString(36)))

Code explanation: Create range of ints 0 to 25, and for each write a new line according to the predicate. For each line, create a range of ints starting at the square of the current int, for a count of 2x+1. Then take that range, and join it into a string delimited by nothing where each int is casted to the String represented by the int ascii code

enter image description here

answered Aug 21, 2016 at 19:18
\$\endgroup\$
8
  • \$\begingroup\$ I highly doubt that the javascript answer without the library would be longer than this. \$\endgroup\$ Commented Aug 21, 2016 at 19:49
  • \$\begingroup\$ Exactly. \$\endgroup\$ Commented Aug 21, 2016 at 19:52
  • \$\begingroup\$ Ill have to optimize my answer ;) \$\endgroup\$ Commented Aug 21, 2016 at 20:22
  • \$\begingroup\$ 86 bytes, bring it on vanilla JS! \$\endgroup\$ Commented Aug 21, 2016 at 21:27
  • \$\begingroup\$ Well, I already had borrowed your %26. Now can you do better? \$\endgroup\$ Commented Aug 22, 2016 at 13:06
1
\$\begingroup\$

Common Lisp, 139 bytes

(let((a"abcdefghijklmnopqrstuvwxyz"))(do((n 3(+ n 2))(i 0 j)(j 1(+ j n))(b a(format()"~a~a"b a)))((= n 55))(format t"~a~%"(subseq b i j))))

Try it online!

answered Aug 14, 2017 at 10:13
\$\endgroup\$
1
\$\begingroup\$

Yabasic, 63 bytes

Long live BASIC.

For J=1To 51
For K=1To J
I=Mod(I,26)+1
?Chr$(64+I);
Next
?
Next

Try it online!

answered Jan 31, 2018 at 20:02
\$\endgroup\$
1
\$\begingroup\$

K4, 25 bytes

Solution:

-1(0,+1円+2*!25)_676#.Q.A;

Example:

q)k)-1(0,+1円+2*!25)_676#.Q.A;
A
BCD
EFGHI
JKLMNOP
QRSTUVWXY
ZABCDEFGHIJ
KLMNOPQRSTUVW
XYZABCDEFGHIJKL
MNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLM
NOPQRSTUVWXYZABCDEFGHIJKLMN
OPQRSTUVWXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL
MNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXY
ZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOP
QRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI
JKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCD
EFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA
BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ

Explanation:

Slice up a list of the alphabet repeated 26 times...

-1(0,+1円+2*!25)_676#.Q.A; / the solution
-1 ; / print to STDOUT and swallow return
 .Q.A / uppercase alphabet A-Z
 676# / 676 take from the alphabet
 _ / cut at indexes given by left
 ( ) / do this stuff together
 !25 / range 0..24
 2* / multiply by 2
 1+ / add 1
 +\ / calculate sums of list
 0, / prepend 0
answered Jan 31, 2018 at 22:28
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 9 bytes

×ばつā·<£»

Try it online!

Outputs a zillion trailing newlines, but whatever.

answered Jun 2, 2022 at 19:09
\$\endgroup\$
1
\$\begingroup\$

Vyxal j, 9 bytes

kA5ẋ:żd‹ẇ

Try it Online!

How?

kA5ẋ:żd‹ẇ
kA # Push the uppercase alphabet
 5 # Push the length without popping (26)
 ẋ # Repeat it that many times
 :ż # Duplicate and get range [1, length]
 d # Double each
 ‹ # Decrement each
 ẇ # Split the repeated alphabet into chunks of that sizez
answered Jun 2, 2022 at 19:06
\$\endgroup\$
0
\$\begingroup\$

Pyth, 14 bytes

V26:*26G^N2^hN2

Try it here!

V26 - for N in range(26):
 ^N2 - N**2
 :*26G - (alphabet*26)[^:V]
 ^hN2 - (N+1)**2
answered Aug 21, 2016 at 12:07
\$\endgroup\$
0
0
\$\begingroup\$

Java, 109 bytes

for(int i=0,c=-1,j;i<26;i++,System.out.println())for(j=0;j<1+2*i;j++)System.out.print((char)(65+(c=++c%26)));

Slightly ungolfed:

for (int i = 0, c = -1, j; i < 26; i++, System.out.println())
 for (j = 0; j < 1 + 2 * i; j++) System.out.print((char) (65 + (c = ++c % 26)));
answered Aug 21, 2016 at 18:22
\$\endgroup\$
1
  • \$\begingroup\$ 99 bytes \$\endgroup\$ Commented Jul 19, 2021 at 5:45
0
\$\begingroup\$

Actually, (削除) 18 (削除ここまで) 17 bytes

úl`╜21╖╜2;ú*Ht`na

Ungolfing:

úl Pushes 26 (the length of the lowercase alphabet).
 ` Start function
 ╜2 Push register 0 (default: 0) and square. Call this a.
 1╖ Increment register 0.
 ╜2; Push register 0, square and duplicate. Call this b.
 ú* b times the lowercase alphabet. Call this repeated_alphabet.
 Ht Get repeated_alphabet[:b][a:]
 ` End function.
n Run the above function (26) times.
a Invert the stack.
 Print the stack implicitly with newlines.

Try it online!

Sherlock9
12.4k1 gold badge31 silver badges68 bronze badges
answered Aug 21, 2016 at 18:48
\$\endgroup\$
0
\$\begingroup\$

><>, 59 bytes

<v01'5'&0
+> :@@:@$-?!v$&:d2*%'A'+o1+&1
 ^0;?=}:{:+2oa~<

This increments one item for each letter, another for how many letters in the current row, and checks that one to a constant to halt the program after the last line of 53 characters

Try it online

answered Aug 23, 2016 at 0:27
\$\endgroup\$
0
\$\begingroup\$

Python 3, 83 bytes

[print("".join([chr((i%26)+97)for i in range(j*j,(j+1)*(j+1))]))for j in range(26)]

My first golfing experience.

answered Aug 24, 2016 at 9:57
\$\endgroup\$
0
\$\begingroup\$

Matlab, 81 bytes

b=char('A'+(1:26)-1)';b=repmat(b,1,26);
for j=1:26
disp(b((j-1)*(j-1)+1:j*j))
end
answered Aug 24, 2016 at 13:02
\$\endgroup\$
0
\$\begingroup\$

Pip, (削除) 17 (削除ここまで) 16 bytes

15 bytes of code, +1 for -n flag.

zX26^@(1,27)**2

Try it online!

Explanation:

zX26 Lowercase alphabet, repeated 26 times
 ^@ Split at these indices:
 1,27 Range(1,27), i.e. numbers 1 through 26
 ( )**2 Square each number
 Autoprint the resulting list, joined on newlines due to -n

My first solution is longer, but I think it's pretty cute:

L26P(zy,Y++++v+y)

Explanation:

L26 Loop 26 times:
 y y is initially "", which is 0 in numeric context
 , Range from y to the following value:
 ++++v v is initially -1; increment it twice (so it goes 1, 3, 5, ...)
 Y +y Add previous y to new v and yank result into y for next iteration
 (z ) Use that range to get a slice from the lowercase alphabet (with
 cyclical indexing)
 P Print the slice (with trailing newline)
answered Oct 31, 2016 at 5:05
\$\endgroup\$
0
\$\begingroup\$

uBASIC, 68 bytes

Anonymous function that takes no input and outputs to the console.

That pesky Chr$(...) function is forcing me to use Left$(...,1) again.

0ForI=1To51:ForJ=1ToI:K=KMod26+1:?Left$(Chr$(64+K),1);:NextJ:?:NextI

Try it online!

answered Jan 31, 2018 at 20:06
\$\endgroup\$
0
\$\begingroup\$

Visual Basic .NET (Mono), 136 bytes

Declared Subroutine that takes no input and outputs to the console.

Module M
Sub Main
Dim I,J,K,S
For J=1To 51
S=""
For K=1To J
I=I Mod 26+1
S+=Chr$(64+I)
Next
Console.WriteLine(S)
Next
End Sub
End Module

Try it online!

answered Jan 31, 2018 at 20:10
\$\endgroup\$
0
\$\begingroup\$

Setanta, (削除) 104 (削除ここまで) 93 bytes

s:="ABCDEFGHIJKLMNOPQRSTUVWXYZ"*26le i idir(0,26){i=2*i+1scriobh(cuid@s(0,i))s=cuid@s(i,999)}

try-setanta.ie link

answered Aug 4, 2024 at 20:59
\$\endgroup\$
0
\$\begingroup\$

Red, (削除) 86 (削除ここまで) 78 bytes

a:""repeat k 26[print take/part append a"ABCDEFGHIJKLMNOPQRSTUVWXYZ"k * 2 - 1]

Try it online!

answered Sep 27, 2024 at 6:42
\$\endgroup\$
0
\$\begingroup\$

AWK, 58 bytes

END{for(;i++<51;print)for(j=i++;j--;)printf"%c",x++%26+65}

Attempt This Online!

answered Nov 4 at 18:56
\$\endgroup\$
0
\$\begingroup\$

Tcl, 97 bytes

time {puts -nonewline [format %c [expr ([incr i]-1)%26+65]]
if int(sqrt($i))**2==$i puts\ ""} 676

Try it online!


Tcl, 100 bytes

Requires Tcl >= 8.7 to allow use of lseq
lmap i [lseq 676] {puts -nonewline [format %c [expr $i%26+65]]
if int(sqrt($i+1))**2==$i+1 puts\ ""}

Attempt This Online!

answered Nov 4 at 17:49
\$\endgroup\$
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.