42
\$\begingroup\$

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.

\$\endgroup\$
15
  • 8
    \$\begingroup\$ 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) \$\endgroup\$ Nov 30, 2020 at 17:57
  • 1
    \$\begingroup\$ Are nested lists and/or non-natural numbers permitted? \$\endgroup\$ Nov 30, 2020 at 18:28
  • 1
    \$\begingroup\$ @UnrelatedString nested lists yes, I will edit the post but non-natural numbers are not permitted. \$\endgroup\$
    – Adam Katav
    Nov 30, 2020 at 18:30
  • 2
    \$\begingroup\$ Is it OK if the output is in random order (as long as all numbers are printed infinitely often with probability 1)? \$\endgroup\$ Nov 30, 2020 at 18:39
  • 1
    \$\begingroup\$ 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) \$\endgroup\$
    – Luis Mendo
    Nov 30, 2020 at 19:03

70 Answers 70

1 2
3
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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 
. . .
\$\endgroup\$
1
\$\begingroup\$

sed in unary, 16 bytes

s/^/1\n1/p;h;G;D

Try it online!

Each unary natural is on its own line. Seems to increase logarithmically.

sed has two spaces: the default one is called the pattern space and is lost after processing the next of input; the other one is the hold space, which instead is retained throughout execution. Both start empty.

(sed cannot run without input, so this program is run with an empty line as input).

The idea is to have the largest number encountered so far on the first line, and then have the result of the previous iterations on the succeeding lines.

s/^/1\n1/ replaces the beginning (symbolised with ^, really just empty space) with 1 followed by a newline (\n) and another 1. The p flag then prints the pattern space. The second 1 inserted increases the maximum number by 1, which was on the first line before the substitution, and now is on the second line.

h replaces the hold space with the contents of the pattern space, after which G appends the hold space to the pattern space, joining them by a newline. This is our method to retain the previous natural numbers encountered.

D deletes the first line (which in our case is always 1 thanks to the substitution made with the s command) and the newline after it. This effectively returns the largest number encountered so far back to the first line. Then D causes the program to be run from the beginning with what remains of the pattern space because the first line was not empty. Because the s command will always make sure the first line is not empty, the initial 1\n effectively acts only for D to cause the program to loop indefinitely.


sed 4.2.2, 15 bytes

:;s/^/1/p;h;G;t

Try it online!

sed 4.2.2 allows empty labels, ridding the need for 1\n and D.

\$\endgroup\$
1
\$\begingroup\$

Chapel, 51 bytes

var i=1;while(i){for j in 1..i do writeln(j);i+=1;}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Batch, 127 bytes

@echo off
setlocal enabledelayedexpansion
set "s=1"
set k=1
for /L %%a in (0,0,0) do (echo !s! &set /a k=k+1 &set "s=!s! !k!") 
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 20 bytes

for(){' '+(1..++$i)}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pyt, 7 bytes

1`⁺řĐƥł

Try it online!

Port of caird coinheringaahing's Jelly answer.

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 3 bytes

Þ∞K

Try it Online!

Þ∞ɾ

Try it Online!

Explanation

Þ∞  # Push an infinite list of positive integers:   [1, 2, 3, 4, 5, ...]
  K # Get the prefixes of this list                 [[1], [1, 2], [1, 2, 3], ...]
  ɾ # Inclusive one-range of each:                  [[1], [1, 2], [1, 2, 3], ...]
\$\endgroup\$
1
\$\begingroup\$

Thunno, \$ 9 \log_{256}(96) \approx \$ 7.41 bytes

[1+DR1+ZK

Attempt This Online!

13 chars for a better format: Attempt This Online!

Explanation

[1+DR1+ZK  # TOS is initially 0
[          # while True:
 1+        #  increment TOS
   DR1+    #  push range(1, TOS+1)
       ZK  #  pop and print
\$\endgroup\$
0
\$\begingroup\$

Python, 76 bytes

def n():
 c=2
 while 1:
  yield from range(1,c)
  c+=1
for i in n():print(i)

Uses generators and yield from to chain infinite ranges. You can call n directly for a generator.

\$\endgroup\$
1 2
3

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.