# Background:

A sequence of infinite naturals is a sequence that contains every natural number infinitely many times.

To clarify, every number must be printed multiple times!

# The Challenge:

Output a sequence of infinite naturals with the shortest code.

# Rules:

1. Each number must be separated by a (finite) amount of visible, whitespace or new line characters that aren't a digit.
2. The program cannot terminate (unless you somehow wrote all numbers).
3. Any way of writing such a sequence is acceptable.

# Examples:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
...


1, 1, 2, 1, 2, 3, 1, 2, 3, 4...

Notice that we write all naturals from 1 to N for all N ∈ ℕ.

Feedback and edits to the question are welcome. Inspired by my Calculus exam.

• Welcome to the site! This is an interesting question, and a nice first attempt. In the future, we recommend using the Sandbox to get feedback before posting to main. There are a couple of clarifications needed here. It took me a few rereads to understand that each number has to appear multiple times, so I'd recommend rewording that to made it clearer. Also, I'd be surprised if this isn't a duplicate of an existing challenge, so don't be discouraged if this is closed as a duplicate (+1 if not however) Nov 30, 2020 at 17:57
• Are nested lists and/or non-natural numbers permitted? Nov 30, 2020 at 18:28
• @UnrelatedString nested lists yes, I will edit the post but non-natural numbers are not permitted. Nov 30, 2020 at 18:30
• Is it OK if the output is in random order (as long as all numbers are printed infinitely often with probability 1)? Nov 30, 2020 at 18:39
• Is it acceptable if floating-point limitations are ignored? For example, if $r$ is a uniformly distributed random float on $(0,1)$, can we assume $1/r$ can be arbitrarily large? (it can in theory, but not in practice due to realmin) Nov 30, 2020 at 19:03

# Java (JDK), 61 bytes

v->{for(int i,j=2;;j++)for(i=0;++i<j;)System.out.println(i);}


Try it online!

Edit: Thanks @user for shaving off a few bytes and helping me learn something today! Big thanks to @KevinCruijssen for -2 bytes.

• Nice answer! 67 bytes by using newlines and rearranging the loop. I don't know if you need a proper method for this one. You could also use a Runnable.
– user
Nov 30, 2020 at 21:44
• 61 bytes by changing the ()-> to v-> (empty unused parameters are allowed by default) and putting the int i,j=2; inside the loop to save on the ;. Dec 2, 2020 at 15:40
• Here's my 47 byte solution. I made a new post because the method approach was different. Dec 3, 2020 at 1:08

# Rust + rand crate, 43 bytes

||loop{print!("{} ",rand::random::<u32>())}


Uses the rand crate to output random numbers, meaning every number is printed infinitely many times.

• Welcome to the site. I know nothing about Rust, but when I run your code it fails with an error: Try it online! Dec 1, 2020 at 9:37
• That lines belongs into the Cargo.toml file, which defines the dependencies. Not sure how it works with that site Dec 1, 2020 at 9:57
• If you require the Cargo.toml file for your code I think you have to count the entire file. A better solution would be to use the language Rust + rand crate and just remove the rand="0".
– ovs
Dec 1, 2020 at 15:12
• And we require solutions on this site to be either full program or a function. In this a closure would be the shortest: ||loop{print!("{} ",rand::random::<u32>())}
– ovs
Dec 1, 2020 at 15:14
• @ovs thanks for the info, fixed the two issues you brought up Dec 3, 2020 at 8:52

# Zsh, 29 .. 19 bytes

Solution by @AdamKatz: Try it Online!

for ((;++i;))seq $i  19 bytes, port from bash : s(){seq$[++n];s};s
25 bytes (per @AdamKatz) : for ((;++i;))echo {1..$i} 25 bytes : for ((;;i++))shuf -i 1-$i
26 bytes (per @AdamKatz) : for ((;;))echo {1..$[++i]} 29 bytes : for ((i=1;;))echo {1..$[i++]}
I tried to use /dev/random for an alternative but it was a mess!

• Nice, that's fully native and without subshells! for ((;;))echo {1..$[++i]} saves you 3B by assuming i is either undefined or 0. Try it online Dec 3, 2020 at 20:48 • Not any more. Saved a byte using shuf from coreutils Dec 8, 2020 at 15:48 • cool, that gives me a way to get the native subshell-free version down to 25B as well: for ((;++i;))echo {1..$i} Try it online Dec 8, 2020 at 17:01
• If you don't care about white space (this has a newline delimiter, printing each number on its own line), you can do for ((;++i;))seq $i at 19B. Try it online Dec 8, 2020 at 17:08 • Thanks, will update this soon. Another one for 19B: s(){seq$[++n];s};s Dec 8, 2020 at 17:34

say while$_.=++$i.$"  Try it online! # Rust, 50 bytes ||for i in(1..).flat_map(|n|1..n){print!("{} ",i)}  Try it online! I know there are a couple of rust answers already, but this one is deterministic and takes a different (and shorter) approach to the problem. explanation: ||for i in(1..).flat_map(|n|1..n){print!("{} ",i)} //anonymous function (1..) //for evey n from 1..infinite .flat_map(|n|1..n) //map to range from 1..n and flatten for i in {print!("{} ",i)} //print with a trailing space  # AWK, 34 bytes {for(;;++i)for(j=0;j++<i;)print j}  Try it online! # APL (Dyalog Unicode), 12 11 bytes (SBCS) Saved 1 byte thanks to @ovs {∇1+⍴⎕←⍳⍵}1  Try it online! This one also uses the sequence from the question. • 11 bytes by using the return value of ←. – ovs Nov 30, 2020 at 23:14 • @ovs Thanks. I'd tried reversing and then picking the first element, which wasn't any shorter, but I hadn't thought of using rho to get the number. – user Dec 1, 2020 at 0:04 # PHP, 30 bytes <?for(;;)echo--$j?:$j=++$i,~_;


Try it online!

The numbers are separated by a no-break space (U+00A0).

• I think you can omit the <? part, it's required to count it when it's <?= because it's an alias for echo, but that's not the case here. We don't ask browser javascript to count the <script> tags after all Dec 2, 2020 at 8:22
• @Kaddath Notice that that's true for answers meant to be executed with the CLI interface (E.g.: php -r). For answers written for a web server and you need to write PHP, you will need the opening tags. Dec 2, 2020 at 10:44
• @IsmaelMiguel yes you're right, I tend to assimilate one to the other because so far I have never seen a question here where requisites directly or indirectly makes you cannot use the CLI interface Dec 2, 2020 at 13:56
• @Kaddath Sometimes, if you want to use PHP 4.1, you can just say that the input comes from POST, GET or COOKIE and use a webserver running that version of PHP, since the keys of those variables will be available into variables (say, a cookie called "abc" will create a variable called $abc). This can help to save a fair bit of code, on some situations. In this case, you are required to use <?. In the case of php -r, you are required to do NOT use <?. If you use php -f (if needed), you have to use <? too. But yes, in this case, php -r is the safest option and saves 2 bytes. Dec 2, 2020 at 15:27 # Stax, 5 bytes VImRJ  Run and debug it ## Explanation VImRJ VIm map 1..infinity to R range 1..i J join with spaces m print each iteration  • If you were a few hours earlier, I could have up voted this. Now I have to wait 24 hours. Very sad. Dec 1, 2020 at 2:19 # JavaScript (V8), 34 bytes Saved 2 bytes thanks to @tsh for(n=k=0;;)print(n=n<k?n+1:++k/k)  Try it online! Returning the n-th term would be 31 bytes: n=>(m=(8*n+1)**.5-1>>1)*~m/2-~n  Try it online! • 34 bytes: for(n=k=0;;)print(n=n<k?n+1:++k/k) – tsh Dec 1, 2020 at 1:55 • 29 bytes: for(n=k=1;;)print(n=--n||k++) Dec 1, 2020 at 2:16 • @Sisyphus I now realize that I've completely misunderstood this challenge. (I thought we were required to output A002260 and nothing else.) I think you should post your version separately. Dec 1, 2020 at 2:23 • @Arnauld Ah, I see. I've posted mine then. Dec 1, 2020 at 2:29 # Wolfram Language (Mathematica), 31 bytes Print@Floor[Sec@n^2]~Do~{n,∞}  Try it online! Not quite as short as the other Mathematica answer, but implements a different and more mathematically interesting sequence, namely the integer part of $$\\sec^2n\$$; the fact that this includes every natural number infinitely often follows from the fact that the integers are uniformly distributed modulo $$\\pi\$$. • 30 bytes – att Dec 24, 2020 at 19:02 # Icon, 46 bytes procedure f() i:=0;|1&write(1to(i+:=1))&\z end  Try it online! Of course procedure ... end block makes it very verbose. Too bad seq() doesn't work with bigints - otherwise I could've saved some bytes with procedure f() |1&write(1to seq())&\z end  # Julia, 27 bytes f(n=1)=println.(1:n),f(n+1)  Try it online! # Burlesque, 6 bytes ,r1)ro  Try it online! Explanation: , # Don't take implicit input from stdin r1 # Range from 1 to infinity ) # Map each value to ro # Range from 1 to value  # Java (JDK), 54 bytes A b;{b=(s,i)->{System.out.print(s+=i+++s);b.B(s,i);};}  Try it online! Usually in lambda functions, this website doesn't include the "interface name =" part, but my answer was a declaration statement without the initialization, so I decided to include it. I couldn't find a rule about it. My older, wrongly counted, 47 byte solution: Try it online! (forced) inputs: a String with any delimiter (" "), and 1, where Y is the class it's in, b is the name of the interface object, and B is the method in the interface being used. • Good Solution! However, I'm not sure if this would qualify because it's output must be in chronological order. Yours: 1 1 2 1 1 2 1 3 1 2 1 Ex: 1 1 2 1 2 3 Dec 3, 2020 at 13:17 • @DMiddendorf I don't think the output has to be sorted – user Dec 3, 2020 at 14:52 • However, since you're using stuff outside your code (Y.b.B), you do need to add that to your byte count, @branboyer, in which case you can just use a plain method. – user Dec 3, 2020 at 14:53 • @DMiddendorf I thought of that too, but "...a sequence that contains every natural number infinitely many times" doesn't necessarily need to be in order as long as it somehow gets every number, and OP's example was out of order too. Dec 3, 2020 at 17:26 • @user, Thanks for the advice, but my method will not work if I do not include all that Y.b.B because of this special thing where recursive lamba functions only work under certain conditions, which is that it needs to be static and called in this exact way, but perhaps the rule conventions allow it somehow. Dec 3, 2020 at 17:28 ## Bash, 21 bytes s(){ seq$[++n];s;};s

1
1
2
1
2
3
…


Run the 21B version on Try It Online

This defines a function s that runs seq NUMBER where NUMBER starts at 1 and increments with each run, then it runs itself recursively. After the definition, we run s.

For the cost of 5B to specify -s\  (separator is a space character), it can be adapted to a one answer per line solution at 26 bytes:

s(){ seq -s\  $[++n];s;};s  1 1 2 1 2 3 …  Run the 26B version on Try It Online • BTW, zsh can do this in 19B as s(){seq$[++n];s};s Dec 8, 2020 at 17:44

# jq, 24 bytes

range(range(infinite))+1


Try it online! (requires the -n flag)

JQ is a stream-based language: every expression can return multiple values and when expressions are combined together the second one is flat-mapped over everything returned by the first. This makes it very easy to express infinite sequences.

infinite is a filter that ignores its input and returns a single infinite floating-point value.
range(n) is a filter that ignores its input and returns every positive integer smaller than n.
+1 adds one to each value in the result.

By default if we run this program it will try to read from STDIN (because each filter needs to take an input, even if it is ignored). Therefore we pass the -n flag to tell the interpreter to pass a single null value as the input to the filter.

# Julia, 47 46 bytes

Thanks to @user recommended to change true to 1>0.

a=[1];while 1>0;println(a);push!(a,last(a)+1);end


output:

[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]


# Python 3, 45 bytes

a=[1]
while 1:
print(a)
a+=[a[-1]+1]

• I don't know Julia, but could true be 0<1?
– user
Dec 4, 2020 at 18:51
• @user yes it can, thanks Dec 4, 2020 at 18:57
• By the way, it's recommended to add a link to an online interpreter/compiler, like this. TIO can generate the Markdown for your answer for you, so it's also more convenient.
– user
Dec 4, 2020 at 19:30

for(){1..++$i}  Try it online! Will output the numbers of the sequence separated by a new line. # Python 3, 36 bytes (including newline) s=[1] while 1:print(s);s+=[s[-1]+1]  Try it online! • My code seems to be exactly the same as the Python 3 solution above. I did not cheat (I haven't looked at the previous codes before I submitted my own answer); however, if this is against the guidelines I can delete my submission. Jan 23, 2021 at 5:46 • Duplicate answers are permitted. You might like to create a TIO link - there you can find a copy/paste template for formatting CGCC answers. Welcome to the site! Jan 23, 2021 at 6:30 # Regenerate-a, 14 bytes (${$1+1} !1 )+  Attempt This Online! The only choice for the regex to make here is how many times to repeat the (...)+ loop. The first iteration will always print 1 , because the first alternative can't match, as $1 hasn't been defined yet.

On subsequent iterations, ${$1+1} does math on $1, treating its contents as a number. It ignores the trailing space, and adds 1 to it. This then becomes the new value for $1, etc.

The ! short-circuiting alternation prevents the second alternative, i.e. 1 , from being taken once it becomes possible for the first alternative, ${$1+1} , to match.

• Of interest: using | instead of ! gives different output, but I think it still fits the challenge criteria. 8 hours ago

# Scala, 23 bytes

Stream from 1 map(1 to)


Try it online

An infinite Stream of IntRanges. Uses the sequence from the question.

# Python 2, 24 33 bytes

Added 9 bytes to fix an error kindly pointed out by caird coinheringaahing.

n=1
while 1:n+=1;print range(1,n)


Try it online!

• This only prints each n once, rather than an infinite number of times. This works however Nov 30, 2020 at 18:10
• @cairdcoinheringaahing Oops, misread the question! T_T Thanks! :D Nov 30, 2020 at 18:16

# Retina 0.8.2, 11 bytes


_
}*M!&_+


Try it online! Outputs in unary. Explanation: The first two lines cause the number of _s to increase on each pass, while the M!&_+ generates all suffixes, which ensures that all integers get printed. The } causes the entire script to repeat indefinitely. The * causes the suffixes to be printed and discarded, since otherwise Retina does not output until the program terminates.

18 bytes for a traditional decimal output. Each integer is printed in turn, and then for each integer all of the previous integers are printed again, so the final sequence is 1, 2, 1, 3, 2, 1, 4, 3 ...

{^
_
*(M!&_+
%_


Try it online!

# Pyth, 4 bytes

f!pS


Try it online!

## Explanation

f!pS
: lambda T:
S  :    range(1, T)
p   :    print and return the range
!    :    logical negate the range
f     : Find first natural number input to function that returns a truthy value


# Raku, 18 bytes

.say for [\,] 1..*


Try it online!

[\,] 1..* generates the list of prefixes of the sequence of naturals. .say for just prints them.

• Disappointing that you can't drop the space after for, not because it's no longer recognizable as for, but because the compiler recognizes that it is for and errors telling you you have to have whitespace Dec 2, 2020 at 0:13

# Nim, 47 bytes

var i=1
while true:
for j in 1..i:echo j
i+=1


Try it online! The footer makes it finite so the output can be observed.

Note that Nim doesn't have bigints, so this works only in theory (in accordance with our rules).

# Common Lisp, 56 bytes

(do((i 1(1+ i)))(())(dotimes(j i)(format t"~v^~a "j j)))


Nested loop solution using the do macro for the outer loop and the dotimes macro for the inner loop. dotimes starts with 0 so format includes a specifier that checks whether it's a 0.

# AWK, 33 bytes

An approach different from @Noodle9's, that luckly uses one less byte.

{for(;;a[++i])for(j in a)print j}


Try it online!

{
for(;;a[++i])  # by only stating the array[element], that element is created in the array.
# all not assigned variables starts at 0.
# at first, i++ would return 0, and increment 1 to i;
# but ++i increments 1 to i, and then return its new value.
for(j in a)  # for every element existing in the array,
print j    # prints the element j.
}


# AWK, 37 bytes

Prints numbers in one line.

{for(;;a[++i])for(j in a)printf j FS}


Try it online!

# AWK, 51 bytes

Prints numbers in a pyramid.

{for(;;a[++i])for(j in a)printf j+1 in a?j FS:j RS}


Try it online!

# Java (OpenJDK 8), 76 chars

void f(){for(int i=0,j;++i>0;out.println())for(j=0;j++<i;out.print(j+" "));}


Try it online!

Output (unlimited):

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
. . .
`