88
\$\begingroup\$

Write a program that counts up forever, starting from one.

Rules:

  • Your program must log to STDOUT or an acceptable alternative, if STDOUT is not available.
  • Your program must be a full, runnable program, and not a function or snippet.
  • Your program must output each number with a separating character in between (a newline, space, tab or comma), but this must be consistent for all numbers.
  • You may print the numbers in decimal, in unary or in base 256 where each digit is represented by a byte value.
  • Your program must count at least as far as 2128 (inclusive) without problems and without running out of memory on a reasonable desktop PC. In particular, this means if you're using unary, you cannot store a unary representation of the current number in memory.
  • Unlike our usual rules, feel free to use a language (or language version) even if it's newer than this challenge. Languages specifically written to submit a 0-byte answer to this challenge are fair game but not particularly interesting.

    Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language.

  • This is not about finding the language with the shortest solution for this (there are some where the empty program does the trick) - this is about finding the shortest solution in every language. Therefore, no answer will be marked as accepted.

Catalogue

The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><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="language-list"> <h2>Shortest Solution 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> <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> <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><script>var QUESTION_ID = 63834; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 39069; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "//api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "//api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(42), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) return -1; return 0; }); for (var i = 0; i < langs.length; ++i) { var language = jQuery("#language-template").html(); var lang = langs[i]; language = language.replace("{{LANGUAGE}}", lang.lang) .replace("{{NAME}}", lang.user) .replace("{{SIZE}}", lang.size) .replace("{{LINK}}", lang.link); language = jQuery(language); jQuery("#languages").append(language); } }</script>

\$\endgroup\$
26
  • 35
    \$\begingroup\$ I'm not sure how to combine must output each number with a separating character in between with may print the numbers [...] in base 256. \$\endgroup\$
    – Dennis
    Commented Nov 14, 2015 at 14:25
  • 8
    \$\begingroup\$ For future challenges, may I recommend the sandbox such that all these details could be sorted out before people start posting answers? :) \$\endgroup\$ Commented Nov 14, 2015 at 14:32
  • 4
    \$\begingroup\$ @IlmariKaronen I interpret that as being a memory limit, not a time limit. possibly a time limit on per-increment. just set the counter to 2**128-10 and see how long it takes to take those last ten steps. \$\endgroup\$
    – Sparr
    Commented Nov 15, 2015 at 7:41
  • 5
    \$\begingroup\$ Can we have leading zeroes in the output? \$\endgroup\$ Commented Nov 15, 2015 at 11:43
  • 4
    \$\begingroup\$ Ugh!!! I have an answer for TI-89 (56b), but I can't post b/c I'm new to the site and don't have Rep 10+! \$\endgroup\$ Commented Nov 15, 2015 at 20:45

232 Answers 232

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

Retina, 13 bytes

+\`^.*
$.(*__

Try it online!

Explanation

Retina 1.0 has a new feature which lets us solve this with an algorithm that conceptually uses a unary representation of the number, but still won't run out of memory. You can test this by initialising the string to a really high number, in that case the program will count from there.

So incrementing decimal numbers isn't really something Retina does, because it only works with strings. Leaky Nun has shown a way to increment decimals, but it's comparably complicated.

What Retina can do with decimal integers is a) repeat a character or string n times and b) measure the length of a string. So the idea is to take the last integer n, generate a string with n+1 characters, and measure its length, and use this as our new result. That's what the code does:

  • ^.* match the current string. We need to use .* instead of .+ because the string is initially empty (the program's input), and ^ because .* would otherwise yield two matches on subsequent iterations.
  • We substitute the match with $.(*__. There is some implicit syntax going on here. The ) is closed implicitly and * has an implicit left-hand operand of $&, so this substitution is really short for $.($&*__). $&*_ generates the string of n underscores (when $& is empty, i.e. on the first iteration, it is treated as zero). The second _ appends another underscore to that, generating a string of n+1 characters. $.(…) measures the length of that string, so we end up with the next integer.
  • \ tells Retina to print the result of this substitution with a trailing linefeed.
  • + runs the entire thing in an infinite loop (+-loops can terminate, but this one won't).

So why does this work for large integers without running out of memory? Retina's $.(…) computation is lazy! Whenever possible, its string argument isn't actually fully evaluated, but its length is computed directly instead. And it knows that a string formed with n*s is n*len(s) characters long. This feature gives us the convenience of a unary approach without the memory requirements. The length computation also uses an arbitrary-precision integer type, so that we can go well beyond 2128.

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

Chip, 2214 bytes

Noncompeting: this uses language features that post-date the challenge. For a competing entry that is too inefficient to run, see the edit history.

2211 bytes for the code, plus 3 bytes for the -w flag.

o z*
`K\' ,--K   ,--K   ,--K   ,--K   ,--K   ,--K   ,--K8  ,--K   ,--K   ,--K   ,--K   ,--K   ,--K   ,--K   ,--K
o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o
o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o
o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o
o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o
o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o
o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o
o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o
o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o | o#-o,' o#-o
 `---'  `---'  `---'  `---'  `---'  `---'  `---'  `---'  `---'  `---'  `---'  `---'  `---'  `---'  `--'
=
o-----)-------------------------------------------------------------------------------------------------.
`z--z------z------z------z------z------z------z------z------z------z------z------z------z------z------z-K----.
o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a o-zo/a
oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b oz-o/b
o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c o-zo/c
oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d oz-o/d
o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e o-zo/e
oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f oz-o/f
o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g o-zo/g
oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h oz-o/h

This solution gives output in 128 bits / 16 bytes, big endian. It has two main sections, one for incrementing, and the other for timing, storage, and output. There is likely a smaller implementation out there, but I haven't yet conceived of it.

Try it online for the 128-bit version.
Try it online for the 16-bit version, much faster than the 128 bit version.

Notes about the TIO: The -v flag is included, which shows the actual incoming/outgoing binary on stderr. In the TIO, we also provide a -cN flag, which cuts off the program after N bytes have been produced.

Explanation

Chip is a (mostly) 2D language that operates on individual bits, which is why it needs a lot of space for 128-bit support. I won't get into the specific details (because we'd be here all day), but here's the gist of what's going on:

  • Layers:
    =
    This is a layer divider. Lines above form the first layer, lines below form the second. Wherever there are o's on both layers, a connection is formed.
  • Incrementing:
     K   ,--
    o#-o |
    o#-o |
    o#-o |
    o#-o |
    o#-o |
    o#-o |
    o#-o |
    o#-o |
     `---'
    Each # is a half-adder, and each column of # serves to increment the eight inputs on the left to the eight outputs on the right. The K at the top is a caching element to help with call stack efficiency. The tail is a wire to bring the carry back up to the next incrementor. There are sixteen such incrementors, for the sixteen bytes required to produce 128 bits. The 8 seen near the eighth incrementor is used for cache priming. This element is always the first element polled each cycle, and so it primes all caches to it's left. This prevents a stack overflow when we eventually compute the values to the right.
  • Storage:
    o-zo
    oz-o
    o-zo
    oz-o
    o-zo
    oz-o
    o-zo
    oz-o
    Each z reads a signal in on its top/right, and outputs that signal to the bottom/left. Therefore, these columns act as registers to hold the current state for the next cycle to use. The z's are offset from each other to prevent cross-talk. Again, sixteen such columns, one per byte.
  • Output:
    o/a
    o/b
    o/c
    o/d
    o/e
    o/f
    o/g
    o/h
    The letters a through h correspond to each of the bits of the byte that will be output, with a being the least signficant bit. The columns of /'s are switches that interact with the timing circuit to determine which byte is due to be output. Once more, sixteen columns, one per byte.
  • Timing:
    o z*
    `v\'
    o-------------
    `z--z------z--
    The timing circuit consists of two parts. The portion on the first layer produces a high signal on the first clock tick only (to get it all started) and connects the rest of the timing circuit to the incrementors (increments by 1 only every 16th tick, increments by 0 otherwise). The portion on the second layer controls the actual sequencing. The pattern is to increment the value and output the highest byte, then output each successive byte without incrementing anything. When all bytes have been printed, the cycle repeats. (There is a cache element K and an or-gate ) that acts as a diode; these are again for efficiency only.)
\$\endgroup\$
3
\$\begingroup\$

Brainetry -w 0, 63 bytes

Golfed version:

a b c d
a b c d e f g h
a b c d e f g
a b c d
a b c d e f g h i

We use -w 0 otherwise we can only count up to 255. If we add the --numeric-io flag we can also get the output as actual integers instead of codepoints.

The golfed version was adapted from the program at the end of this answer. To try this online you can

  • head over to this replit link, copy&paste the code into the btry/replit.btry file and hit the green "Run" button;
  • clone the github repo and from your terminal run ./brainetry btry/ppcg/infinite_count.btry -w 0 --numeric-io.

Original program:

This program runs forever.
Not only it runs forever but it also
prints all natural numbers starting from one.
What a simple task.
I did it in just five lines of code.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 for I did it in just five lines of code. \$\endgroup\$ Commented Aug 14, 2020 at 4:46
3
\$\begingroup\$

Stax, 3 bytes

VIm

Run and debug it

the other Stax answer is invalid since it starts from zero. This is shorter, anyway.

\$\endgroup\$
1
  • 6
    \$\begingroup\$ Haven't seen it, but was the other answer Emacs? \$\endgroup\$ Commented Feb 26, 2021 at 20:59
3
\$\begingroup\$

Dash, 12 bytes

yes|sed -n =

Or, alternatively:

yes|sed =\;d

Try it online!

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

Add++, 11 7 bytes

Dx,+1,O

Try it online!

-4 Thanks to @cairdcoinheringaahing

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

Rust, 99 69 bytes

fn main(){for i in 0..=!0u128{print!("{i}
")}print!("{}6",!0u128/10)}

Try it on the Rust Playground!

45 bytes if it weren't for that darn inclusive. Also, be warned, the size of the output may cause the rust playground to crash.

Edit: saved 30 bytes with some help from @The Fifth Marshal, and it now prints out 2^128-1

\$\endgroup\$
7
  • \$\begingroup\$ How do I run this? TIO throws an error. \$\endgroup\$
    – Chris
    Commented Sep 30, 2020 at 19:44
  • \$\begingroup\$ @Chris Try it on the rust playground! \$\endgroup\$
    – Aiden4
    Commented Sep 30, 2020 at 19:49
  • \$\begingroup\$ Yeah, that works. Thanks :) \$\endgroup\$
    – Chris
    Commented Sep 30, 2020 at 19:51
  • 1
    \$\begingroup\$ Also +1 for being the first answer I tried to verify that actually manages to go up to 2^128 ;) \$\endgroup\$
    – Chris
    Commented Sep 30, 2020 at 19:54
  • 1
    \$\begingroup\$ 76 bytes \$\endgroup\$ Commented Mar 23, 2022 at 18:19
3
\$\begingroup\$

Desmoslang Assembly, 3 Bytes

1O+  

Explanation: Command gets 1 added to it, so it is 1. That is outputted, a plus is added, and it gos to the start because that is the default to value.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to CGCC! \$\endgroup\$ Commented May 29, 2023 at 14:30
3
\$\begingroup\$

Rockstar, 30 bytes

X's 1
while X
say X
let X be+1

Try it (Code will need to be pasted in)

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

Linux X86_64, 55 54 47 bytes

newer version actually completes the challenge, using 136-bit integers :)

another improvement was suggested by ceilingcat; using pushq and popq instead of mov to set registers

register uses:
%rax: syscall number (1), %ah is always zero
%rdx: byte count (=18)
%rcx: clobbered by syscall
%bl: highest 8 bits of number
%rsp: pointer to top of string at the start of program, becomes middle 64 bits of number
%rbp: lowest 64 bits of number
%rsi: pointer to bottom of output string
%rdi: file descriptor (1)

0000000000000000 <_start>:
   0:   6a 01                   push   $0x1
   2:   5f                      pop    %rdi
   3:   6a 12                   push   $0x12
   5:   5a                      pop    %rdx
   6:   6a 0a                   push   $0xa
   8:   48 89 e6                mov    %rsp,%rsi
   b:   48 83 ee 09             sub    $0x9,%rsi
   f:   31 db                   xor    %ebx,%ebx
  11:   31 ed                   xor    %ebp,%ebp
  13:   31 e4                   xor    %esp,%esp

0000000000000015 <loop>:
  15:   88 1e                   mov    %bl,(%rsi)
  17:   48 0f 38 f1 66 01       movbe  %rsp,0x1(%rsi)
  1d:   48 0f 38 f1 6e 09       movbe  %rbp,0x9(%rsi)
  23:   89 f8                   mov    %edi,%eax
  25:   0f 05                   syscall
  27:   48 01 fd                add    %rdi,%rbp
  2a:   48 11 dc                adc    %rbx,%rsp
  2d:   10 e3                   adc    %ah,%bl
  2f:   eb e4                   jmp    15 <loop>
```
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$ Commented Jun 5, 2023 at 22:12
  • 1
    \$\begingroup\$ Suggest push $0x12; pop %rdx instead of xor %rdx, %rdx; mov $0x12, %dl \$\endgroup\$
    – ceilingcat
    Commented Jun 19, 2023 at 15:05
3
\$\begingroup\$

c + GMP library, 90

Score includes +4 for passing -lgmp to the compiler. You'll need GMP installed to build and run this.

Here's how we do arbitrary precision in c:

#include<gmp.h>
main(){mpz_t i;for(mpz_init(i);mpz_add_ui(i,i,1),gmp_printf("%Zd\n",i););}

Compile with:

cc forever.c -o forever -lgmp

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ I don't understand... what's wrong with double long unsigned long i; for(i=0;1>0;i++){printf("v%\n",i)} ? \$\endgroup\$
    – cat
    Commented Dec 29, 2015 at 16:04
  • \$\begingroup\$ @cat I'm not familiar with the double long unsigned long type - which compiler are you using? GCC doesn't like it. \$\endgroup\$ Commented Dec 29, 2015 at 16:55
  • \$\begingroup\$ GCC compiled it for me but I didn't realise it segfaulted, never mind ¯_(ツ)_/¯ \$\endgroup\$
    – cat
    Commented Dec 29, 2015 at 17:04
  • \$\begingroup\$ @KrzysztofSzewczyk How would that work? stdout the stream associated with the STDOUT fd (commonly numerical 1), but is itself a FILE * pointer. mpz_out_str() needs a FILE * and not an int 1. \$\endgroup\$ Commented Feb 13, 2018 at 19:15
  • \$\begingroup\$ @KrzysztofSzewczyk By the way, I navigated to the URL you link to on your profile page. My McAfee detected WASM/Cryptonight. Did you know you are spreading trojans? \$\endgroup\$ Commented Feb 13, 2018 at 19:19
2
\$\begingroup\$

Befunge, 4 bytes

1+:.

This just requires an interpreter with arbitrary precision integers.

\$\endgroup\$
1
  • \$\begingroup\$ You don't even compile your Befunge code? :( \$\endgroup\$
    – yyny
    Commented Nov 16, 2015 at 18:14
2
\$\begingroup\$

Python 2 VM Opcodes, 14 bytes?

LOAD_CONST               0
LOAD_CONST               1
INPLACE_ADD
DUP_TOP
PRINT_ITEM
JUMP_ABSOLUTE            2

I'm not sure how to score this, the compiled code is 12 bytes of vm code but there are also the constants, which would be unscored. Each constant can fit in a single bit but I'll count them as 1 byte each for now.

The compiled .pyc file is 106 bytes long(!) and the raw text is 111 bytes long.

I will probably just write a short script that just takes python opcodes and runs them properly (A bit like Pyth but much more low level).

Overall, it's a very simple program. It loads 0 and 1 onto the stack and adds them. This removes both from the stack and leaves it with the result. It then duplicates the stack and prints the value of the first item, removing it from the stack. It then goes to line 2.

.pyc file (hexdump)

03 F3 0D 0A DB 5B 47 56 63 00 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 73 0C 00 00 00 64 01 00 64 02 00 37 04 47 71 03 00 28 03 00 00 00 4E 69 00 00 00 00 69 01 00 00 00 28 00 00 00 00 28 00 00 00 00 28 00 00 00 00 28 00 00 00 00 74 09 00 00 00 70 79 6B 65 5F 63 6F 64 65 52 00 00 00 00 01 00 00 00 74 00 00 00 00
\$\endgroup\$
2
\$\begingroup\$

Rotor, 7 bytes

1{1+}|

There's an unprintable again, hexdump:

0000000: 317b 1b31 2b7d 7c                      1{.1+}|

ew 7 bytes

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

Kotlin, 102 bytes

import java.math.BigInteger.*fun main(a:Array<String>){var i=ZERO;while(1>0){i=i.add(ONE);println(i)}}

Based on SuperJedi224's Java answer.

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

GolfScript, 9 bytes

1{.p).}do

1      # push 1
{      # loop
  .p   # duplicate, output
  ).   # increment, duplicate
}do    # while non-zero
\$\endgroup\$
2
\$\begingroup\$

Win32 x86 Machine Code (PE32 Executable), 933 bytes

This implements a virtual datatype using a lookup table (aka array). The challenge requests support for 2^128 which is 39 decimal digits. By accident, this solution supports 38+N digit numbers, where N is the width of word (i.e. two bytes). After that, the behavior is undefined. You might notice a whole lot of too simple or too redundant code in the assembly below, but this is intended, because the compression algorithm optimizes this.

For example,

mov EAX, 0
mov dword [@@DATA+164], EAX

is two bytes shorter than

mov dword [@@DATA+164], 0

Also: I though of using the C runtime library (MSVCRT) to convert numbers to strings, but this is a DIY approach, so this solution contains it's own method of converting numbers to strings. Without this, the byte count would be 800-ish. Keep in mind that the PE header and decompression take up about 697 bytes :). Also, this is Win32 code (fully debugged, shouldn't produce any errors and spawns a clean console context to run in), it is not "just using Interrupts", so this contains it's own string printing method.

I've used MEW11SE to crunch the executable, because MEW's LZMA-E9 routine beats UPX, mpress, kkrunchy etc. 9.9 out of 10 times by far. I could have used nasm and crinkler, but I hate both of them. The assembly below is fasm.

Here's a binary (compressed) - Here's another binary (uncompressed)

Disclaimer: The compressed binary will probably set your PC on fire if you happen to have an AV installed.

Assembly

; minxomat

; Link as CUI subsystem
format PE CONSOLE
section ".code" code readable executable

; Convert single digit unsigned word to null-terminated char
makestr:
  push EBP
  mov EBP, ESP
  sub ESP, 12
  mov EAX, @@TEXT+0
  mov dword [EBP-4], EAX
  mov EAX, @@TEXT+3
  mov dword [EBP-8], EAX
  mov EAX, 0
  mov dword [EBP-12], EAX
  mov EAX, dword [EBP+8]
  add EAX, dword [EBP-8]
  mov AL, byte [EAX+0]
  mov EBX, dword [EBP-12]
  add EBX, dword [EBP-4]
  mov byte [EBX+0], AL
  mov AL, 0
  push EAX
  mov EAX, dword [EBP-12]
  add EAX, 1
  add EAX, dword [EBP-4]
  pop EBX
  mov byte [EAX+0], BL
  mov EAX, dword [EBP-4]
  mov ESP, EBP
  pop EBP
ret 4

; Ouput null-term'ed string at this pointer to console
printme:
  push EBP
  mov EBP, ESP
  sub ESP, 4
  mov EAX, 0
  mov dword [EBP-4], EAX

  addone:
    mov EAX, dword [EBP-4]
    add EAX, dword [EBP+8]
    mov AL, byte [EAX+0]
    cmp AL, 0
    je fwd
    inc dword [EBP-4]
    jmp addone

  fwd:
    xor EAX, EAX
    push EAX
    lea EAX, [EBP-4]
    push EAX
    mov EAX, dword [EBP-4]
    push EAX
    mov EAX, dword [EBP+8]
    push EAX
    mov EAX, 4294967285
    push EAX
    call [@10024]
    push EAX
    call [@10025]
    mov ESP, EBP
    pop EBP
ret 4

; Entry point.
main:
  ; Print initial zero
  push EBP
  mov EBP, ESP
  mov EAX, @@TEXT+14
  push EAX
  call printme

  @10007:
    mov EAX, 1
    mov dword [@@DATA+160], EAX
    mov dword [@@DATA+164], EAX
    mov EAX, 38
    mov dword [@@DATA+156], EAX

  @10008:
    mov EAX, dword [@@DATA+156]
    cmp EAX, 0
    jle @10016

  @10009:
    mov EAX, dword [@@DATA+156]
    shl EAX, 2
    mov EAX, dword [@@DATA+EAX+0]
    mov EBX, dword [@@DATA+160]
    add EAX, EBX
    mov EBX, dword [@@DATA+156]
    shl EBX, 2
    mov dword [@@DATA+EBX+0], EAX
    mov EAX, dword [@@DATA+156]
    shl EAX, 2
    mov EAX, dword [@@DATA+EAX+0]
    cmp EAX, 9
    jle @10011

  @10010:
    mov EAX, 0
    mov EBX, dword [@@DATA+156]
    shl EBX, 2
    mov dword [@@DATA+EBX+0], EAX
    mov EAX, 1
    mov dword [@@DATA+160], EAX
    jmp @10014

  @10011:
    mov EAX, 0
    mov dword [@@DATA+160], EAX

  @10014:
    dec dword [@@DATA+156]
    jmp @10008

  @10016:
    mov EAX, dword [@@DATA+156]
    cmp EAX, 39
    jge @10022

  @10017:
    mov EAX, dword [@@DATA+156]
    shl EAX, 2
    mov EAX, dword [@@DATA+EAX+0]
    cmp EAX, 0
    jg @10019

  @10018:
    mov EAX, dword [@@DATA+164]
    cmp EAX, 0
    jne @10021

  @10019:
    mov EAX, dword [@@DATA+156]
    shl EAX, 2
    mov EAX, dword [@@DATA+EAX+0]
    push EAX
    call makestr
    push EAX
    call printme
    mov EAX, 0
    mov dword [@@DATA+164], EAX

  @10021:
    inc dword [@@DATA+156]
    jmp @10016

  @10022:
    mov EAX, @@TEXT+17
    push EAX
    call printme
    jmp @10007

section ".data" data readable writeable

@@TEXT db "00", 0
       db "0123456789", 0
       db "0", 10, 0
       db 10, 0

@@DATA rb 168

section ".idata" import data readable writeable

dd 0, 0, 0, RVA @10026, RVA @10024
dd 0, 0, 0, 0, 0

@10024 dd RVA @10027
@10025 dd RVA @10028
       dd 0

@10026: db "KERNEL32.DLL", 0

@10027: dw 0
        db "GetStdHandle", 0
@10028: dw 0
        db "WriteFile", 0

section ".reloc" fixups data readable discardable
entry main

Hexdump

Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000  4D 5A 6B 65 72 6E 65 6C 33 32 2E 64 6C 6C 00 00  MZkernel32.dll..
00000010  50 45 00 00 4C 01 02 00 00 00 00 00 00 00 00 00  PE..L...........
00000020  00 00 00 00 E0 00 0F 01 0B 01 00 00 00 02 00 00  ....à...........
00000030  00 00 00 00 00 00 00 00 95 51 00 00 10 00 00 00  ........•Q......
00000040  00 10 00 00 00 00 40 00 00 10 00 00 00 02 00 00  ......@.........
00000050  04 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00  ................
00000060  00 60 00 00 00 02 00 00 00 00 00 00 03 00 00 00  .`..............
00000070  00 10 00 00 00 10 00 00 00 00 01 00 00 00 00 00  ................
00000080  00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00  ................
00000090  91 51 00 00 14 00 00 00 00 00 00 00 00 00 00 00  ‘Q..............
000000A0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000B0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000C0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000D0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000E0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000100  00 00 00 00 00 00 00 00 4D 45 57 00 46 12 D2 C3  ........MEW.F.ÒÃ
00000110  00 40 00 00 00 10 00 00 00 00 00 00 00 00 00 00  .@..............
00000120  00 00 00 00 00 00 00 00 00 00 00 00 E0 00 00 C0  ............à..À
00000130  02 D2 75 DB 8A 16 EB D4 00 10 00 00 00 50 00 00  .ÒuÛŠ.ëÔ.....P..
00000140  A5 01 00 00 00 02 00 00 00 00 00 00 00 00 00 00  ¥...............
00000150  00 00 00 00 E0 00 00 C0 BE 1C 50 40 00 8B DE AD  ....à..À¾.P@.‹Þ.
00000160  AD 50 AD 97 B2 80 A4 B6 80 FF 13 73 F9 33 C9 FF  .P.—²€¤¶€ÿ.sù3Éÿ
00000170  13 73 16 33 C0 FF 13 73 21 B6 80 41 B0 10 FF 13  .s.3Àÿ.s!¶€A°.ÿ.
00000180  12 C0 73 FA 75 3E AA EB E0 E8 72 4E 00 00 02 F6  .Àsúu>ªëàèrN...ö
00000190  83 D9 01 75 0E FF 53 FC EB 26 AC D1 E8 74 2F 13  ƒÙ.u.ÿSüë&¬Ñèt/.
000001A0  C9 EB 1A 91 48 C1 E0 08 AC FF 53 FC 3D 00 7D 00  Éë.‘HÁà.¬ÿSü=.}.
000001B0  00 73 0A 80 FC 05 73 06 83 F8 7F 77 02 41 41 95  .s.€ü.s.ƒø.w.AA•
000001C0  8B C5 B6 00 56 8B F7 2B F0 F3 A4 5E EB 9B AD 85  ‹Å¶.V‹÷+ðó¤^ë›.…
000001D0  C0 75 90 AD 96 AD 97 56 AC 3C 00 75 FB FF 53 F0  Àu..–.—V¬<.uûÿSð
000001E0  95 56 AD 0F C8 40 59 74 EC 79 07 AC 3C 00 75 FB  •V..È@Ytìy.¬<.uû
000001F0  91 40 50 55 FF 53 F4 AB 75 E7 C3 00 00 00 00 00  ‘@PUÿSô«uçÃ.....
00000200  33 C9 41 FF 13 13 C9 FF 13 72 F8 C3 66 51 00 00  3ÉAÿ..Éÿ.røÃfQ..
00000210  73 51 00 00 00 00 00 00 00 50 40 00 30 01 40 00  [email protected].@.
00000220  8D 10 40 00 00 10 40 00 55 03 89 E5 83 EC 0C B8  ..@[email protected].‰åƒì.¸
00000230  81 20 40 C3 D1 45 FC E1 03 53 08 F8 70 20 E1 11  . @ÃÑEüá.S.øp á.
00000240  F4 8B EC 08 03 CE 1C 8A 1E 1E 5D FC 73 3E FC 88  ô‹ì..Î.Š..]üs>üˆ
00000250  38 B0 37 50 26 B1 83 C0 01 BF 2C 3F 5B B7 18 18  8°7P&±ƒÀ.¿,?[·..
00000260  60 89 EC 5D C2 70 04 AB 46 BD 33 36 FC 66 2F 40  `‰ì]Âp.«F½36üf/@
00000270  08 6C 70 3C 18 74 05 FF 18 EB EF 0D 31 C0 50 8D  .lp<.t.ÿ.ëï.1ÀP.
00000280  10 A2 76 04 7C 08 1B B8 F5 FF 02 F7 41 15 28 30  .¢v.|..¸õÿ.÷A.(0
00000290  B2 DA 0F 2C 28 47 E2 B8 0E A1 82 50 E8 AB BD 45  ²Ú.,(Gâ¸.¡‚P諽E
000002A0  B1 01 9F A3 B3 BA 21 B7 B7 0B F5 26 08 0F AF 94  ±.Ÿ£³º!··.õ&..¯”
000002B0  A1 05 1C 83 F8 0A 7E 6A 0A 80 C1 E0 02 8B 80 13  ¡..ƒø.~j.€Áà.‹€.
000002C0  DD 13 94 1D 2D 1A 01 D8 10 20 17 E3 02 89 83 84  Ý.”.-..Ø. .ã.‰ƒ„
000002D0  8D 25 31 70 09 7E DE 9A AA 36 27 85 6F 0D EB 0A  .%1p.~Þšª6'…o.ë.
000002E0  40 18 0C FF 50 0D 25 EB 53 8C 74 0A 27 7D 49 4F  @..ÿP.%ëSŒt.'}IO
000002F0  F3 80 7F 0A A1 52 B7 0A 15 75 24 1D A4 04 C8 9D  ó€..¡R·..u$.¤.È.
00000300  FE 19 0C DD 14 22 53 B7 11 05 0C AD 2A B8 11 23  þ..Ý."S·....*¸.#
00000310  44 C0 E9 55 10 F0 80 00 00 20 40 00 30 E3 C3 98  DÀéU.ð€.. @.0ãØ
00000320  31 32 33 03 34 35 36 37 38 39 38 16 0A 72 C0 00  123.4567898..rÀ.
00000330  D1 4F 40 00 28 38 30 40 07 4B 45 52 4E 30 4C 33  ÑO@.([email protected]
00000340  32 2E 75 44 ED C0 80 47 1C 65 74 53 83 64 48 61  2.uDíÀ€G.etSƒdHa
00000350  6E A3 6C D6 1C 1F 57 72 69 FB BA 46 51 0B 80 00  n£lÖ..WriûºFQ.€.
00000360  00 00 00 00 D1 4F 40 00 4C 6F 61 64 4C 69 62 72  ....Ñ[email protected]
00000370  61 72 79 41 00 47 65 74 50 72 6F 63 41 64 64 72  aryA.GetProcAddr
00000380  65 73 73 00 00 00 00 00 00 00 00 00 00 00 00 00  ess.............
00000390  00 0C 50 00 00 E9 BE AF FF FF 00 00 00 02 00 00  ..P..龯ÿÿ......
000003A0  00 0C 50 00 00                                   ..P..
\$\endgroup\$
4
  • \$\begingroup\$ Can't you save header by using a .com file? \$\endgroup\$
    – TWiStErRob
    Commented Nov 15, 2015 at 9:26
  • \$\begingroup\$ @TWiStErRob That would be no fun. (Plus no one would be able to run it). \$\endgroup\$
    – user42643
    Commented Nov 15, 2015 at 10:07
  • 1
    \$\begingroup\$ I thought Dosbox or a VM with WinXPx86 downwards would do it. \$\endgroup\$
    – TWiStErRob
    Commented Nov 15, 2015 at 10:12
  • \$\begingroup\$ @mınxomaτ - you should take a look at clinkster unless you're already familiar with it. It's a 4k synth - the output of a 2m38s Renoise track built into an exe alongside the synth, using crinkler is 2,838 bytes. It's 3 times the size of your program, grabs 100MB of mem and thrashes 1 core of an i7 3537u for 23 seconds before the music starts. It's pretty impressive! Right up there with "Farbrausch - fr-041: debris". (Also small point - you've mistakenly said that the word size of a 32 bit program is 16 bits) Here's Clinkster's page at Pouet: pouet.net/prod.php?which=61592 \$\endgroup\$
    – enhzflep
    Commented Nov 15, 2015 at 12:26
2
\$\begingroup\$

Ceylon (on JVM), 92 87 78 bytes

(The Big integer type Whole which is used here does only exist on the JVM, not when compiling for JavaScript.)

This is the third improvement (78 bytes):

import ceylon.math.whole{o=one}shared void run(){loop(o)(o.plus).each(print);}

The loop function is a functional version of the while-statement – it creates a stream from a starting value (one) and a function (one.plus), applying the function to the previous value until it returns finished (which our function never does, so it becomes an infinite stream). On each element, print(...) is applied.

An Integer version which goes only until just under 2^63 (on JVM) or 2^31 (on JS), then will wrap around to negatives (JVM) or stall (for JS), for 48 bytes:

shared void run(){loop(1)(1.plus).each(print);}


Second version (87):

import ceylon.math.whole{o=one}shared void run(){{o}.cycled.scan(o)(plus).each(print);}

That builds first an infinite stream of ones, and then creates a scanning stream, which calculates the partial sums (which then are each printed).


Original version (92):

A straight-forward n++-loop, with a big integer type (called Whole in Ceylon, but the type name is not actually used in this program, just the name of the 1):

import ceylon.math.whole{o=one}shared void run(){variable value n=o;while(0<1){print(n++);}}


This one is shorter (83), counting from 1 to 2^65536, but it doesn't actually work:

import ceylon.math.whole{o=one,t=two}shared void run(){(o..t^t^t^t^t).each(print);}

The problem is that the range notation x..y will call span(x,y), which will call x.offset(y) to get the offset between them, and this method is defined to return an Integer – which will overflow here, as the difference is much too large to fit into an Integer (which was the whole point of using Whole here).

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

C + gcc extensions, 77 bytes

char c[]={[0 ...49]=48};main(a){for(;;puts(c))for(a=49;++c[a]>57;c[a--]=48);}
\$\endgroup\$
2
\$\begingroup\$

Common Lisp, 30 bytes

(do((i 0))(())(print(incf i)))

or

(do((x 0(1+ x)))(())(print x))

This should get to 2128 without a problem, since Common Lisp has bignums. E.g.:

CL-USER> (print (expt 2 128))
; 340282366920938463463374607431768211456
\$\endgroup\$
2
\$\begingroup\$

JacobFck, 10 Bytes

This is so short!

^1:m^1+>_m

See expanded and commented here.

\$\endgroup\$
3
  • \$\begingroup\$ Does this work up to 2^128? \$\endgroup\$
    – Dennis
    Commented Dec 29, 2015 at 20:43
  • \$\begingroup\$ I feel that 10 bytes are pretty long. \$\endgroup\$
    – user85052
    Commented Dec 14, 2019 at 7:09
  • \$\begingroup\$ All links are dead. \$\endgroup\$ Commented Oct 3, 2020 at 22:06
2
\$\begingroup\$

JavaScript, 80 bytes

r=t=>{t=a.pop()||0;if(++t>9){r();t=0}a.push(t)};for(a=[1];;r())alert(a.join(''))

To test at 2^128 in console, for 1000 cycles;

r=t=>{t=a.pop()||0;if(++t>9){r();t=0}a.push(t)};for(x=1,a=[3,4,0,2,8,2,3,6,6,9,2,0,9,3,8,4,6,3,4,6,3,3,7,4,6,0,7,4,3,1,7,6,8,2,1,1,4,5,6];x<=1000;r(),x++)console.log(a.join(''))

And to test all 9s for 2 cycles;

r=t=>{t=a.pop()||0;if(++t>9){r();t=0}a.push(t)};for(x=1,a=[9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9];x<=2;r(),x++)console.log(a.join(''))

My original idea was to actually use objects, but the array idea turned out to be super small. Mine should be the smallest JavaScript one at the moment

Object Oriented: 107 bytes

function d(p){var x=0,s="";this.o=q=>{return++x>9&&(x=0,p=p||new d,s=p.o()),s+x}}for(c=new d;;)alert(c.o())
\$\endgroup\$
4
  • \$\begingroup\$ How does this work? Strings? \$\endgroup\$ Commented Apr 1, 2016 at 12:18
  • \$\begingroup\$ Also, be sure to avoid the maximum call stack size, which is 19299. \$\endgroup\$ Commented Apr 1, 2016 at 12:20
  • \$\begingroup\$ Each digit is an item in an array. And a function is defined so that the last item is popped off and 1 is added to that item. If the item is 10 then it recursively calls the function popping off the next item and so on. If it is not 10 then it just pushes the item back onto the array. \$\endgroup\$
    – Adam Dally
    Commented Apr 2, 2016 at 0:40
  • \$\begingroup\$ Therefore, the stack size is equal to the number of digits. And 19299 digits leaves the largest number capable of running in this script to be 9.99999.... x 10^1929.9, much much larger than the number of atoms in the observable universe. The fastest computer in the world can do 33.86 x 10^15th operations per second, which would take 3.38 x 10^16 seconds if each operation were an entire iteration, or about a billion years, before the stack reached its max call size. :) \$\endgroup\$
    – Adam Dally
    Commented Apr 2, 2016 at 1:03
2
\$\begingroup\$

awk , 65 bytes

BEGIN{for(;;){for(;++$++i>9;)$i=0;for(i=NF+1;i;)printf--i?$i:RS}}

Counts up "forever" using the fields as an array. I made sure this works with the standard Ubuntu awk (mawk).

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

GNU sed, 36

(score includes +1 for requiring the -r flag)

:=
p
s/$/1/
:
s/(^|_)1{10}/1_/
t
b=

This counts in unary-coded decimal, and must be primed with the starting number in UCD (e.g. 1) on stdin:

$ ./63834.sed <<<1 | head -n 30
1
11
111
1111
11111
111111
1111111
11111111
111111111
1_
1_1
1_11
1_111
1_1111
1_11111
1_111111
1_1111111
1_11111111
1_111111111
11_
11_1
11_11
11_111
11_1111
11_11111
11_111111
11_1111111
11_11111111
11_111111111
111_

Here's a version that takes input and output in decimal (now using x as the digit and 0 as separator):

#!/bin/sed -rf

s/[1-9]/0&/g
s/[5-9]/4&/g
y/8/4/
s/9/4&/g
s/4/22/g
s/[37]/2x/g
s/[26]/xx/g
s/[1-9]/x/g


:=
h
s/0x/-x/g
s/xx/2/g
y/x/1/
s/22/4/g
s/44/8/g
s/81/9/g
s/42/6/g
s/21/3/g
s/61/7/g
s/41/5/g
s/-//g
p

g
s/$/x/
:
s/(^|0)x{10}/x0/
t
b=

This is the same code, but with UCD/decimal conversion going on. This makes it easier to make it start at 2^128:

$ bc <<<'2 ^ 128' | ./63834.sed | head -n 10
340282366920938463463374607431768211456
340282366920938463463374607431768211457
340282366920938463463374607431768211458
340282366920938463463374607431768211459
340282366920938463463374607431768211460
340282366920938463463374607431768211461
340282366920938463463374607431768211462
340282366920938463463374607431768211463
340282366920938463463374607431768211464
340282366920938463463374607431768211465
\$\endgroup\$
2
\$\begingroup\$

Prolog (SWI), 35 bytes

p:-between(1,inf,N),writeln(N),1=2.

Edit: Saved 6 bytes thanks to @Fatalize

\$\endgroup\$
2
  • \$\begingroup\$ A 35 bytes solution is possible, using the SWI-Prolog predicate between/3 : p:-between(1,inf,N),writeln(N),1=2.. It will count up to infinity (inf) because of the 1=2 which will induce backtracking after each integer printed. \$\endgroup\$
    – Fatalize
    Commented Feb 2, 2016 at 15:17
  • \$\begingroup\$ @Fatalize: oh right, you can use inf like that. \$\endgroup\$
    – Emigna
    Commented Feb 2, 2016 at 19:58
2
\$\begingroup\$

GNU coreutils, 16 bytes

seq `bc<<<2^225`

GNU seq happily handles values over 2^128. 2^225 is the largest value bc will print without wrapping (it assumes 80-character width), so that's what I've used here.

Here's the proof that we can handle values higher than 2^227 (dc wraps one char later than bc):

$ seq `dc -e2d227^p` `dc -e2d227^10+p`
215679573337205118357336120696157045389097155380324579848828881993728
215679573337205118357336120696157045389097155380324579848828881993729
215679573337205118357336120696157045389097155380324579848828881993730
215679573337205118357336120696157045389097155380324579848828881993731
215679573337205118357336120696157045389097155380324579848828881993732
215679573337205118357336120696157045389097155380324579848828881993733
215679573337205118357336120696157045389097155380324579848828881993734
215679573337205118357336120696157045389097155380324579848828881993735
215679573337205118357336120696157045389097155380324579848828881993736
215679573337205118357336120696157045389097155380324579848828881993737
215679573337205118357336120696157045389097155380324579848828881993738
\$\endgroup\$
1
  • \$\begingroup\$ I discovered yesterday that GNU 'seq' will accept inf as the upper limit, but it then falls back to scientific notation (printf '%g' format), so it didn't help in the way I wanted... \$\endgroup\$ Commented Feb 19, 2016 at 9:25
2
\$\begingroup\$

Bash, 31, 47 46 bytes

for((i=0;;i=`echo "$i+1"|bc`));do echo $i;done

I'm a bash novice but I've been wanting to use bash in code golf for a while. There must be a better way than this though, I'm sure it can be beaten!

I believe this fixes the issues.

> a=$((2**128))    # Too big
> echo $a
0

> a=$(echo "2^128"|bc)    # Works!
> echo $a
340282366920938463463374607431768211456

So all expressions are piped through bc to make sure it can handle them. It returns a string representation to the variable, which can then be printed. Open to golfing, maybe I can use bc better or maybe I can do code substitution better.

\$\endgroup\$
5
  • \$\begingroup\$ Does this satisfy the requirement of being able to reach 2^128 without running out of memory? \$\endgroup\$ Commented Feb 2, 2016 at 15:05
  • \$\begingroup\$ Honestly, I don't know! Aside from letting it run, how would I find out? \$\endgroup\$
    – Ogaday
    Commented Feb 2, 2016 at 15:06
  • \$\begingroup\$ What size are bash's integers? \$\endgroup\$ Commented Feb 2, 2016 at 15:07
  • 1
    \$\begingroup\$ I'm getting the impression it's system defined, and probably won't be able to hit 2**128 \$\endgroup\$
    – Ogaday
    Commented Feb 2, 2016 at 15:21
  • \$\begingroup\$ Bash only has 64-bit signed integers so you'll always be limited to 2^63-1 unless you outsource to bc, dc or similar. Or just use bc standalone... \$\endgroup\$ Commented Feb 13, 2018 at 19:23
2
\$\begingroup\$

F#, 50 49 36 bytes

let rec(!)d=printfn"%O"d;!(d+1I)
!1I

BigIntegers made this pretty easy, although I didn't know I could access them this easily in F#. =)

Update: It's even easier! You don't have to use bigint 1 when you can use 1I! =D

Without using BigInteger, I was able to get it down to 153 bytes. Note that this uses a base 256 representation, with a space between the digits and a newline between the numbers:

let rec i=function|255::[]->0::[1]|255::r->0::(i r)|n::r->(n+1)::r
let rec f d=printfn"%s"(List.rev d|>Seq.fold(fun s c->s+" "+(string c))"");f(i d)
f[0]

The function revolves around treating a list of byte-sized integers as a big number. The recursive function i increments that number (carrying changes up the digits by calling itself if needed), while the recursive function f prints the list as a number, increments the list, then calls itself.

Overall, a pretty fun problem. Thanks! =)

Saved a byte thanks to a trick I saw @Lynn do: name your functions with a character to call them more concisely. Thanks! =)

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

Hoon, 25 bytes

$:|=(a/@ ~&(a $(a +(a))))

Just repeatedly prints the sample of the gate, and recurses with a = a+1. Hoon's native number is a bignum backed by gmp, so it should be able to count up to 2^128 just fine. Simply paste that code into the Hoon repl (:dojo) in Urbit to run it.

It does abuse the feature of Hoon that gates are instantiated with a default sample, though. Pulling the $ arm for the gate runs it immediately, with the sample set to that default instead of having to call it with (f 0). It's actually smaller to do it this way instead of =+(a=0 |-(~&(a $(a +(a)))))

\$\endgroup\$
2
  • \$\begingroup\$ Is all of the whitespace necessary? \$\endgroup\$ Commented Feb 29, 2016 at 1:16
  • \$\begingroup\$ Unfortunately, yes. It's the seperation of twigs: ~& takes two expressions, one to print and one to evaluate to. a(b c) is sugar for recursing to a with b set to c, so they have to be seperate expressions as well. The a/@ part is the type signature of the gate, and is even in a different syntax (tile syntax instead of twig). \$\endgroup\$ Commented Feb 29, 2016 at 1:20
2
\$\begingroup\$

Aubergine, 61 bytes

=A1+i1
=bi-b1-b1:Ba+b1=oB+A1=aA-a1-ii               =oB-a1-ii

Try It Online! but be sure to hit Kill quickly so as to not crash your browser!

Prints in unary, one number per line. The Python interpreter, at least, will ensure it runs forever. (Make sure it is EXACTLY 61 bytes if you run it: adjust the number of spaces in the middle if it is not.)

How it works:

=A1                      SET MEM[0]=1 (this will be the counter).
   +i1                   JUMP 1 byte forward. (Skips the newline.)
   =bi-b1-b1             SET b=5 (points to the '1'=49 on the prev line)
   :Ba                   JUMP to 49 if a>0 (Will land on the other side of the spaces)
   +b1                   SET b=6 (now points to the newline)
   =oB                   OUTPUT what b points to (the newline)
   +A1                   INC MEM[0]
   =aA                   COPY MEM[0] INTO a (but we haven't printed the prev num yet!)
   -a1                   DEC a (Now a=the prev num)
   -ii                   JUMP to the beginning
                        (The sixth instruction jumps here when a>0:)
      =oB                OUTPUT contents of b ('1')
      -a1                DEC a
      -ii                JUMP to the beginning
\$\endgroup\$
1 2
3
4 5
8

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.