177
\$\begingroup\$

I think the question as above is clear, but just in case:

  • Write a full program (not just a function) which prints a positive base 10 integer, optionally followed by a single newline.

  • Qualifying programs will be those whose output is longer (in bytes) than the source code of the program, measured in bytes (assuming ASCII or UTF-8 encoding for the program source code).

    I.e. the code must be shorter than the number of digits in the resulting number.

  • Leading zeros are disallowed under all circumstances. Counting leading zeroes trivialises the problem; ignoring leading zeros unnecessarily complicates the question.

  • The winning program will be the qualifying program which prints the integer with the smallest magnitude.

Leaderboard snippet

var QUESTION_ID = 67921;
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;
function answersUrl(index) { return "https://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 "https://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) { if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); }
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*)(?:,|[-\u2013] ).*?([\d,^!e+]+)(?=\:?[^\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.replace(/<sup>([^\n<]*)<\/sup>/g, "^$1").replace(/\(\d+(?:\^\d+,)? [\w\s]+\)/g, "").replace(/floor\(10\^(\d+)\/9\)/g, "$1 ones").replace(/(\d+) ones/g, function (_, x) { return Array(+x + 1).join(1); }).match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2].replace(/,/g, "").replace(/(\d+)\s*\^\s*(\d+)/, function (_, a, b) { return Math.pow(a, b); }).replace(/(\d+)!/, function (_, n) { for (var i = 1, j = 1; i <= n; i++) j *= i; return j; }), 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(), 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 src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
<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>

\$\endgroup\$
  • 62
    \$\begingroup\$ Number 1 on the Hot Network Questions. Not bad for a first question... \$\endgroup\$ – trichoplax Dec 28 '15 at 15:15
  • 6
    \$\begingroup\$ @Kslkgh Strictly less than, otherwise the question is trivial for programs which implicitly print their last value. \$\endgroup\$ – Arandur Dec 28 '15 at 15:57
  • 6
    \$\begingroup\$ Is 1.0 an integer? \$\endgroup\$ – histocrat Dec 28 '15 at 20:05
  • 22
    \$\begingroup\$ The restriction to UTF-8 is ridiculous and detrimental. Bytes are bytes, no matter the encoding. I strongly recommend that you change the rules, as as they currently are they disallow languages that are not character-based (e.g. Minecraft, Piet, Folders) or have longer UTF-8 byte counts than their "real" (valid according to this question) byte counts (e.g. APL, TI-BASIC, Seriously, Jelly). \$\endgroup\$ – lirtosiast Dec 29 '15 at 3:35
  • 7
    \$\begingroup\$ @ZachGates that's not how the HNQ list works. ;) \$\endgroup\$ – Martin Ender Dec 29 '15 at 8:38

138 Answers 138

2
\$\begingroup\$

><> (fish), 1000010000

a:*:*:nn;

explaination:

duplicates and multiplies 10 to get to two 10000's then prints both, being ten total characters long, one more than the program character length

\$\endgroup\$
  • \$\begingroup\$ First reaction : meh, there must be something better, let's try using a loop, using ascii values as integer or even exiting with an error ! 1 hour later : <strike>damn you</strike> well done torcado ! \$\endgroup\$ – Aaron Mar 8 '16 at 16:07
  • \$\begingroup\$ @Aaron haha yeah I also thought there was for sure a shorter way to do this but this is the best I could get. Thanks! \$\endgroup\$ – torcado Mar 16 '16 at 17:00
2
\$\begingroup\$

Befunge 98 - 10000000000:

a8k:8k*.@

or:

a8k:8k*.q  

The number printed has twelve didgits and the program is eleven bytes long.

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

R - 134217728

cat(8^9)

I doubt it can be make any shorter having to use cat function. Pretty boring, though.

Edit: Improved thanks to Christian Sievers

\$\endgroup\$
  • \$\begingroup\$ My version answers with "1e+09", and anyway, why not 8^9? \$\endgroup\$ – Christian Sievers Sep 21 '16 at 11:26
2
\$\begingroup\$

Mathematica, score 120

5!

Contrary to the assertion in this answer, you don't need Print to print something in Mathematica. Mathematical operations like exponentiation and factorials are supported by default.

\$\endgroup\$
  • \$\begingroup\$ why did you change your score from 120 to 1024? \$\endgroup\$ – Sparr Dec 28 '15 at 22:27
  • \$\begingroup\$ @Sparr It was a mistake, thanks for pointing it out. \$\endgroup\$ – March Ho Dec 28 '15 at 22:28
  • \$\begingroup\$ @glenn I was under the impression the length of the number must be longer than the program itself. \$\endgroup\$ – March Ho Dec 28 '15 at 22:33
  • 1
    \$\begingroup\$ "Write a full program (not just a function)"; this isn't a full program, it's a REPL snippet. \$\endgroup\$ – LegionMammal978 Dec 28 '15 at 23:57
  • \$\begingroup\$ I think this would be a valid TI-BASIC answer too. \$\endgroup\$ – kamoroso94 Dec 4 '17 at 2:58
2
\$\begingroup\$

Cubically, score 836,308,545 328,426,785 45,454,545 1,818

Knocked the score down by utilizing existing face values instead of wasting space messing with the notepad. Too bad I golfed it before the revision history kicked in :(

%22

Try it online! Explanation:

  • Functions are called strangely in Cubically. When the interpreter hits a function, it sets the internal "default function" to that function. Then, when it hits any integer, it calls the internal default function with that integer. So R1 will call R with 1, R11 will call R with 1 twice, etc.
  • %22 prints the value of the front face (18) two times.

Outdated:

Fun fact! Due to how functions are called in Cubically, for each extra byte (6 at the end) we add, we can multiply the output length. Example:

:5*6666%66

Prints 836308545836308545. :5*6666%666 prints 836308545836308545836308545. Etc.

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

Z80Golf, 123334567 (8 bytes)

00000000: 3cf6 30fe 3820 0176                      <.0.8 .v

Try it online!

Disassembly:

  inc a
  or '0'
  cmp '8'
  jr nz, ok
  halt
ok:

Runs as follows:

  • A is incremented, then or'd by '0', giving '1'.
  • This is not '8', so we jump past the halt.
  • Code runs through many NOPs into putchar ($8000). We print '1'. Then we effectively return: The PC is set to (SP) which is $f63c, the first word in memory (i.e. the first word of our code) – we never pushed anything, so we're stack-underflowing into our own code, interpreting it as 16-bit addresses. SP is incremented by 2, now $0002.
  • PC runs through NOPs from $f63c to $0000, the start of our code. A is incremented to '2'.
  • We jump past the halt again and run into $8000. We print '2', then jump to $fe30 and SP is now $0004. We run from there into $0000, the start of our code again.
  • We similarly print '3', but the next return address we pop is $2038, which means the PC reaches $8000 again before it reaches our code; so we print '3' again, then returning to $7601 and thus printing '3' a third time; before finally returning to $0000 where execution continues as normal. Now SP is $000a.
  • Nothing is weird past here: the return address will always be $0000 since SP is now pointing after our code. We count up printing '4', '5', '6', '7' until finally A = '8' and we halt.
\$\endgroup\$
2
\$\begingroup\$

Alchemist, 11109876543210

a->Out_a!12a

Try it online!

Down from ~1e18 to ~1e14 thanks to @Jo King

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

F#, 10000000000000000000

Here's a pretty clever trick:

printf"%o"(1L<<<57)
\$\endgroup\$
1
\$\begingroup\$

Bash, 100000000 (9 digits)

bc<<<A^8

Solution by @Digital Trauma ported to bash.

\$\endgroup\$
  • \$\begingroup\$ crap, I took a break from my answer before coming back to finish tidying it up and post it, and you snuck in ahead of me >.< \$\endgroup\$ – Peter Cordes Dec 29 '15 at 4:58
1
\$\begingroup\$

Pure bash, 1125899906842624 (250, 16 digits).

echo $((1<<50))

This is the best pure bash I've come up with, not depending on any special conditions or using external commands.

Bash + POSIX, 100000000: (108, 9 digits)

bc<<<A^8   # bc with a here-string script that evaluates 10^8

Thanks to Digital Trauma's answer for bringing up bc and the fact that it accepts hex digits even with the default ibase=10. Glenn beat me to actually posting the bc-based answer, but I'll leave it in my answer as well for completeness.


I'm an infrequent golfer, so IDK if there are standard assumptions that rule this out, to avoid needing overly pedantic rules in every question:

Bash + rule-bending: 10737418240 (5 * 231, 11 digits)

The question doesn't appear to rule out the the long number being be part of a longer string (e.g. part of an error message):

$((5<<31))          # prints "bash: 10737418240: command not found"

Bash assuming a 5-digit PID: a 10-digit number (PID repeated twice)

echo $$$$       # depends on the shell's current PID being a 5-digit number

Or repeating 3 times to still work with a 4-digit PID: a 12 or 15 digit number. (e.g. 573057305730: The shell's PID repeated 3 times).

echo $$$$$$     # depends on the shell's current PID being a 4-digit number

test framework for the number-generating expression:

e='$((5<<31))'; eval decimal=$e;  echo "$decimal: srclen=${#e}  digits=${#decimal}"

test framework for whole commands (works with methods other than the error-message hack.)

e='bc<<<A^8'; eval decimal=\"\$\($e\)\";  echo "$decimal: srclen=${#e}  digits=${#decimal}"

up-arrow and edit e to try different numbers.


Ideas that didn't work: (for readability, not fully golfed and in a (subshell) to avoid breaking your interactive shell when testing.)

(set {1..9}; IFS=; echo "$*" )   # $* in double quotes joins with no separator if IFS is null.  Too much setup overhead

You could play silly tricks to get $0 to contain a long number, but then you'd have to count the whole bash -c 'echo $0' $((10**7)) as part of the program.

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

ResPlicate, score 111111111

2 9 0 49

I was thinking of all kinds of complicated ways to do this, but it turns out the solution is very simple. After one step, you get:

0 49 0 49 0 49 0 49 0 49 0 49 0 49 0 49 0 49

which is just 9 commands to print ASCII number 49, so "111111111" is output.

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

C, score ~10^26 (100000000000000004764729344)

main(){printf("%f",1E26);}

Apparently float failes there a little. Tested there.

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

BotEngine, floor(1065/9)

v
e1
ldddddd<
ldddddd<
ldddddd<
ldddddd<
ldddddd<
>ddddP

This is probably the largest number of d instructions I've ever used in a single BotEngine program.

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

Candy, 10

N

push the number 10 onto the stack. 10 is useful since it's ascii for \n

Candy dumps the stack on exit.

\$\endgroup\$
  • 2
    \$\begingroup\$ BTW, your score is the number printed, not the length of the code. \$\endgroup\$ – ETHproductions Dec 29 '15 at 20:29
  • \$\begingroup\$ @ETHproductions oh, should I have printed something like 10 (1 digit)? \$\endgroup\$ – Dale Johnson Jan 1 '16 at 2:15
1
\$\begingroup\$

x86 MS-DOS .COM file, score 111,111,111,111

Hex dump of the 11 byte .COM file (to reverse the hex dump, pass it into xxd -r -seek -256):

0100: b4 02 b2 31 b1 0c cd 21 e2 fc c3                   ...1...!...

Unassembled using debug:

0100 B402     MOV AH,02         ; prepare to print character to stdout
0102 B231     MOV DL,31         ; ASCII '1' to be printed
0104 B10C     MOV CL.0C         ; counter=12
0106 CD21     INT 21            ; print
0108 E2FC     LOOP 0106         ; repeat until counter is 0
010A C3       RET               ; end
\$\endgroup\$
1
\$\begingroup\$

Binary-Encoded Golfical, floor(1014/9)

Hexdump of binary encoding:

00 90 01 00 31 18 18 18 18 18 18 18 1D

Original image:

enter image description here

Magnified 80x, with color labels:

enter image description here

Explanation: Stores 49 (the code point of the digit 1), prints it as a character 7 times, then turns around and prints it seven more times before terminating.

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

ForceLang, 1013

Noncompeting, language postdates the question.

io.write 1e13
\$\endgroup\$
1
\$\begingroup\$

Cy, score 134217728

8 9 ^ :<

8 9 pushes a 9 and an 8 to the stack (duh), ^ computes 8 ^ 9 (exponent, not xor), :< prints it.

This was not intended to be a golfing language, but this answer ended up pretty short compared to "real" languages. I didn't even need exponential notation!

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

beeswax, 909090 823543

The new solution just computes 7^7, which is 85547 smaller than the old solution.

_7FB{

Explanation:

_       [0,0,0]•       create bee
 7      [0,0,7]•       push 7 on lstack
  F     [7,7,7]•       set all stack values to 1st value
   B    [7,7,823573]•  1st=1st^2nd
    {                  output lstack 1st to STDOUT
\$\endgroup\$
1
\$\begingroup\$

marioLANG, 700666666005555550044444400333333002222220011111100 (51 digits for 49 bytes)

still no marioLANG anwser? well here's one,

Try it online

+
+
+
+
+
+!::::::<
+#======"
:>)::(-[!
="======#
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 15625

=5^6

equals 15625

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

Reng v.3.3, score 104976

Try it here!

I²²n~

Prints

104976

This squares I (18) twice, yielding 104976. n prints this, and ~ terminates.

Reng v.2.2, score 101010101010

{An}``````~

Previously, score 96549157373046880

Z{ZZ**}#xxxxxxn~

Prints 96549157373046880.

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

Jelly, score 100

³

Prints 100 when counting as 2 bytes in UTF-8.

Try it Online!

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

Charcoal (non-competing), 11111111

F⁸1

Try it online!

for(8)Print('1').

Charcoal could have done better (1111 score) if source was measured in Charcoal bytes.

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

Carrot, 11111

1^*4

1     //"1" is pushed into the stack
 ^    //The stack is finished
  *   //Multiply or duplicate string operator (in this case it is the latter)
   4  //Duplicate the string 5 times (4+1)

"1" is pushed into the stack. Then the * operator duplicates the string 5 times, not 4 times because the * operator on strings always duplicates the strings 1 time more than what is stated, because duplicating it once is meaningless and hence it is removed. So the program outputs 11111.

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

7, 16031, language postdates challenge

1603

Try it online!

It's unclear what "must be encoded in ASCII or UTF-8" means in 7. The most obvious meaning is that I have to write each command as a single character, and encode its name in ASCII (this is particularly suitable here, because that's an input encoding that the 7 interpreter understands). In this case, the best I can do is 16031 (the first character here is a literal that's never executed, thus could be any octal digit; 0603 outputs 06031, but leading zeroes are disallowed, so the best digit we can use here is 1).

Explanation

A 7 program is basically a sequence of literals that are pushed to the stack, unescaping them in the process; thus the "initial run" of the program is always fairly boring and just puts things on the stack. Then the rightmost stack element is executed (while leaving it on the stack), repeatedly until the program exits.

Although all the commands that can occur in literals have names, some of the commands that can appear as the result of unescaping don't. The usual convention I use for this on PPCG is to bold "active" commands (which do something interesting when executed), and leave "passive" commands (which just push an active command) unboldened. The four active commands that don't have names are then given the same name as the corresponding passive command, and thus I rely on the font to distinguish. 7's "stack" is a little strange, relying on separators | between elements (which are manipulated as though they were a character in the language).

||           1603    Initial stack and program; 6 is active, 103 are passive
|103                 Result after the program's initial run
|103         103     (implicit) Top stack element is copied to the program
|1031        03      1 appends 1 to the stack
716031       3       0 escapes the top stack element and removes the | before it
                     3 prints output, and exits if the stack is low

The output in this case is 716031. The first step in producing output is to check to see if it needs escaping (the algorithm for this is fairly complex, but typically boils down to making active commands passive, and enclosing passive sections in 76). In most of my 7 programs, it does (due to containing anonymous commands), but all the commands seen there have names (unsurprisingly, because we escaped it manually on the previous line), so no escaping is necessary. Next, the first character output in the entire program (here, 7) specifies the output format (7 means "the same as the input", so the output will be printed as a sequence of octal digits in ASCII). Finally, the remaining characters, 16031, will be printed directly (shown here without the bold because one they're printed, they aren't commands any more, just characters).

Interestingly, the victory condition is different from a , and that actually mattered. It's possible to write a smaller program that produces numerical output longer than itself: 163 outputs 71631 (basically because it doesn't escape explicitly, and implicit escaping by the 3 command always prepends a 7 to the output). However, in this case, although the program is shorter, the output is larger, so using a longer program gave a smaller score.

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

Common Lisp (GCL), 1586013445029888

(princ(exp 35))
\$\endgroup\$
1
\$\begingroup\$

Brachylog (2), 11111, language postdates challenge

1j₅w

Try it online!

Just makes five copies of 1 and prints them.

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

Python 2, 1000000000

A simple program in a simple language. I have two answers depending on wether the python shell is permitted or not.

If it isn't, then:

print 1e9

Which is 9 bytes and outputs 1000000000, 10 bytes.

If it is, then:

1e3

Which is 3 bytes and outputs 1000, 4 bytes.

These ignore the .0 printed on the end of these numbers since the method returns a float and the question asks for an int.

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

PHP, 9223372036854775807

I know they're not the shortest for PHP but..

9223372036854775807

<?=PHP_INT_MAX;

10000000000000000000

<?=str_pad(1,20,0);
\$\endgroup\$

protected by Addison Crump Dec 29 '15 at 21:34

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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