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 code-golf. Program with lowest byte-count wins.
61 Answers 61
C, 60 bytes
i;main(j){for(;j<27;j*j^++i||puts("",j++))putchar(i%26+65);}
-
\$\begingroup\$
putsonly takes one argument. (Some undefined behaviour is permitted in codegolf normally but this is a bit too far outside the usual lanes) \$\endgroup\$M.M– M.M2016年08月23日 01:13:31 +00:00Commented 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\$xsot– xsot2016年08月23日 01:38:14 +00:00Commented Aug 23, 2016 at 1:38
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.
-
\$\begingroup\$ Nice answer! You could do
if(c<92)c=65to take one byte off, and you might also be able to doint a()instead ofvoid 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\$DJMcMayhem– DJMcMayhem2016年08月23日 06:05:06 +00:00Commented 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\$limelier– limelier2016年08月26日 08:02:10 +00:00Commented Aug 26, 2016 at 8:02
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
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.
-
\$\begingroup\$ Hi, and welcome to PPCG! Nice first post! \$\endgroup\$Riker– Riker2016年08月23日 02:00:29 +00:00Commented Aug 23, 2016 at 2:00
Perl 6: 51 bytes
say .join for (|("A".."Z")xx*).rotor(1,3...51)[^26]
Perl 6: 50 bytes
Regex based solution:
.put for (("A".."Z").join x 26)~~m:g/.**{1+2*$++}/
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.)
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
-6 thanks to @jimmy23013
-
\$\begingroup\$
26 51⍴(,↑1⍴¨⍨¯1+2×⍳26)676円⍴⎕A\$\endgroup\$jimmy23013– jimmy230132016年08月22日 13:29:30 +00:00Commented Aug 22, 2016 at 13:29 -
\$\begingroup\$
↑(∊1,1,¨0⍴¨⍨2×⍳25)⊂676⍴⎕A\$\endgroup\$jimmy23013– jimmy230132016年08月22日 13:39:52 +00:00Commented Aug 22, 2016 at 13:39
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.
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
05AB1E, (削除) 14 (削除ここまで) (削除) 13 (削除ここまで) 11 bytes
×ばつ26L·<£»
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
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
-
\$\begingroup\$ I highly doubt that the javascript answer without the library would be longer than this. \$\endgroup\$Leaky Nun– Leaky Nun2016年08月21日 19:49:45 +00:00Commented Aug 21, 2016 at 19:49
-
-
\$\begingroup\$ Ill have to optimize my answer ;) \$\endgroup\$applejacks01– applejacks012016年08月21日 20:22:51 +00:00Commented Aug 21, 2016 at 20:22
-
\$\begingroup\$ 86 bytes, bring it on vanilla JS! \$\endgroup\$applejacks01– applejacks012016年08月21日 21:27:37 +00:00Commented Aug 21, 2016 at 21:27
-
\$\begingroup\$ Well, I already had borrowed your %26. Now can you do better? \$\endgroup\$edc65– edc652016年08月22日 13:06:37 +00:00Commented Aug 22, 2016 at 13:06
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))))
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
Vyxal j, 9 bytes
kA5ẋ:żd‹ẇ
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
Pyth, 14 bytes
V26:*26G^N2^hN2
V26 - for N in range(26):
^N2 - N**2
:*26G - (alphabet*26)[^:V]
^hN2 - (N+1)**2
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)));
-
\$\begingroup\$ 99 bytes \$\endgroup\$ceilingcat– ceilingcat2021年07月19日 05:45:06 +00:00Commented Jul 19, 2021 at 5:45
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.
><>, 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
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.
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
Pip, (削除) 17 (削除ここまで) 16 bytes
15 bytes of code, +1 for -n flag.
zX26^@(1,27)**2
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)
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
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
Setanta, (削除) 104 (削除ここまで) 93 bytes
s:="ABCDEFGHIJKLMNOPQRSTUVWXYZ"*26le i idir(0,26){i=2*i+1scriobh(cuid@s(0,i))s=cuid@s(i,999)}
Red, (削除) 86 (削除ここまで) 78 bytes
a:""repeat k 26[print take/part append a"ABCDEFGHIJKLMNOPQRSTUVWXYZ"k * 2 - 1]
Tcl, 97 bytes
time {puts -nonewline [format %c [expr ([incr i]-1)%26+65]]
if int(sqrt($i))**2==$i puts\ ""} 676
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\ ""}
-
\$\begingroup\$ Failed outgolf: tio.run/##K0nO@f@/JDM3VaE6saAgNS9FoVghOi2/… \$\endgroup\$sergiol– sergiol2025年11月04日 18:34:32 +00:00Commented Nov 4 at 18:34
-
Explore related questions
See similar questions with these tags.
modfunction. May make one myself if I have time. \$\endgroup\$