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>

  • 61
    Number 1 on the Hot Network Questions. Not bad for a first question... – trichoplax Dec 28 '15 at 15:15
  • 6
    @Kslkgh Strictly less than, otherwise the question is trivial for programs which implicitly print their last value. – Arandur Dec 28 '15 at 15:57
  • 6
    Is 1.0 an integer? – histocrat Dec 28 '15 at 20:05
  • 22
    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). – lirtosiast Dec 29 '15 at 3:35
  • 7
    @ZachGates that's not how the HNQ list works. ;) – Martin Ender Dec 29 '15 at 8:38

137 Answers 137

Retina, score 1


The empty program counts the number of matches of the empty regex in the input (which is the empty string). That's exactly 1 match, so it prints 1.

Try it online.

  • 37
    ... WE HAVE A WINNER. – Arandur Dec 28 '15 at 15:11
  • 8
    Just btw, I'd say the number of matches is infinite. The empty string can repeat an unlimited number of times and be matched infinitely. – nicael Dec 28 '15 at 15:19
  • 48
    @nicael Luckily, that's not how regex works. ;) – Martin Ender Dec 28 '15 at 15:19
  • 8
    @MartinBüttner that depends on your regex engine. I've definitely encountered systems that will crap out if you ever try to match the empty string. – Sparr Dec 28 '15 at 16:30
  • 19
    @LorenPechtel that rule applies only to quine challenges where the empty program is trivially a solution in most languages. I think it's fair game here, since this empty program actually has non-trivial semantics and Retina does not have this behaviour because of challenges like this but because it's the only consistent generalisation of its behaviour for all single-line programs. – Martin Ender Dec 30 '15 at 7:27

Pyth, 10

T

First attempt at using Pyth. Having had the question clarified, it seems 10 will be the smallest number. In Pyth the letter T starts off as the number 10, so this simply prints 10 which is larger than the length of the source code. You can try it here.

  • 94
    I like how your first attempt at using Pyth is just the letter T. – djechlin Dec 28 '15 at 15:34
  • 8
    Well, I thought this would be the most readable Pyth program I'd see for a while, but this is impressively easy to understand. – Deusovi Dec 30 '15 at 7:41
  • 1
    This is a polygot, this works in 05AB1E too. 05ab1e.tryitonline.net/#code=VA – Magic Octopus Urn Oct 24 '16 at 16:46

bc, 10

A

Luckily, bc prints the result of the last expression by default. A is interpreted as a hex digit, so results in 10.

  • 1
    But there're already (at least) 5 similar answers, including the solution with A, because many golfing languages are defining A as 10. – nicael Dec 28 '15 at 18:42
  • 45
    @nicael Yes, that's true. I claim this answer is different because bc is not a golfing language. It is in fact a Posix-defined language available by default on just about any standard *nix system you can find. – Digital Trauma Dec 28 '15 at 18:45

Fishing, score 7,958,661,109,946,400,884,391,936 1,208,925,819,614,629,174,706,176

Is this the highest non-trivial-looking score ever in a minimization challenge? (Even though it has been golfed by 84.8%)

v+CCCCCCCCCC
  `32`nSSSSP

Explanation

v              Sets the casting direction to down
 +             Increments the casting distance by 1
  CCCCCCCCCC   Casts the rod

`32`         Pushes a string "32" to the stack
    n        Converts the stack from a string to an integer
     SSSS    Repeated squaring of the stack
         P   Prints the stack

The number is 32^16 and has 25 digits. The code is 24 bytes long. The previous answer was 6^32.

  • 1
    Brought a tear to my eye. Thank you for your contribution. – Arandur Dec 28 '15 at 14:53
  • 27
    It will probably still beat Java... Hopefully. – Arcturus Dec 28 '15 at 14:53
  • 2
    Well, it beats C#... – LegionMammal978 Dec 28 '15 at 15:52
  • Why 6 and not 9? – immibis Dec 30 '15 at 2:10
  • @immibis The goal of the challenge is to minimize the score, so it's not just minimizing bytes; putting 9 before would have increased my score unnecessarily. – Arcturus Dec 30 '15 at 2:59

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.

  • 18
    The esolang answers, while valid, are kinda boring. Glad to see one that isn't! – Arandur Dec 28 '15 at 14:50
  • 1
    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...) – derobert Jan 3 '16 at 7:23
  • @derobert True. 1e1 would satisfy the problem statement and give a score of 10 (the lower, the better) (Assuming a char is 1 byte) – dberm22 Jan 5 '16 at 13:18
  • @dberm22 10 (2 characters) is not longer than 1e1 (3 characters) – SuperJedi224 Jan 5 '16 at 14:05
  • @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. – dberm22 Jan 5 '16 at 15:36

TI-84 BASIC, 120

5!

ᴇ2 would score better if not for the silly UTF-8 requirement. (It's only two bytes in the calculator's native tokenized encoding, but it's 4 in UTF-8...)

  • Maybe I’m missing something, but the string "E2" is only two bytes in UTF-8… – jbg Dec 30 '15 at 9:10
  • 11
    @JasperBryant-Greene and E are not the same character. In TI-BASIC, is scientific notation and E is a variable. – SuperJedi224 Dec 30 '15 at 13:14
  • 4
    Thanks :) I should have noticed that the character was subtly-smaller-than-full-height… – jbg Dec 30 '15 at 13:27
  • Wouldn't 3! score better? – dberm22 Jan 5 '16 at 13:23
  • @dberm22 3! has only 1 digit, we need at least 3. – SuperJedi224 Jan 5 '16 at 14:04

C#, score 10^72 10^70 10^64 10^63

class A{static void Main(){System.Console.Write($"1{0:D63}");}}

That's 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000. I guess that I tried...

  • 1
    Stunning, really. – Arandur Dec 28 '15 at 15:46
  • 4
    Wouldn't Write(new string('1',70)) be better? – Lynn Dec 28 '15 at 18:02
  • 1
    You can write it out as a double rather than a string, which shortens it to 10^56 : class A{static void Main(){System.Console.Write(1e56);}} – goric Dec 30 '15 at 17:00
  • @goric That just outputs 1E+56. – LegionMammal978 Dec 30 '15 at 17:50

Hexagony, score 100100

Code:

d!!@

In a more readable form:

  d !
 ! @ .
  . .

The char value of d is 100. This will simply print the char value twice and terminates after.

Try it online!

  • Cute! Thank you for your contribution. – Arandur Dec 28 '15 at 15:12
  • 17
    More readable form? Really? xD – RK. Jan 3 '16 at 2:34

JavaScript, score 100,000,000,000 (or 1*1011)

alert(1e11)

This is if using alert. Though you can get 100 000 000 times lesser score if using console:

1e3

Score 1000 as you can see, I'm not sure it counts using the console though.

  • I don't know why it wouldn't count! – Arandur Dec 28 '15 at 14:49
  • 6
    @Arandur I'm guessing "I'm not sure it counts" refers to the lower score of 1000, since that requires using the console as a REPL, rather than being a full program. – trichoplax Dec 28 '15 at 14:52
  • @tri exactly, made a clarification. – nicael Dec 28 '15 at 14:53
  • 11
    +1 for alert, I would not allow the automatic console output – edc65 Dec 28 '15 at 14:58

PlatyPar, 59

#

# starts a numeric base-60 literal, and since no digits are found, it ends up as 59. This started as a happy accident, but since I have already [ab]used this bug in another answer, I kept it.

Try it online!

Here's another approach, my take on the boring way that everyone and their grandmother used for this challenge.

PlatyPar, 100000000 (9 digits)

'18Md0+;

Explanation

'1        ## push "1" (a string) to the stack
  8Md  ;  ## repeat 8 times
     0+    ## add a 0
          ## [implicitly] print the result

Try it online!

Brainf**k, 11111111111111111111111111111111111 (~1e34)

And another reduction:

+++++++[>+++++++>+<<-]>>[<.....>-]

Which gives 35 consecutive 1's, or approximately 1e34.


A bit smaller still

++++++++[>++++++>+<<-]>+>+[<....>-]

Gives 36 1's which is a number about 11% larger than 1e35.


Thanks to @Martin Büttner for knocking off a couple of characters reducing the total output by a factor of 100 with this code (yields 1e36):

++++++[>++++++++>++<<-]>+.->[<...>-]

My old code (yields 1+e38):

++++++++[>++++++>++<<-]>+.->+++[<..>-]

I've been experimenting with esolangs out of boredom. This is the best I could do in BF. I wonder if it is possible to make it smaller?

You can try it online here.

  • I wrote a shorter answer, from scratch: link – Lynn Dec 30 '15 at 12:27
  • @Mauris impressive! – Tom Carpenter Dec 30 '15 at 13:15

C, 1000000000000000000000000000 (28 digits)

main(){printf("1%027d",0);}

Similar to my C++ answer, without the #include <stdio.h> (Ignore the warning about missing declaration of printf. Thanks @Dennis)

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

Japt, score 10

A

As shows the score, prints 10.

  • This is not code golf;the goal is to print the smallest number, your score is 10. – ppperry Dec 28 '15 at 14:26
  • @ppp "The number must be longer in bytes than the program -- have more digits than the code has characters" - OP's just edited. – nicael Dec 28 '15 at 14:32
  • 1
    You have not misunderstood; if the Japt program A prints 10, then this is a valid program with a score of 10. – Arandur Dec 28 '15 at 14:34
  • 3
    Woohoo, my language is tied for <s>first</s> second place! glares at Retina – ETHproductions Dec 28 '15 at 16:21
  • 4
    Any reason for downvote? – nicael Dec 28 '15 at 19:14

Python 2, 101010101010

print'10'*6

PHP, score 10,000,000

<?=1e7;

This prints 10000000 as can be seen there.

  • 2
    Since the OP asked for the smallest integer, why did you pick an exponent of seven? Does it print without commas? – WGroleau Dec 29 '15 at 4:51
  • @WGroleau it does print without any commas, justachat.freevar.com/test.php – nicael Dec 29 '15 at 6:28

Labyrinth, score 10,000,000

1!!!!>@

It might be possible to bring this down by one order of magnitude, but I can't find anything right now.

The first 1!!!! prints 1000. Then > shifts the source code to

@1!!!!>

which avoids early termination. Then the IP hits a dead end and turns around. Now !!!! prints four more zeroes and @ terminates the program.

Try it online.

Brainfuck, 3333333333333333333333333 (25 threes)

This is written "from scratch" so I think it's okay to post a separate answer:

-[>+>+<<-----]>-[-->.<]

23 bytes long.

  • I was considering a bf solution, are there any current interpreters that only output as numbers? – Rohan Jhunjhunwala May 5 '17 at 23:21
  • @RohanJhunjhunwala this outputs 51 (the ASCII code for "3") 25 times. – Level River St May 6 '17 at 2:16
  • @LevelRiverSt what Im saying is that there might exist a bf interpreter that doesn't input as ascii and only does integer output, thus (-.) would output 255. – Rohan Jhunjhunwala May 6 '17 at 2:32

DC, 10000

4 chars program:

I4^f

5 digits output:

$ dc<<<'I4^f'
10000
  • 3
    +1. Yep, I think that's optimal in dc. Unusually bc is shorter. – Digital Trauma Dec 28 '15 at 19:00
  • I thought I was doing well with 6d^p (yielding 46656), but you have that beat - well done! – Toby Speight Jan 6 '16 at 17:19

Vitsy, 7! = 5040

7FN

Outputs 5040.

Try it online!

  • Why not 3FN outputting 6? – Peter Taylor Mar 25 '16 at 7:42
  • @PeterTaylor The number has to be greater in length, not greater in magnitude. – Addison Crump Mar 26 '16 at 13:29

C, 11111111111111111111111111111111111 (35 ones)

main(c){while(c++<36)putchar(49);}

Maybe there's a shorter way. The lack of a simple way to print big numbers in C makes it tricky.

Samau, 42

A

A pushes the Answer to the Ultimate Question of Life, The Universe, and Everything onto the stack. Then the top of the stack is automatically printed.

  • I think something is wrong with your commit messages :P – Doorknob Jan 3 '16 at 7:23
  • 3
    @Doorknob冰 I don't know how to write commit messages, so I just use random emojis. – alephalpha Jan 3 '16 at 9:17
  • 5
    Ok, here's the real question: what does Q push? – Cyoce Mar 8 '16 at 15:20

CJam, score 10

A

Try it online!

  • Why not Y, score 2? – Peter Taylor Mar 25 '16 at 7:43
  • 1
    Because 2 is only one character, so it isn't longer than the source code. – Dennis Mar 25 '16 at 12:48

Java, 111111111111111111111111111111111111111111111111111111111111111111111111111111111 (81 ones)

interface A{static void main(String[]a){for(A i:new A[81])System.out.print(1);}}
                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I've underlined the part that is actually "variable" here; everything else is absolutely necessary for a working Java program.

Presumably this is shorter than mucking around with Java's BigInteger.

  • Presumably ... - perhaps System.out.print(BigInteger.TEN.pow(10)); - is that one character less? Sorry - nowhere near big enough - and .pow(10).pow(10) is longer. – OldCurmudgeon Dec 29 '15 at 13:31
  • You can replace interface with class. – LegionMammal978 Dec 30 '15 at 11:46
  • 2
    But then main would need to be public. – Lynn Dec 30 '15 at 12:01
  • 7
    you can shave off 2 characters by doing this for the for loop: for(A a:new A[81]) – Jack Ammo Jan 1 '16 at 21:48
  • @OldCurmudgeon Would .pow(100) not work for some reason? – ETHproductions Mar 16 '16 at 15:50

Cubix, 100100

@'dOu

Cubix is a 2-dimensional, stack-based esolang. Cubix is different from other 2D langs in that the source code is wrapped around the outside of a cube.

Test it online!

Explanation

The first thing the interpreter does is figure out the smallest cube that the code will fit onto. In this case, the edge-length is 1. Then the code is padded with no-ops . until all six sides are filled. Whitespace is removed before processing, so this code is identical to the above:

  @
' d O u
  .

Now the code is run. The IP (instruction pointer) starts out on the far left face, pointing east.

The first char the IP encounters is ', which pushes the next byte onto the stack; this byte is d, or 100. Next is O, which outputs the top item (100) as an integer.

Then the IP hits u, which turns it to the right, moves it forward, then turns it again. It switches to the bottom face pointing north, then rotates to the east. This wraps it to the O again, outputting 100, then up to @ which terminates the program.

MATL, 1000

1e3

Note: latest GitHub commit of the compiler works on Octave as well as on Matlab.

This interprets the number in scientific notation, and implicitly prints it, thus producing the output

1000

Perl, 1000000000

print 1e9

Straightforward.

  • 1
    Can't you reduce your score by using say? – Neil Dec 29 '15 at 23:10
  • That would require enabling the -E flag, I suppose. I'm not sure how to count that, in this challenge. – Lynn Dec 30 '15 at 1:16
  • 1
    Since "-M5.010, when needed, is free," you shouldn't have to count it at all. – ThisSuitIsBlackNot Dec 30 '15 at 20:47

Python 2, 107918163081

print 69**6
  • You can remove the space between print and the 2. – Bakuriu Dec 29 '15 at 19:40
  • 1
    @Bakuriu No you can't – Blue Dec 29 '15 at 20:24
  • 69**6 is only 107918163081. – Neil Dec 29 '15 at 23:15
  • 2
    Interstingly enough, this also works in Perl. – Grimy Jan 4 '16 at 16:17

C++, 1e46

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

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

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

O, 10

A

Apparently the score is the number we print!

05AB1E, score 10

Code

T

Explanation:

T         # Puts 10 onto the stack
          # Implicit, print the last item of the stack

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.