Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 zeroes are disallowed under all circumstances. Counting leading zeroes trivializes the problem; ignoring leading zeroes unnecessarily complicates the question.

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

share|improve this question
43  
Number 1 on the Hot Network Questions. Not bad for a first question... – trichoplax Dec 28 '15 at 15:15
5  
@Kslkgh Strictly less than, otherwise the question is trivial for programs which implicitly print their last value. – Arandur Dec 28 '15 at 15:57
5  
Is 1.0 an integer? – histocrat Dec 28 '15 at 20:05
5  
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

98 Answers 98

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.

share|improve this answer
23  
... WE HAVE A WINNER. – Arandur Dec 28 '15 at 15:11
4  
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
36  
@nicael Luckily, that's not how regex works. ;) – Martin Ender Dec 28 '15 at 15:19
5  
@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
14  
@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.

share|improve this answer
65  
I like how your first attempt at using Pyth is just the letter T. – djechlin Dec 28 '15 at 15:34
5  
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

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.

share|improve this answer
    
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
34  
@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.

share|improve this answer
1  
Brought a tear to my eye. Thank you for your contribution. – Arandur Dec 28 '15 at 14:53
17  
It will probably still beat Java... Hopefully. – A Nerd - I 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. – A Nerd - I Dec 30 '15 at 2:59

MATLAB, 1000000000

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.

share|improve this answer
15  
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 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 at 13:18
    
@dberm22 10 (2 characters) is not longer than 1e1 (3 characters) – SuperJedi224 Jan 5 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 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...)

share|improve this answer
    
Maybe I’m missing something, but the string "E2" is only two bytes in UTF-8… – jbg Dec 30 '15 at 9:10
8  
@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 at 13:23
    
@dberm22 3! has only 1 digit, we need at least 3. – SuperJedi224 Jan 5 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...

share|improve this answer
    
Stunning, really. – Arandur Dec 28 '15 at 15:46
2  
Wouldn't Write(new string('1',70)) be better? – Lynn Dec 28 '15 at 18:02
    
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

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.

share|improve this answer
    
I don't know why it wouldn't count! – Arandur Dec 28 '15 at 14:49
4  
@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
9  
+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!

share|improve this answer

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!

share|improve this answer
    
Cute! Thank you for your contribution. – Arandur Dec 28 '15 at 15:12
9  
More readable form? Really? xD – RK. Jan 3 at 2:34

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.

share|improve this answer
    
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

Japt, score 10

A

As shows the score, prints 10.

share|improve this answer
    
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
2  
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

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

share|improve this answer

Vitsy, 7! = 5040

7FN

Outputs 5040.

Try it online!

share|improve this answer
    
Why not 3FN outputting 6? – Peter Taylor Mar 25 at 7:42
    
@PeterTaylor The number has to be greater in length, not greater in magnitude. – VTCAKAVSMoACE Mar 26 at 13:29

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.

share|improve this answer

CJam, score 10

A

Try it online!

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

dc, 10000

4 chars program:

I4^f

5 digits output:

$ dc<<<'I4^f'
10000
share|improve this answer
1  
+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 at 17:19

Python 2, 101010101010

print'10'*6
share|improve this answer

PHP, score 10,000,000

<?=1e7;

This prints 10000000 as can be seen there.

share|improve this answer
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

Python 2, 107918163081

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

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.

share|improve this answer
    
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
1  
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 at 21:48
1  
That's genius! Thank you. – Lynn Jan 1 at 21:49

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.

share|improve this answer

Perl, 1000000000

print 1e9

Straightforward.

share|improve this answer
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

QBasic, 15625

?5^6

? auto-magically gets converted to PRINT, 5 to the power of 6 yields the most convenient number of more than 4 bytes.

share|improve this answer
    
QBasic automagically converts your code to PRINT 5 ^ 6 (which is 11 bytes) before it is run. I'm not counting the 2 bytes for the newline because that would be mean but strictly speaking even the empty QBasic program has two bytes... – CJ Dennis Dec 29 '15 at 9:44
1  
@CJDennis If I save this in a .BAS file and ask QBasic.exe to /run that, it runs. – steenbergh Dec 29 '15 at 12:01

TeaScript, 10 points

e

The e variable is preinitialized to 10

Try it online

share|improve this answer
3  
Isn't that confusing, given that e is another number? – Cyoce Dec 30 '15 at 8:37
    
@Cyoce true but variable Me is e so both are usable – Downgoat Dec 31 '15 at 17:42

O, 10

A

Apparently the score is the number we print!

share|improve this answer

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

share|improve this answer

Brian & Chuck, 11,111,111,111,111 (≈ 1.1e13)

?1<SO>{?
#{>.>-?

Here, <SO> stands for the "shift out" control character with character code 0x0E.

Try it online.

That's all my language covered then. :)

Explanation

The 1 in Brian's code (first line) is used for printing. Then <SO> is used as a counter variable. The rest is just a simple loop setup which prints that 1 while decrementing the <SO> down to zero.

share|improve this answer

Mathematica, score 87,178,291,200

Print[14!]

This solution is not helped by the fact that it takes 6 characters to print anything.

share|improve this answer
    
Why do you want to use Print though? Raw operations are valid programs in Mathematica (answer) – March Ho Dec 28 '15 at 22:22
3  
@MarchHo the fact that you got away with it in the past does not mean it's generally legitimate to assume a REPL environment. ;) – Martin Ender Dec 28 '15 at 23:01
    
@MartinBüttner I don't get your point. It manifestly does not "take 6 characters to print anything" for this question in Mathematica. I don't see where REPL environments come into play here. – March Ho Dec 28 '15 at 23:10
2  
@MarchHo if you run 14! from a script file, it will not print anything. It only prints something when typed into a notebook, which is a REPL environment. – Martin Ender Dec 28 '15 at 23:11
1  
@MartinBüttner I was about to post in Meta, but someone beat me to it – March Ho Dec 29 '15 at 0:00

C++ 10000000000000000000000000000000000000000000000 (47 digits)

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

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

share|improve this answer

protected by VTCAKAVSMoACE 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.