52
\$\begingroup\$

Write a program or function which will provably print all integers exactly once given infinite time and memory.

Possible outputs could be:

0, 1, -1, 2, -2, 3, -3, 4, -4, …

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -2, -3, -4, -5, -6, -7, -8, -9, 10, 11, …

This is not a valid output, as this would never enumerate negative numbers:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …

  • The output must be in decimal, unless your language does not support decimal integer (in that case use the natural representation of integers your language uses).

  • Your program has to work up to the numbers with the biggest magnitude of the standard integer type of your language.

  • Each integer must be separated from the next using any separator (a space, a comma, a linebreak, etc.) that is not a digit nor the negative sign of your language.

  • The separator must not change at any point.

  • The separator can consist of multiple characters, as long as none of them is a digit nor the negative sign (e.g. is as valid as just ,).

  • Any supported integer must eventually be printed after a finite amount of time.

Scoring

This is , so the shortest answer in bytes wins

Leaderboard

var QUESTION_ID=93441,OVERRIDE_USER=41723;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • 4
    \$\begingroup\$ If our language supports infinite lists, can we output the list from a function rather than printing? (Calling print on such a list would print its elements one at a time forever.) \$\endgroup\$ – xnor Sep 16 '16 at 8:57
  • 6
    \$\begingroup\$ I feel like the requirement on arbitrary-size integers does nothing but discourage languages without such integers from participating. They either have to have an import they can use or solve a totally different challenge from everyone else. \$\endgroup\$ – xnor Sep 16 '16 at 9:10
  • 3
    \$\begingroup\$ @xnor Changed, though that kinds of ruins the very name of the challenge. \$\endgroup\$ – Fatalize Sep 16 '16 at 9:14
  • 6
    \$\begingroup\$ @xnor, languages with arbitrary precision integers still have to solve a different problem from everyone else, so all that that change has accomplished is to make this problem boringly trivial in a lot of languages. \$\endgroup\$ – Peter Taylor Sep 16 '16 at 9:54
  • 3
    \$\begingroup\$ @PeterTaylor Yeah, this is unfortunate. The wrapping solutions don't feel to me like they are printing any negatives, but I don't see a way to firmly specify the difference when it's a matter of representation. \$\endgroup\$ – xnor Sep 16 '16 at 9:58

130 Answers 130

1 2 3 4
5
0
\$\begingroup\$

Yabasic, 22 bytes

An anonymous answer that takes no input and outputs all integers to the console.

?0
Do
i=i+1
?-i,i
Loop

Try it online!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Visual Basic .NET (Mono), 95 bytes

A declared subroutine that takes no input and outputs to the console.

Module M
Sub Main
Dim i
Do
Console.WriteLine(-i &IIf(i," "&i,""))
i=i+1
Loop
End Sub
End Module

Try it online!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

F#, 69 bytes

let f=
 let mutable i=0m
 while 1>0 do
  printf"%O,-%O,"i i
  i<-i+1m

It's pretty straight forward. The 0m sets i as 0, but of type decimal. Since we're allowed to print 0, -0 I'm doing that. i gets incremented with every iteration of the loop, and the value is printed out.

I my initial idea would have been 70 bytes but (like most of my initial ideas) it would never have worked:

let g=Seq.iter(fun x->printfn"%O,-%O"x x)[0m..System.Decimal.MaxValue]

In this one [0m..System.Decimal.MaxValue] creates an array starting at 0, adding 1 each time and going all the way to the maximum decimal value. (Another example would be the expression [1..10] creates an array with {1,2,3,4,5,6,7,8,9,10}).

Seq.iter would then apply the function for printing to that array.

The problem with this is that the array must be constructed before iterating through it. So for all integers, with infinite memory the array could be constructed - but it would also take an infinite amount of time, so nothing would ever be printed out!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Javascript, 96 bytes

f=n=>f([].concat(...n.map(i=>alert(+i?i+' -'+i:i)||+i?y.map(o=>i+o):[])));f(y=[...`0123456789`])

Alerts all integers in the sequence (0, 1, -1, 2, -2, ...), using strings to go beyond JS's usual max safe integer 9007199254740991. This is equivalent to the "Infinite version" in Arnauld's earlier answer.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Scala, 112 bytes

val x=Int.MaxValue
val y=Int.MinValue
print(0)
for(a <-1 until x) print(","+a + "," + a * -1)
print(","+x+","+y)

Try it online!

Testable Running Program with limits [-10,10]

Scala, 143 bytes

object ListNumbers extends App {
  val x = 10
  val y = -10
  print(0)
  for(a <- 1 until x) print(","+a + "," + a * -1)
  print(","+x+","+y)
}

Try it online!

Using Java 8 Streams just for fun, it generates a lazy populated string

types declarations can be omitted but I included for "documentation"

Scala, 253 bytes

val zero :Stream[String] = Stream("0")
val positive :Stream[Int]= Stream.from(1)
val negative :Stream[Int]= positive.map(_* -1)

val stream = zero ++ positive.zip(negative).map(x => x._1.toString()+","+x._2.toString())
//stream.take(Int.MaxValue).toList

Try it online!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Japt, 12 10 bytes

_OlU°,Un}a

Try it online!

Explanation:

_OlU°,Un}a
            | Implicit U = 0
_       }a  | Start an infinite loop
 Ol         | Write to the console:
   U°       |   U++
     ,      |    and
      Un    |   0-U
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Gol><>, 7 bytes

lN01l-N

Try it online!

How it works

lN01l-N
l        push current stack length (n)
 N       pop and print with newline
  01l    push 0, 1, then stack length (n+2)
     -   pop n+2, 1, then push 1 - (n+2) = -n-1
      N  pop and print with newline
         now the stack length is n+1; repeat indefinitely
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Zephyr, 48 bytes

set i to 0
while 1=1
print-i,i+1...
inc i
repeat

Space-separated. If the separator could alternate between space and newline, the ... could be removed for -3 bytes. Try it online!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Small Basic, 92 bytes

A Script that takes no input and outputs to the TextWindow object.

TextWindow.Write(0+" ")
While 1=1
i=i+1
TextWindow.Write(Text.Append(-i+" ",i+" "))
EndWhile
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

W d, 6 5 bytes

Newline-separated. Look ma, no bitwise magic!

▲h(2Z

Explanation

Decompressed:

0      % Print 0 (the implicit not-given operand, to be precise)
 i   E % In the range 1..infinity...
  a    % Explicit loop variable
   P   % Print the number with a newline
    _  % Negate the number
       % Implicit print on each iteration
       % Make sure that the second-to-top executes (this is now implicit)
|improve this answer|||||
\$\endgroup\$
1 2 3 4
5

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