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

3
\$\begingroup\$

Julia, 27 bytes

f(n=1)=println.(1:n),f(n+1)

Try it online!

\$\endgroup\$
3
\$\begingroup\$

AutoHotkey, 36 bytes

Loop
Loop,%A_Index%
Send,%A_Index%`t

Output is via keyboard simulation so it will print in whatever the active window is.
Numbers are separated by tabs.

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   1   2   3   4   5   6   7   8   9   10  1   2   3   4   5   6   7   8   9   10  11  1   2   3   4   5   6   7   8   9   10  11  12  1   2   3   4   5   6   7   8   9   10  11  12  13  1   2   3   4   5   6   7   8   9   10  11  12  13  14  1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  

Alternatively - and not in compliance with the challenge spec - if you prefer your numbers to be spoken rather than printed:

Loop
Loop,%A_Index%
ComObjCreate("SAPI.SpVoice").Speak(A_Index)
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – user
    Nov 30, 2020 at 21:44
  • \$\begingroup\$ 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 ;. \$\endgroup\$ Dec 2, 2020 at 15:40
  • \$\begingroup\$ Here's my 47 byte solution. I made a new post because the method approach was different. \$\endgroup\$
    – branboyer
    Dec 3, 2020 at 1:08
3
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ Welcome to the site. I know nothing about Rust, but when I run your code it fails with an error: Try it online! \$\endgroup\$
    – Dingus
    Dec 1, 2020 at 9:37
  • \$\begingroup\$ That lines belongs into the Cargo.toml file, which defines the dependencies. Not sure how it works with that site \$\endgroup\$
    – Patiga
    Dec 1, 2020 at 9:57
  • 2
    \$\begingroup\$ 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". \$\endgroup\$
    – ovs
    Dec 1, 2020 at 15:12
  • 2
    \$\begingroup\$ 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>())} \$\endgroup\$
    – ovs
    Dec 1, 2020 at 15:14
  • \$\begingroup\$ @ovs thanks for the info, fixed the two issues you brought up \$\endgroup\$
    – Patiga
    Dec 3, 2020 at 8:52
3
\$\begingroup\$

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!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Adam Katz
    Dec 3, 2020 at 20:48
  • \$\begingroup\$ Not any more. Saved a byte using shuf from coreutils \$\endgroup\$
    – roblogic
    Dec 8, 2020 at 15:48
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Adam Katz
    Dec 8, 2020 at 17:01
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Adam Katz
    Dec 8, 2020 at 17:08
  • \$\begingroup\$ Thanks, will update this soon. Another one for 19B: s(){seq $[++n];s};s \$\endgroup\$
    – roblogic
    Dec 8, 2020 at 17:34
3
\$\begingroup\$

Perl 5, 22 20 bytes

say while$_.=++$i.$"

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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

AWK, 34 bytes

{for(;;++i)for(j=0;j++<i;)print j}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ @AdamKatz No, j needs to be re-initialised for every iteration of the inner loop. \$\endgroup\$
    – Noodle9
    Sep 26, 2022 at 20:57
  • \$\begingroup\$ hah, good point \$\endgroup\$
    – Adam Katz
    Sep 26, 2022 at 20:59
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 11 bytes by using the return value of . \$\endgroup\$
    – ovs
    Nov 30, 2020 at 23:14
  • \$\begingroup\$ @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. \$\endgroup\$
    – user
    Dec 1, 2020 at 0:04
2
\$\begingroup\$

PHP, 30 bytes

<?for(;;)echo--$j?:$j=++$i,~_;

Try it online!

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

\$\endgroup\$
4
  • \$\begingroup\$ 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 \$\endgroup\$
    – Kaddath
    Dec 2, 2020 at 8:22
  • \$\begingroup\$ @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. \$\endgroup\$ Dec 2, 2020 at 10:44
  • \$\begingroup\$ @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 \$\endgroup\$
    – Kaddath
    Dec 2, 2020 at 13:56
  • \$\begingroup\$ @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. \$\endgroup\$ Dec 2, 2020 at 15:27
2
\$\begingroup\$

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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you were a few hours earlier, I could have up voted this. Now I have to wait 24 hours. Very sad. \$\endgroup\$
    – lyxal
    Dec 1, 2020 at 2:19
2
\$\begingroup\$

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!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 34 bytes: for(n=k=0;;)print(n=n<k?n+1:++k/k) \$\endgroup\$
    – tsh
    Dec 1, 2020 at 1:55
  • \$\begingroup\$ 29 bytes: for(n=k=1;;)print(n=--n||k++) \$\endgroup\$
    – Sisyphus
    Dec 1, 2020 at 2:16
  • \$\begingroup\$ @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. \$\endgroup\$
    – Arnauld
    Dec 1, 2020 at 2:23
  • \$\begingroup\$ @Arnauld Ah, I see. I've posted mine then. \$\endgroup\$
    – Sisyphus
    Dec 1, 2020 at 2:29
2
\$\begingroup\$

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\$.

\$\endgroup\$
1
  • \$\begingroup\$ 30 bytes \$\endgroup\$
    – att
    Dec 24, 2020 at 19:02
2
\$\begingroup\$

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

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

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.

\$\endgroup\$
9
  • \$\begingroup\$ 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 \$\endgroup\$ Dec 3, 2020 at 13:17
  • \$\begingroup\$ @DMiddendorf I don't think the output has to be sorted \$\endgroup\$
    – user
    Dec 3, 2020 at 14:52
  • \$\begingroup\$ 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. \$\endgroup\$
    – user
    Dec 3, 2020 at 14:53
  • \$\begingroup\$ @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. \$\endgroup\$
    – branboyer
    Dec 3, 2020 at 17:26
  • \$\begingroup\$ @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. \$\endgroup\$
    – branboyer
    Dec 3, 2020 at 17:28
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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]
\$\endgroup\$
3
  • 1
    \$\begingroup\$ I don't know Julia, but could true be 0<1? \$\endgroup\$
    – user
    Dec 4, 2020 at 18:51
  • 1
    \$\begingroup\$ @user yes it can, thanks \$\endgroup\$ Dec 4, 2020 at 18:57
  • \$\begingroup\$ 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. \$\endgroup\$
    – user
    Dec 4, 2020 at 19:30
2
\$\begingroup\$

PowerShell Core, 14 bytes

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

Try it online!

Will output the numbers of the sequence separated by a new line.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 36 bytes (including newline)

s=[1]
while 1:print(s);s+=[s[-1]+1]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Jan 23, 2021 at 5:46
  • 1
    \$\begingroup\$ 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! \$\endgroup\$
    – Dingus
    Jan 23, 2021 at 6:30
2
\$\begingroup\$

Vyxal, 5 bytes

{nɾṄ,

Try it Online! The 5 flag makes the online interpreter time out after 5 seconds.

Explanation:

{      # Open a(n infinite) while loop
 n     # Get the loop variable's value
  ɾ    # Push the range from 1 to n
   Ṅ   # Join by spaces (ɾ creates a list)
    ,  # Print
\$\endgroup\$
5
  • \$\begingroup\$ You can just do Þ∞K to output an infinite list. \$\endgroup\$
    – naffetS
    Jul 25, 2022 at 1:30
  • \$\begingroup\$ @Steffan Can you send me a link? It times out for me when I try to join the lists \$\endgroup\$
    – aketon
    Jul 25, 2022 at 1:42
  • \$\begingroup\$ Try it Online! \$\endgroup\$
    – naffetS
    Jul 25, 2022 at 2:03
  • \$\begingroup\$ @Steffan That doesn't join the lists, This times out, is what I meant. \$\endgroup\$
    – aketon
    Jul 25, 2022 at 5:51
  • \$\begingroup\$ The OP said that nested lists are fine. That times out because infinite strings are not a thing. You can add f if you don't like nested lists. You can also use the S flag to join by spaces, which will work on infinite lists \$\endgroup\$
    – naffetS
    Jul 25, 2022 at 15:22
2
\$\begingroup\$

Appleseed, 16 bytes

(q(_(map 1to(1to

Anonymous function that can be called with no arguments and returns an infinite nested list ((1) (1 2) (1 2 3) ...). Try it online!

Explanation

(q           ; Anonymous function
 (_          ; that takes any number of arguments:
  (map 1to   ; Inclusive range from 1 to N for each N in
   (1to))))  ; numbers from 1 to infinity
\$\endgroup\$
2
\$\begingroup\$

Bash, 21 19 bytes

s()(seq $[++n];s);s
1
1
2
1
2
3
…

Run the 19B 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 24 bytes:

s()(seq -s\  $[++n];s);s
1
1 2
1 2 3
…

Run the 24B version on Try It Online

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

Scala, 23 bytes

Stream from 1 map(1 to)

Try it online

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

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

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!

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

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!

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

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

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$ Dec 2, 2020 at 0:13
1
\$\begingroup\$

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).

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

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.

\$\endgroup\$

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.