The looping counter is a challenge that prints an infinite number of lines. The first line has one *, the next line has one more * than this line. An example output is:
*
**
***
****
*****
******
*******
... (goes forever)
However, the question above is too simple and is a duplicate, so let's make it harder.
For every line, instead of printing *'s, print an uppercase English letter; also, the number of characters is not increased by one, but by the ASCII value of character in this line minus 64: the first line is one A, the second line is two B's, the third line is four C's, ..., the 26-th line is 326 Z's, the 27-th line is 352 A's, ...
A
BB
CCCC
DDDDDDD
EEEEEEEEEEE
FFFFFFFFFFFFFFFF
... (goes forever)
This is code-golf, so code in the fewest bytes.
Your output can be in any place (e.g.: Standard output, files, graphics, etc.), but must follow the format.
14 Answers 14
Charcoal, 23 bytes
×ばつ⊕↨1+1%υ26§αLυD⎚⊞υLυ
Try it online! Link is to verbose version of code. Explanation:
W1«
Repeat forever.
×ばつ⊕↨1+1%υ26§αLυ
Reduce all of the members of the list modulo 26, increment each, take the sum, then increment that, and use that to repeat the current letter.
D⎚
Output the string and clear the canvas. (Normally Charcoal doesn't output until it terminates, but that's not useful for infinite output challenges.)
⊞υLυ
Push the current count to the predefined empty list.
Note that ⊕ works on empty lists in the newer version of Charcoal on ATO allowing it to replace +1 for a 1-byte saving.
If the number of lines was an input parameter and padded output was allowed then it would be possible in only 17 bytes (16 on ATO):
×ばつ⊕↨1+1%...0ι26§αι
Try it online! Link is to verbose version of code. Explanation:
N Input number
E Map over implicit range
α Predefined variable uppercase alphabet
§ Indexed by
ι Current value
×ばつ Repeated by
... Range from
0 Literal integer `0`
ι To current value
% Vectorised reduced modulo
26 Literal integer `26`
+1 Vectorised incremented
↨1 Summed
⊕ Incremented
Implicitly print
The original challenge was possible in 5 bytes:
W1«*D
Try it online! Link is to verbose version of code. Explanation:
W1«
Repeat forever.
*
Append a * to the canvas.
D
Output the current contents of the canvas.
Note that Dump is throttled on TIO, making it take 5 seconds to fill the 128KB output buffer; it's about ×ばつ faster if you Attempt This Online! with the --nt option.
-
\$\begingroup\$ @DLosc Finally got around to updating this; sorry for the delay. \$\endgroup\$Neil– Neil2024年06月08日 07:42:36 +00:00Commented Jun 8, 2024 at 7:42
Nekomata, 15 bytes
Ňr26%→Ɔ64+$∑→řH
the original challenge, 5 bytes
'*ŇPř
'*ŇPř
'* Push the character '*'
Ň Choose a natural number
P Check that it is positive
ř Repeat the character that many times
Without any command-line flags, Nekomata will print all possible results separated by newlines.
JavaScript (Node.js), 74 bytes
for(i=0,s=1;;s+=i,i%=26)console.log(String.fromCharCode(++i+64).repeat(s))
JavaScript (Node.js), 30 bytes
for(a='';;)console.log(a+='*')
Can't find a dup (削除ここまで)
Python 3, 45 bytes
a=b=1
while[print(chr(b+64)*a)]:a+=b;b=b%26+1
A full program that prints forever.
Retina 0.8.2, 46 bytes
$
@
{2=T`@L`L`(.)1円*
}2=`(.)1円*
$&1ドル
}:T`@L`LA
Try it online! Explanation:
$
@
Append an @.
2=T`@L`L`(.)1円*
Bump all of the characters up by one code point, but only in the second run of identical characters.
2=`(.)1円*
$&1ドル
Duplicate the last character, but only if there are two runs of identical characters.
{`
}`
Repeat until all of the letters are the same, at which point there is only one run of identical characters and so no substitutions are made.
:T`@L`LA
Bump all of the characters up by one code point, wrapping Z back around to A, and output the new string.
}`
Repeat forever, since the output keeps changing.
The original challenge was possible in 6 bytes:
+:`$
*
Try it online! Explanation:
+`
Repeat forever, since the output keeps changing.
:`
After performing the command, output the new value.
$
*
Append a * to the output.
This is a case where Retina 0.8.2 is golfier than Retina 1 where the * needs to be quoted with a $. (Retina 1 also uses \ instead of Retina 0.8.2's :.)
-
\$\begingroup\$ Challenge was updated, this is no longer valid \$\endgroup\$noodle person– noodle person2024年06月06日 17:21:17 +00:00Commented Jun 6, 2024 at 17:21
-
1\$\begingroup\$ @noodleman Finally got around to updating this; sorry for the delay. \$\endgroup\$Neil– Neil2024年06月08日 14:40:27 +00:00Commented Jun 8, 2024 at 14:40
C++, (削除) 113 (削除ここまで) 109 bytes
#include<iostream>
int i,p=1;int main(){while(p){std::cout<<std::string(p,char(i%26+65))<<'\n';p+=i++%26+1;}}
-4 bytes thanks to suggestions by Neil and ceilingcat.
-
1\$\begingroup\$ Moving the
i++into thep+=expression saves 2 bytes, but you can save 5 more by using aforloop. \$\endgroup\$Neil– Neil2024年06月08日 07:32:03 +00:00Commented Jun 8, 2024 at 7:32 -
1\$\begingroup\$ 88 bytes \$\endgroup\$ceilingcat– ceilingcat2024年06月13日 18:36:06 +00:00Commented Jun 13, 2024 at 18:36
Java (JDK), (削除) 88 (削除ここまで) (削除) 87 (削除ここまで) 84 bytes
v->{for(int j=1,s=1,a;;s+=j,j=j%26+1)for(a=0;a<=s;)System.out.write(a++<s?j+64:10);}
- -1 byte after a discussion with corvus_192
- -3 bytes thanks to ceilingcat
+1 credit to this answer for the idea to use System.out.write with the ternary operator and this Javascript answer for the algorithm.
-
\$\begingroup\$ -2 bytes:
j%=26,s+=j++\$\endgroup\$corvus_192– corvus_1922024年06月07日 15:46:10 +00:00Commented Jun 7, 2024 at 15:46 -
\$\begingroup\$ @corvus_192 Impossible, if we put modulo operation before the assignment of
s, we are gettings=s+j%26instead ofs=s+j. This gives incorrect string lengthes after the 26th letter (Z). But this 1-byte reduction can still be donej=j%26+1;) \$\endgroup\$Glory2Ukraine– Glory2Ukraine2024年06月07日 18:21:13 +00:00Commented Jun 7, 2024 at 18:21
Perl 5 + -M5.10.0 -061, 38 bytes
$/+=$-,$-%=26while say+(A..Z)[$-++]x$/
Explanation
-061 sets $/ to 1 which starts our repetition counter where we need it.
while we say (always returns truthy) the letter of the alphabet, at index $- (starts at 0 and post-incremented), repeated $/ times, we increment $/ by $-, then set $- to itself, modulo 26.
05AB1E, 14 bytes
1[DN&g×ばつ,N2%>+
Or alternatively:
AuÞ∞<2%>.\&g×ばつ,ドル
Original closed challenge (5 bytes):
'*[=Ć
Explanation:
1 # Start with 1
[ # Loop indefinitely:
D # Duplicate the current value
N> # Push the 1-based loop-index
.b # Convert it to an uppercase character (1→A,2→B,...,26→Z,27→A,...)
×ばつ # Repeat that letter the duplicated value amount of times
, # Pop and print it with trailing newline
N # Push the 0-based loop index
2% # Modulo-26
> # Increase it by 1
+ # Increase the current value by this for the next iteration
A # Push the lowercase alphabet
u # Uppercase it
Þ # Convert it to a list of characters, and cycle indefinitely
∞ # Push an infinite positive list: [1,2,3,...]
< # Decrease by 1 to make it non-negative: [0,1,2,...]
2% # Modulo-26 each
> # Then increase each back by 1
.\ # Prepend 0 and undelta
> # Increase each by ×ばつ # Repeat the letters those values amount of times
€ # Loop over each string:
, # Output it with trailing newline
'* '# Push "*"
[ # Loop indefinitely:
= # Print the current string with trailing newline (without popping)
Ć # Enclose; append its own first character
Google Sheets, 103 bytes
To be entered in A1:
=LET(R,LAMBDA(R,a,b,i,l,IF(i=ROWS(A:A),l,R(R,a+b,MOD(b,26)+1,i+1,{l;REPT(CHAR(b+64),a)}))),R(R,1,1,1,))
The formula prints until the last row of the sheet. It breaks after a few thousand rows because of the 32k max char limitation of the REPT function.
Arturo, (削除) 58 (削除ここまで) 57 bytes
s:1i:0whileø->loop'A'..'Z'=>['s+i,[email protected]:s&|print'i+1]
Explore related questions
See similar questions with these tags.
n=0;n+=1ton='';n+='*'\$\endgroup\$