178
\$\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\$
  • 64
    \$\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
  • 23
    \$\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

139 Answers 139

1
\$\begingroup\$

Python 3, 100000000000

One digit shorter than the current python 3

print(1e11)

prints 1*(10^11)

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

Perl 6, 10000000

1e7.say

Try it online!

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

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

Attache, 8 bytes

8^9|Echo

Try it online!

Outputs 134217728.

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

Mumps, score 100000

W 1e5

Writes 10^5, or 100000. Had the newline not been optional, that would have added 2 more characters to the program:

W 1e7,!  ; this would output 10000000

Now that I mention it... it would be interesting to see how much would change on some of the winning answers if an explicit newline was required - especially the 0 or 1 byte answers. :-)

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

MSM, 10101010

.;.;.01

7 byes for an 8 byte output. Repeatedly duplicates and concatenenates starting with 10.

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

Visual Basic .NET, 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111 (86 ones)

Module A
Sub Main
System.Console.Write(New System.String("1"C,86))
End Sub
End Module

Oh dear. (This is shorter than using a loop, unless there's some fast way to do that that I don't know about.)

\$\endgroup\$
  • 1
    \$\begingroup\$ You don't need to write System.. \$\endgroup\$ – Qwertiy Mar 16 '16 at 17:37
0
\$\begingroup\$

k4, 10000

$ cat a.k; echo
_1e4
$ wc -c a.k
4 a.k
$ q a.k </dev/null
10000
$ 

The input file is missing its trailing newline, but that doesn't matter to the interpreter. The language's default behavior on reading code files is to print the output of any statement which is not explicitly suppressed, so all I have to do is create the number. Exponential notation creates floats though, so I have to floor it to create an integer, otherwise the output would be 10000f (the default representation of a float that has no fractional part).

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

dc, 117649

$ cat a.dc; echo
7 6^p
$ wc -c a.dc
5 a.dc
$ dc -f a.dc
117649
$ 

Inspired by WGroleau's comment on the question.

\$\endgroup\$
  • \$\begingroup\$ Six to the power of something? Three bytes code. What happens with five instead of six? \$\endgroup\$ – WGroleau Dec 29 '15 at 5:01
  • \$\begingroup\$ Never mind—I didn't see the seven space \$\endgroup\$ – WGroleau Dec 29 '15 at 5:07
0
\$\begingroup\$

Insomnia, 129

i

Apart from i (105), r (115) or } (125) outputs the same result, since the position of the bit pointer doesn't matter when it's the last state-changing operation.

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

Python 3, 1028071702528

print(52**7)

One byte longer than Python 2, but the same approach.

EDIT: turns out my search wasn't exhaustive enough -- thanks, Neil.

\$\endgroup\$
  • 2
    \$\begingroup\$ 52**7 is only 1028071702528. \$\endgroup\$ – Neil Dec 29 '15 at 23:12
0
\$\begingroup\$

Ceylon, 111111111111111111111111111111111111111111 (42 digits)

This was my second try:

shared void run(){print("1".repeat(42));}

A program with length 41, prints a string of 42 ones.


My first, more complicated try printed 112589990684262411529215046068469761152921504606846976 (54 digits):

shared void run(){print("``2^50````2^60````2^60``");}

This program has a length of 53 bytes.

Ceylon integers go just up to right under 2^63, which gives us only 19 digits. Due to the boilerplate here we need to concatenate three of them, which is done by the use of string templates (the contents of `` ... `` inside a string literal are evaluated as an expression and then converted into a string). Then it was simply a point of looking up the powers of two with (relatively) smallest first digits (2^50 starts with 1125, 2^60 with 1152), such that the length of them sums to 54. I guess combining other powers might make the program slightly shorter, but this still will not beat the simple one above.

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

PHP, score 10,000,000,000

echo 1e10;

Assuming the opening/closing tags don't count, otherwise it's

<?=echo 1e13;

And if we allow non-PHP, we have <?=1e7 and 1e3 as options. Taking the first answer as the "most correct" of the four.

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

Pylons, 10

B

How it works:

B # Push B to the stack. (pre-initialized to 10)
  # The stack is implicitly printed at the end of the program.
\$\endgroup\$
0
\$\begingroup\$

Oracle SQL 11.2, 30903154382632612361920641803529

SELECT''||POWER(9,33)FROM DUAL;
\$\endgroup\$
0
\$\begingroup\$

Mathcad, 3 "bytes", 4 digits, 5040

Updated version. See text of Original version for suppression of decimal places discussion. As the original version, used one integer raised to power of another, I've also included what I think is the smallest number that meets the criterion.

enter image description here


Original (and wrong!) Version ...

Here's two Mathcad expressions (you may regard the "3" version as even more cheaty than the 8 version, hence my providing the two versions ...)

enter image description here

Mathcad doesn't have a "text" file as such. It operates more as a 2D whiteboard, where you can either type characters and operators onto a "worksheet", or enter them via a single-click toolbar. Mathcad stores the worksheet as an xml file. From a user perspective, however, typing "cat" counts as 3 distinct characters, which I'm taking to be the equivalent of "bytes" for the purposes of golfing; pi is single character as well. An operator, such as "=" (evaluate), "+" (add) or ":=" (define), also counts as a "byte".

There are also a number of settings, such as the number of decimal places shown displaying a real number, which are available from a dialog box. If, for example, setting the number of decimals to zero counts as a 0 "byte" operation, then π= is the shortest one I can think of. If this is regarded as cheating from a golfing perspective, then the exponent version should meet the spirit of the competition.

\$\endgroup\$
  • \$\begingroup\$ Nice answer, but the goal of this challenge is to print an integer that has more digits than the program has bytes, e.g. print a 3-digit number with a 2 byte program. Could you please fix this? \$\endgroup\$ – ETHproductions Mar 16 '16 at 15:44
  • \$\begingroup\$ Doh! yabinm (Yet Another Brain In Neutral Moment). \$\endgroup\$ – Stuart Bruff Mar 16 '16 at 17:13
0
\$\begingroup\$

AWK, 1e21

I didn't see an AWK answer, so here's mine.

Since a 'full program' was requested, this is the best I could come up with (full program is a bit fuzzy in AWK).

awk '{$0=1e21}1'<<<1

If we don't consider how the program is called as part of the program, then the score could be dropped to about 1e13 using:

 '{$0=1e13}1'
\$\endgroup\$
0
\$\begingroup\$

Desmos, 6561

9^4

equals 6561

previous answer, 1048576

2^{20}

equals 1048576

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

Forth, 1073741824 (2^30)

1 30 << .

Try it online

If the result could be left on the stack:

100663296

96 20 <<
\$\endgroup\$
0
\$\begingroup\$

Turtlèd, 100000000000

11:[*'0l]'1

11- set register to 11

: move right by amount in register

[*'0l] loop: [* ]- while current cell is not *, '0- write zero to cell, l -move left

'1- write one

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

///, score 111111111111

/./111/....

Try it online!

/./111/ removes itself and replaces every . with 111, so .... becomes 111111111111.

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

DUP, 100100

'd$..

Put character d on stack, duplicate it, print its value twice.

Online DUP interpreter.

My Julia implementation on GitHub, with comprehensive language description.

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

SmileBASIC, 16713480 1111111 10000

?1E4

Boring.

Previous Answers: ?-#CYAN, ?"1"*7

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

Syms, 999980001 1111111 (noncompeting)

1:7;*>

Try it online!

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

Alice, 3628800 (10!)

a/P
o@

Try it online!

Explanation

a   Push 10.
/   Reflect to SE. Switch to Ordinal.
    Reflect off bottom right corner, move back NW.
/   Reflect to W. Switch to Cardinal.
a   Push 10.
    Wrap around to the end of the first line.
P   Compute 10! = 3628800.
/   Reflect to NW. Switch to Ordinal.
    Immediately reflect off top boundary, move SW.
o   Implicitly convert the result to a string and print it.
    Reflect off bottom left corner, move back NE.
/   Reflect to S. Switch to Cardinal.
@   Terminate the program.
\$\endgroup\$
0
\$\begingroup\$

tcl, 10000000000000000

puts [expr 1e16]

Outputs:

10000000000000000.0

The integer part is 1 and 16 zeros.

http://rextester.com/RWNL80143

\$\endgroup\$
  • \$\begingroup\$ Doesn't that make this answer invalid? \$\endgroup\$ – LegionMammal978 Jan 17 '17 at 2:07
  • \$\begingroup\$ @LegionMammal978: Waiting feedback from question's author. 1e17 is still an integer! \$\endgroup\$ – sergiol Jan 17 '17 at 10:16
  • \$\begingroup\$ "1e+17" has a length of 5, while your program has a length of 16 \$\endgroup\$ – 12Me21 Jan 30 '17 at 15:17
  • \$\begingroup\$ @12Me21: I don't have a + in 1e17! \$\endgroup\$ – sergiol Jan 30 '17 at 15:42
  • \$\begingroup\$ The output does \$\endgroup\$ – 12Me21 Jan 30 '17 at 15:47

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.