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

Alchemist, 11109876543210

a->Out_a!12a

Try it online!

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

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

Attache, 8 bytes

8^9|Echo

Try it online!

Outputs 134217728.

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

Z80Golf, 111111111 (8 bytes) 11111111 (7 bytes)

00000000: 3676 6636 3b3e 31                        6vf6;>1

Try it online!

Disassembly

start:
  ld (hl), $76 ; 36 76 ; overwrite the 1st byte of program to `halt`
  ld h, (hl)   ; 66    ; h = $76; hl = $7600
  ld (hl), $3b ; 36 3b ; write `dec sp` to memory $7600
  ld a, $31    ; 3e 31 ; a = '1'

The main point is what happens after the program runs through the code. Every time before reaching putchar, the PC hits dec sp at $7600. This means each run of putchar increases SP by 1 instead of 2. So the return addresses become the following:

$7600 -> $7676 -> $3666 -> $3b36 -> $3e3b -> $313e -> $0031 -> $0000

Note that, when the return address is $7676, dec sp is skipped. So the ASCII 1 is printed 8 times in total.


Previous solution, 111111111 (8 bytes)

00000000: 3e31 0609 ff10 fd76                      >1.....v

Try it online!

Disassembly

start:
  ld a,$31  ; 3e 31 ; a = '1'
  ld b,9    ; 06 09 ; b = 9
loop:
  rst $38   ; ff    ; equivalent to "call $8000" or "call putchar"
  djnz loop ; 10 fd ; b -= 1; if (b) goto loop
  halt      ; 76

Arguably less clever than Lynn's solution but still an improvement. This uses the instruction djnz which is similar to dec b then jr nz, label, but one byte shorter. If b is not touched before the loop, b starts with zero and the loop runs exactly 256 times.

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

Pascal (FPC), 10^52 1111111111111111111111111111111111111111111111 (~1.1e46)

var i:word;begin for i:=1to 46do write(1)end.

Try it online!

Obvious one, I missed it somehow at first, thinking it would be bigger number.


Previous approach - 10^52 (10000000000000000000000000000000000000000000000000000)

uses sysutils;begin write(1,Format('%.52D',[0]))end.

Try it online!

Format is a function in sysutils which gives a string from specified formatting string and array of arguments. Formatting string works pretty much like in C: % starts the part which will be replaced, D means integer in decimal format, .52 for D means the number will be written with at least 52 digits; since 0 has less than 52 digits, it is left-padded with zeroes. 1 is written before it to make a valid integer.

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

Perl 6, 10000000

1e7.say

Try it online!

I can't see any Perl6 solutions, so for completeness...

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

Java, 1e66

Shortest full program I could make:

interface A{static void main(String[]a){System.out.print(1e66);}}

With 65 bytes, this program outputs:

1e66

Try it online!

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

Yabasic/QBasic/VBA, 10000 4 bytes

Takes no input and outputs 10000to the console.

?1E4

Try it online!

\$\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\$
0
\$\begingroup\$

Canvas, 10

Try it here!

pushes 10 to the stack, then Canvas inplicitly prints the item at the top of the stack.

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

PHP, 10000000

10 million (8 digits) printed in 7 bytes:

<?=1e7;

Almost 2.5 years and I am the first to think of that? Amazing.

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

Brain-Flak, 106735757048926755512911448358783973263883108352

48 digits, 46 bytes.

(((()()()){})){({}[()]<(({({}[()])}{}){})>)}{}

Try it online! (but not really because it will error with a segment fault).

This uses the common triangular method, ({{}[()]}), but applies it recursively on the previous result. It starts with 6 as both the counter and the total and starts the loop, first decrementing the counter, then replacing the total with 2*triangle(total). Repeat a few times and it gets exponential, with each repetition roughly doubling the amount of digits.

Python code used to calculate the total:

def tri(t):
    return (t)*(t-1)/2

def f(n,t):
    for _ in range(n):
        t = tri(t)*2
    return t

print(len(str(int(f(6,6)))))

Try it online!

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

Momema, score 10000000001000000000

01000000000-8*0-8*0

Explanation:

0  1000000000  #  [0] = 1000000000
-8 *0          #  print num [0]
-8 *0          #  print num [0]
\$\endgroup\$
3
\$\begingroup\$

Vim, 11111

5i1␛

Works by inserting the character 1 five times. Try it online!

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

Commodore 64/VIC 20 BASIC, 5 PETSCII characters (excluding the carriage return)

 0?1E5

Some caveats here; firstly, Commodore BASIC adds in a white space before printing any number or numeric variable; secondly, there is no concept of true integer types in Commodore BASIC (I think that applies to all variants through to BASIC 7 on the Commodore 128); and finally LISTing the program will show its un-obfuscate form, and add in a white space after each line number, so the above symbolic listing becomes:

0 PRINT1E5

So, on that basis, it could be that this is automatically disqualified if these caveats are taken into account. Does my initial golfed version count as a valid entry?

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

Implicit, score 65

A

Pushes the ASCII value for A (65). Implicit output.

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

Underload, score 100000100000

(100000):*S
\$\endgroup\$
0
\$\begingroup\$

Braingolf, 20

Oh hey look I can golf it now thanks to niladic stuff

+

Niladic + (ie run when the stack is empty) pushes 20 to the stack, implicit output.

Braingolf, 100

Just throwing in my 2 cents (or 2 bytes, as it were)

#d

Pushes the charcode of the character 'd' to the stack, which is 100. Braingolf will automatically print the last element of the stack when the code terminates if there is no semicolon present in the code.

Note:

As with every Braingolf submission thus-far on PPCG, this is non-competing, as the language was created on 3rd May 2017.

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

Pyke, 1 byte

T

Try it here!

In Pyke, as well as in Pyth, T is a constant pre-initialised to 10.

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

Prolog(SWI), 1e+15

?-write(1e15).

Outputs (surprise surprise) 1.0e+15

Try it online!

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

Röda, score 10,000,000,000,000 (1013)

main{[10^13]}

Try it online!

Röda statement, score 1,000,000 (106)

[10^6]

The program is executed using this command (the flags are required to execute the program, so they are free):

röda -e "[10^6]" -n
\$\endgroup\$
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\$
1
\$\begingroup\$

Whitespace, 11111111111111111111111111111111111111 (38 ones)

Visible representation:

SSTTSSTTSNNSSNSSSTNSNSTNSTTSSSSNSNTTN

What it does:

     push -38
loop:
     push 1
      dup
       pnum
      add
     dup
      jn loop

Surprisingly short for a whitespace program. Other considered approaches were pushing a big integer followed by a sequence of duplicate-multiply but this proved to be less efficient.

\$\endgroup\$
35
\$\begingroup\$

MATLAB, 1,000,000,000 (109)

Also works with Octave

disp(1e9)

Never going to beat the esolangs, but just for fun, this is the smallest MATLAB/Octave will be able to do, so thought I would post it anyway.

\$\endgroup\$
  • 18
    \$\begingroup\$ The esolang answers, while valid, are kinda boring. Glad to see one that isn't! \$\endgroup\$ – Arandur Dec 28 '15 at 14:50
  • 1
    \$\begingroup\$ perl say 1e9 is slightly shorter, if you want to improve your golf score. (Though it's nowhere near the smaller integer for which this approach works...) \$\endgroup\$ – derobert Jan 3 '16 at 7:23
  • \$\begingroup\$ @derobert True. 1e1 would satisfy the problem statement and give a score of 10 (the lower, the better) (Assuming a char is 1 byte) \$\endgroup\$ – dberm22 Jan 5 '16 at 13:18
  • \$\begingroup\$ @dberm22 10 (2 characters) is not longer than 1e1 (3 characters) \$\endgroup\$ – SuperJedi224 Jan 5 '16 at 14:05
  • \$\begingroup\$ @SuperJedi224 Ahh, I read it as the magnitude of the printed number has to be larger than the bytecount, not the number of digits in the printed number. Thanks for the clarification. \$\endgroup\$ – dberm22 Jan 5 '16 at 15:36
0
\$\begingroup\$

PHP, 100 ones, 23 chars

<?=str_repeat("1",1E2);
\$\endgroup\$
  • \$\begingroup\$ @MorganThrapp Thanks, fixed it. \$\endgroup\$ – timmyRS Feb 24 '16 at 19:33
  • \$\begingroup\$ @MorganThrapp bytes.... ok, now. \$\endgroup\$ – timmyRS Feb 24 '16 at 19:36
  • 1
    \$\begingroup\$ <?=str_repeat("1",23); works for score 11111111111111111111111. \$\endgroup\$ – Esolanging Fruit May 30 '17 at 3:41
  • \$\begingroup\$ <?=str_repeat(1,21); 21 ones, 20 bytes - <?=str_pad(1,20,0); - a one plus 19 zeroes, 19 bytes \$\endgroup\$ – Titus Apr 20 '18 at 1:29
0
\$\begingroup\$

Check, score 1000010000

Non-competing as language postdates the challenge.

>10000pp
\$\endgroup\$
1
\$\begingroup\$

C, 1844674407370955161518446744073709551615 (40 digits)

main(){printf("%lu%1$lu",-1ul);}

Certainly not winning, but it's a fun challenge and this is a fun answer.

How it works:

  • The printf format specifier %lu prints a long unsigned integer.
  • The printf format specifier %1$lu prints the first long unsigned integer in printf's argument list.
  • -1ul in printf's argument list is -1 cast to an unsigned long. Since the value is negative and the integer is unsigned, it turns into the maximum value an unsigned long can hold.
\$\endgroup\$
  • 1
    \$\begingroup\$ Actually, -1ul is an unsigned long value 1, negated. (unsigned long)-1 would be -1 cast to an unsigned long. BTW, you could reduce the score a little by using -9ul. \$\endgroup\$ – Toby Speight Mar 12 '18 at 10:59
0
\$\begingroup\$

S.I.L.O.S, 28 byte, 111111111111111111111111111111 (29 ones)

x=29
lbla
print 1
x-1
if x a

Ugh! Looping is expensive!

Try it online!

\$\endgroup\$
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
4
+150
\$\begingroup\$

NTFJ, 111111111111111111111111111111 (30 ones)

Code:

#(#~~~#@::::::)$::::*****(~#^

Explanation

One the first run through we push six 49s (ASCII value for one) to set up for the loop this only runs the first time because # is used to enter the loop. On all subsequent runs the # is skipped and thus the loop is skipped as well.

After that as well as with all future runs we pop a value $ which for the first run is a 49 but for all subsequent runs is the zero used to skip the loop. We then duplicate the TOS four times and output five times decreasing the total by one. If the TOS is zero (i.e. not 49) we end the program otherwise we jump back to the first instruction immediately skipping to back to four duplications.

This outputs 30 ones and has a length of 29.

Additional solution

I thought since Conor O'Brien forgot about the : operator I'd do a solution without it to be fair to him.

#(##################~)~##~~~#@*~~##~~~~@*~~##~~~~@*$(~#^

This prints

100100100100100100100100100100100100100100100100100100100

I was going to make an explanation for this but I forgot how it works.

\$\endgroup\$
  • \$\begingroup\$ Wow, I like the alternative solution. Fantastic! \$\endgroup\$ – Conor O'Brien Oct 24 '16 at 11:17
6
\$\begingroup\$

C++, 1e46

#include <stdio.h>
main(){printf("1%046d",0);}

Newline would require an additional 2 bytes, using format "1%048d\n"

\$\endgroup\$
  • \$\begingroup\$ I don't think C++ allows you to omit the return type of main(). OTOH, this would make a good C answer... \$\endgroup\$ – Toby Speight Mar 12 '18 at 11:01

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.