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.

I was wondering how one might go and produce a program that takes input from some kind of random source and uses it to produce constant output. An obvious way is, for example, to take an integer x and output x/x for 1, but that isn't too clever, is it? Therefore, the task is to write a program that outputs the same thing, no matter which input is given to it.

Specifications

  • The program may assume that the input is either a number or a one line string (that is, valid characters are [a-z][A-z][0-9] and spaces). If you adopt one of those restrictions, specify which in your answer.
  • The program must use the entered input in some way. To put it more formally, the contents of the variable that takes the input must be used afterwards in some calculation that affects the final variable to be printed.

The winner will be the most upvoted solution.

share|improve this question
6  
x/x for 1 doesn't work with 0/0 –  ajax333221 Mar 27 '12 at 17:26
4  
The challenge is contradictory, if you say it has to affect the final variable to be printed, and meanwhile it has not. There is no way to get around that: If the input is the output, there is no influence. –  user unknown Mar 29 '12 at 0:59
    
After reading a comement to another post, I get another impression of the task: Fox input 7, output might be 7, and for input 8, output has to be 8 too, then? I thought in the beginning that we have to output the input. –  user unknown Mar 31 '12 at 0:05
    
+1, because when I first read the question I wanted to -1 and flag, but then I read the answers it provoked. –  vsz May 12 '12 at 21:56
add comment

29 Answers

Python

This question is boring. I'd rather sleep.

from time import sleep
sleep(input())

Input a number.

share|improve this answer
1  
So the output is... consistently... nothing? –  GigaWatt Mar 29 '12 at 14:49
8  
@GigaWatt: yes indeed! My guiding principal here was: ask a stupid question, get a stupid answer. Honestly, I thought the question would be closed before I got any upvotes. –  boothby Mar 29 '12 at 17:28
add comment

GolfScript

~{.3*){.2%}{2/}until.@=!}do

This program will take a positive integer as input, and will output 1 unless the input is a counterexample to the Collatz conjecture. While the Collatz conjecture remains an open problem, it is known that there are no counterexamples below 5 × 260.

share|improve this answer
3  
Shouldn't it not halt for counterexamples. So really, it will always output 1. –  walpen Jun 3 '12 at 21:30
add comment

Perl

<>/0

produces for any input string:

Illegal division by zero at test5.pl line 1, <> line 1.
share|improve this answer
1  
it's not code golf –  user unknown Mar 29 '12 at 1:00
add comment

Ruby

def a(x)
  4
end

I somehow felt this was related:

enter image description here

share|improve this answer
add comment

Python

n=input()
p=37
print (n**p-n)%p

Takes an integer input, and always outputs 0. Uses Fermat's little theorem, which states that n^p == n mod p.

share|improve this answer
add comment

J

=

Usage:

   =10
1

Always outputs 1.

Or how about an unhappy smiley verb:

{:0[

Usage:

   {:0[ 147
0

Always ouputs 0.

share|improve this answer
    
+1 for depressive ascii art code –  schnaader Mar 28 '12 at 23:32
    
That's not what = does. You're thinking of =~. Practically (but not semantically) they're the same thing when your input is one integer. –  Eelvex Oct 25 '12 at 14:24
    
@Eelvex Yes, I was thinking of =~. I've removed the incorrect bit from my answer. –  Gareth Oct 25 '12 at 14:34
add comment

C

This program reads an integer from stdin and then prints that integer, right?

main() {
    int i;
    printf("%d\n", scanf("%d", &i));
}

Well, no. It just prints 1.

The input must be an integer though (otherwise it may output 0 or -1), which proves that the program "uses" the input.

share|improve this answer
    
um... isn't it UB to point to the argument space? For one, it's not possible with regcall. –  Jan Dvorak Nov 30 '13 at 14:47
    
Maybe. Then why would you use regcall? –  daniero Nov 30 '13 at 23:14
    
Ok, on a (slighly) more serious note, I just used the argument because I'm lazy. Its use is besides the point really. I'll change it if it makes you happy. –  daniero Nov 30 '13 at 23:16
add comment

Javascript

var input = ~~prompt("Input an integer!");
if(Math.abs(input)<=Math.pow(2,31))
  console.log("It's too small! (not bigger than 2^31 = "+Math.pow(2,31)+")");
else console.log("It's quite big!");

Though JS can represent any integer between ±9,007,199,254,740,992, it will always print

It's too small! (not bigger than 2^31 = 2147483648)

because bitwise operation is performed in range of 32bit.

share|improve this answer
add comment

Python 2

x=input()
print x^x

Only accepts integers. always outputs 0 by xoring itself

share|improve this answer
add comment

Ruby:

p !gets

Always outputs false. gets takes user input, ! negates that and since strings are truthy, the result will be false.

share|improve this answer
    
What about empty strings? –  Timtech Nov 30 '13 at 14:53
    
@Timtech p !"" #=> false. –  Michael Kohl Jan 11 at 1:33
    
Hmm, wasn't aware of that. –  Timtech Jan 11 at 13:09
    
What else did you expect? Everything in Ruby that's not false or nil is truthy, the empty string's no exception. –  Michael Kohl Jan 11 at 23:43
add comment

APL

⍴⍣≡

is the shape function, ⍴⍣≡ is the fixpoint of the shape function.

All APL values have a shape. Say, a 4x6x8 array has shape 4 6 8; a scalar (like 3) has shape (no dimensions). Therefore, always returns a one-dimensional list. This means that applying the output of to will always get you a one-dimensional list with one number in it. That means that applying again will always get you a one-dimensional list with the number 1 in it.

Therefore, converges to 1; therefore ⍴⍣≡ will give 1 for all possible inputs.

share|improve this answer
add comment

Golfscript

.=

Accepts any input and always prints 1.

share|improve this answer
    
It doesn't really use the input though. –  MrZander Mar 27 '12 at 22:12
5  
@MrZander: Well, the criteria for what counts as using the input or not are quite vague. I'd say that if dividing by itself is OK, comparing to itself should also be fine. –  hammar Mar 27 '12 at 22:18
add comment

Python

def f(x):    
    return 0*x
share|improve this answer
6  
f(1e+300/1e-300) returns nan. –  leftaroundabout Mar 29 '12 at 20:26
1  
You just blew my mind. –  Anti Earth Mar 30 '12 at 0:15
    
Thinking about it though, I guess that's ok: "valid characters are [a-z][A-z][0-9] and spaces", apparently floating point is out. So +1 for a simplistic yet working solution! –  leftaroundabout Mar 30 '12 at 8:27
add comment

JavaScript

var input = prompt("Input something!");

console.log(new Number(input) === 1);

Will always return false, even with the input 1

share|improve this answer
add comment

Golfscript (4 chars)

~1\?

Accepts an integer. Outputs 1.

share|improve this answer
add comment

Perl

$a=<>+1;for(1..length$a){$a=length$a}print$a

This program does use the input number, and it always prints 1.

share|improve this answer
add comment

Python

print float("inf")*sum(map(ord,raw_input()))

Always returns 'infinity'.

share|improve this answer
add comment

Q

Returns 1. Where no parameters are declared, Q implicitly uses x,y& z where appropriate. I think this satisfies the second condition...

{1}
share|improve this answer
add comment

Python

print input()*0

share|improve this answer
add comment

Python

This one works for numbers only.
I can't prove that it works for all numbers (can anyone?), but it prints "False" for all cases I've tested.
EDIT: Fixed a bug in the last line.

p=lambda x:x>1 and (i*i>x for i in xrange(2,x+1) if x%i==0 or i*i>x).next()
q=lambda x:(i<x for i in xrange(2,x+1) if p(i) and p(x-i) or i==x).next()
x = input()
print x%2==0 and x>2 and not q(x)

Explanation (for those who think my Python code is unclear):
p(x) tells you if x is a prime.
q(x) looks for two primes, whose some is x.
The program prints True if the number is even, greater than 2 and not the sum of 2 primes.
Please tell me if you find such a number.

share|improve this answer
    
We probably won't find such a number: en.wikipedia.org/wiki/Goldbach's_conjecture –  Pieter Bos Aug 13 '13 at 21:18
    
I was about to say 4, but then I thought, 2 is prime :) –  Timtech Nov 30 '13 at 14:55
add comment

C

Produces 1 (prints and returns) for every input. (Input being the number of command line args)

j;main(c){j=0;while(c){j+=c%2;c/=2;}return j^1?main(j):printf("%d",j);}

This program does the same #error 1 but cannot properly speaking be said to take input (unless you count the rest of the program after as input).

share|improve this answer
add comment

Python

print eval('42#'+raw_input())

Accepts any input

share|improve this answer
add comment

PostScript

This function takes one argument of any type, actively discards, and will always output ­, unless you didn't supply it with enough arguments (which I am going to define as being undefined behavior for this function), depending on the value of the argument. Here is its implementation:

/f {pop} def

This function will always take one more argument than is currently on the stack, so it will always output a stackunderflow error, depending on the input.

/f {clear pop} def

And here is a third, even more useful function, which I will define as having undefined behavior, depending on the number, types, and values of the arguments. Here is its implementation:

­
share|improve this answer
add comment

Common Lisp

(unless (read))

Always returns nil (unless you crash it with stupid input)

share|improve this answer
add comment

Clojure

(and(read-line)0)

Crudely equivalent to the prior common lisp submission, except that it makes no attempt to ensure that the line "read" is valid in any way shape or form. Compiled or interpreted, this program will read a single line and exit 0 in all cases.

share|improve this answer
add comment

Game Maker Language

if argument0 return argument0/argument0 else return argument0

It can be any script, just call script(argument) - it returns true if the number is not 0, else it returns false

If this was code golf, it would be if argument0 a=1 return a which is 25 characters when compiled with the option "Treat all uninitialized variables as value 0".

share|improve this answer
    
I forgot to add, the input must be a number. –  Timtech Nov 30 '13 at 15:03
add comment

Brainf**k

Since the question is self-contradictory it's hard to give a serious answer, so here it is:

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

Accepts any string or number as input, always outputs "!!!". Have fun figuring out what it does.

share|improve this answer
add comment

C++

Accepts any input. Prints ASCII equivalent of each character entered:

#include <iostream>
#include <string>
using namespace std;

int main() {
  string s;
  cout << "Enter a string: ";
  cin >> s;

  cout << "ASCII characters of input:\n";

  for (int i = 0; i < s.size(); i++)
    cout << s[i] << ": " << int(s[i]) << endl;

  return 0;
}
share|improve this answer
add comment

Befunge

&@

always returns nothing.


And a less pointless version:

&>:v
 -
 ^1_.@

Always returns 0

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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