Take the 2-minute tour ×
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.

The output of the program must have 1 as the first line. The rest of the output can be anything.

Bonus Points: (the main target.. everything else is participation prize)

  • -500 if code length = 0
    ie- output 1 using a 0-byte source code program. program itself should be a zero-byte entry.

Question's origin - http://codegolf.stackexchange.com/a/21264/13419

Winner will be decided on 1st April 2014 based on the number of Up Votes. (Code length = 0)

share|improve this question

closed as too broad by Jonathan Van Matre, hosch250, Quincunx, bwoebi, The Guy with The Hat Mar 8 at 13:38

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
In this case, you posted the wrong question. You should post something like "output 1 using a 0-byte source code program". And you should have tagged this as code-challenge or even popularity-contest, since the program itself should always be a zero-byte entry. But now, it is too late to fix that. –  Victor Feb 19 at 14:54
1  
Edit please. I downvoted because I though the bonus was joke, but I now want to upvote (the real challenge is to get the -500, as @rahulroy9202 said). –  Ismael Miguel Feb 19 at 15:10
1  
@IsmaelMiguel Done –  Timtech Feb 19 at 15:34
1  
@blabla999 It means that the source code of the program MUST be 0-bytes long. It doesn't matter, as long as the 1st line has '1' somewhere and the source-code file exists and is empty. –  Ismael Miguel Feb 19 at 22:58
2  
As previous comments mention, this is impossible to golf. What the question boils down to is "Find a language whose compiler/interpreter will output '1' with a null input." As such, it will be a polyglot of every answer that satisfies the criteria for the bonus. :-) –  Comintern Feb 20 at 4:59
show 8 more comments

14 Answers

ls (coreutils), -500

Create an empty file named 1 and save it in the current directory.

Usage: In the terminal, type ls 1

This usage is analogous to how you run a python script by typing in a terminal python 1.py or running a perl script by typing perl 1.pl, so I think this should qualify.


Edit: since someone in the comments doesn't seem to get what this answer means, here's an explanation:

First, an empty file is created and is saved in the current directory, with the name 1.

Next, in a terminal that is at the current directory, run ls 1. Since I suspect he doesn't know what ls is, here is a copy from the ls man page:

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about  the FILEs (the current directory by default).
       Sort entries alphabetically if none of -cftuvSUX nor --sort  is  speci‐
       fied.

Output: As long as you are in the directory where your "script" is located, it will always output 1.


Now, some may argue that this answer isn't really valid because it is not really a "script" and ls isn't exactly a programming or scripting language. I provide the counter-argument that this "script" is invoked in the same fashion as the popular scripting languages python and perl, that is, by calling the interpreter executable, followed by the file name. I argue that in this answer, I call the interpreter executable, namely ls, then provide the script name, 1. Since they are invoked in the same fashion, I argue that the should be treated the same, and this answer should be valid.

Hope this clears things up. I'm just trying to come up with a "smart" way to get the -500 bonus - I mean, if no one tries that, this question would become meaningless (since it is even more trivial than printing "Hello World").

share|improve this answer
    
But it MUST print 1. Those 2 examples (using perl and python) use an empty file and output nothing. The ls 1 answer MIGHT output 1 somewhere in the 1st line, but it's a 4 byte long solution. This is an invalid answer, sorry. –  Ismael Miguel Feb 19 at 18:15
    
@Ismael Miguel did you even read my answer? The mention of perl and python is an argument that this answer is valid, that is not part of the answer. The ls 1 part is an invocation of the "script", not the actual script, and is like how you invoke a python script or a perl script –  ace Feb 19 at 18:39
    
You should read the comment carefully. It says 'Those 2 examples[...]' and not 'Those 2 answers[...]'. And that 'invocation of the "script"' still won't print 1, so, it's still invalid. –  Ismael Miguel Feb 19 at 18:42
    
@Ismael Miguel are you kidding me? I said it is analogous to how you INVOKE A SCRIPT. The USAGE is similar, not the OUTPUT. Also, if you CREATED a file named 1, then in that directory run ls 1 it will ALWAYS output 1. Look at this screenshot: i.imgur.com/O2Itu56.png –  ace Feb 19 at 18:48
    
ok, fine, you won... –  Ismael Miguel Feb 19 at 18:53
show 2 more comments

C - 0 bytes (-500)

Source:



Note: this will not work in all compilers, and the targeted compiler will throw some warnings.

How it works: The C language specification doesn't give any guidance as to what a compiler should do with a zero length source file, and as undefined behaviour it is up to the implementor of the compiler how that case should be handled. I implemented the following compiler in bash, and the code above will produce an executable that outputs "1" and nothing else.

Here's the source code for the compiler so you can test my code (pretty exciting for me, I never wrote a compiler before). It does have a dependency, gcc has to be installed:

#!/bin/bash
_file="$1"

if [ -s "$_file" ]
then
    gcc "$_file"
else
    echo 'main(){return printf("1");}' | gcc -xc -
fi

As an added bonus, this is a polyglot! It also runs in ls (coreutils) and PHP/Bash.

share|improve this answer
2  
I was wondering if anyone would actually write a compiler :) –  Jwosty Feb 23 at 5:17
    
It's fairly standards compliant too. Unfortunately it doesn't support multiple input files. :-) –  Comintern Feb 23 at 5:19
    
Well, it wouldn't count against you to improve your compiler here. –  Jwosty Feb 23 at 5:21
add comment

PHP: 1 character

1

The whole source file is dumped into output.

share|improve this answer
3  
It is interesting that this is a quine too! –  Victor Feb 19 at 22:53
add comment

PHP/Bash (-500 bytes):

Hopefully valid:

php -r 'echo 1|@include("/dev/null");'

To work on windows:

php -r "echo 1|@include('nul');"

Notice that nul is the Windows' equivalent to Linux's /dev/null

Reference: http://en.wikipedia.org/wiki//dev/null

To automatically detect the OS and set the path, use this:

php -r "echo 1|@include(PHP_SHLIB_SUFFIX=='dll'?'nul':'/dev/null');"

Old answer:

Bash/cmd command:

php -r 'echo 1|@include("b.php")'

File b.php:

share|improve this answer
    
what is the output?? –  rahulroy9202 Feb 19 at 14:33
1  
@rahulroy9202 The output would be 1. The include construct returns 1 if the file was found and successfully included and doesn't contain any return statements in the global scope. –  Tony H. Feb 19 at 15:03
    
It should output 1, either the file exists or not (i edited the answer). The source file only exists to give the bonus of -500. Why the bonus: the source file has no code! And according to the question, a 0-byte source file is a -500 bonus. –  Ismael Miguel Feb 19 at 15:08
    
@rahulroy9202 I added a new 'file-less' path-independent solution. The output will always be 1 and possibly a ton of garbage. And still with a 0 byte 'file'. –  Ismael Miguel Feb 19 at 16:15
2  
+1 for @include("/dev/null") –  Victor Feb 19 at 22:46
show 1 more comment

Brainfuck$, 2 characters

+:

In Brainfuck$, the : command gives numeric output. So, this outputs 1

share|improve this answer
    
Technically 1 byte since there are only 14 commands, all of which can be represented with 4 bits. 2 4-bit chars = 1 byte. –  Timtech Feb 19 at 16:36
add comment

Braille, 1 bit

Image copied from Wikipedia (with the rest of the numbers for interest only. 1 bit Can mean 1 or A.

enter image description here

share|improve this answer
    
I think that you really need 6 bits, but 5 of them are zeros. Anyway it is still less than 1 byte. –  Victor Feb 19 at 15:06
1  
Hey, numbers with Braile have the prefix like .⋮ –  V-X Feb 19 at 15:55
    
@Victor you actually need 4 bits to encode the numbers 0-9 with no spares. 0000 is not used, and because there is no box around each symbol, 0001, 0010, 0100 and 1000 are indistinguishable. similarly only one of the two possibilities of 2 vertical dots and one of the two possibilities of 2 horizontal dots is used. The bottom cells are only used for letters. I justify my cheeky "1 bit" by saying that a braille reading robot would definitely read this as "1" or possibly "A" and that it is a Huffman type encoding where common symbols use less bits (though that would be more valid for Morse code.) –  steveverrill Feb 19 at 17:02
    
@ V-X You are correct, but Louis Braille's original system did not include this symbol (nor did it include the letter W, because there is no W in French.) Therefore my excuse is that I am using "Original Braille" ;-) It's a joke answer anyway. –  steveverrill Feb 19 at 17:05
add comment

Polyglot (9 bytes, plenty of languages):

print(1);
share|improve this answer
    
In some languages just print 1 might do it. –  Victor Feb 19 at 14:28
    
I know, but not all. I want to include as many languages as possible. –  Ismael Miguel Feb 19 at 14:29
    
at least one language wants "1 print" ;-) –  blabla999 Feb 19 at 16:44
    
Well, This answer targets 'plenty', and not 'all'. It would be great if it targeted all, but then it would be boring 'cause all languages would look a copy of each other (like all gecko browsers look almost equal). –  Ismael Miguel Feb 19 at 18:45
add comment

Bash: 5 characters

seq 1

Let's print a sequence of numbers...

share|improve this answer
add comment

Polyglot: Just 6 characters

Works for Command (cmd), bash, PHP and probably a few others.

echo 1
share|improve this answer
1  
And it works on PHP too. And cmd, bash..... –  Ismael Miguel Feb 19 at 15:41
add comment

Javascript: 14 / 1 characters

console.log(1)

If you are in the console (like firebug), this could be reduced to 1 character:

1
share|improve this answer
add comment

JSFuck - 2635 / 5 characters

This is my previous answer translated to JSFuck.

[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]](([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+(![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+[+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]])()

If you are in the console (like firebug):

+!+[]
share|improve this answer
    
JSFuck isn’t a language, and who cares –  minitech Feb 19 at 17:35
add comment

TI-BASIC    

1
share|improve this answer
add comment

Smalltalk (Smalltalk/X), 1 (0 discussable)

as there is a read-eval-print-loop deep down under the hood, and (like in lisp), numbers eval to themself, just evaluate "1".

enter image description here

Wow - it beats (the otherwise hard to beat) brainf*** ;-))

For the zero discussion: well you talked about "zero sized source program"; but what about languages like lisp, scheme, st, basic,... (+hundreds more) which have a repl (read-eval-print-loop) underneath?

We can simply feed it a "1" as input (aka "echo 1 | <any of the above>".

Of course, when you accept that argument, many many programs would score (-500), eg. "echo 1" alone will do or "echo 1 | cat" and so on. It depends on your definition (is "1" a program or an input, or an argument, and what makes it a difference?).

share|improve this answer
    
For it to qualify to the bonus, it MUST have a REAL 0-byte long source file. But we can say that you made a 0-byte long solution, but the source-code has to be 0-bytes long too. And there is no source file, so, it's not valid for the bonus. Find a way to include a file (like sh echo 1 | sh /dev/null for Linux shell) and you can have a -500 bytes solution. –  Ismael Miguel Feb 19 at 18:34
    
You mean something like: "touch z ; stx -f z -P 1" would be ok? (-f argument specifies an input file). That would be easy. –  blabla999 Feb 19 at 19:35
    
Your answer wouldn't be far from mine. But yes. I would just make the include inside the smalltalk language. (check my php answer for an idea) –  Ismael Miguel Feb 19 at 19:51
add comment

Deadfish

io

Increment and output.

share|improve this answer
add comment

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