Tell me more ×
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
4  
x/x for 1 doesn't work with 0/0 – ajax333221 Mar 27 '12 at 17:26
3  
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

20 Answers

Python

This question is boring. I'd rather sleep.

from time import sleep
sleep(input())

Input a number.

share|improve this answer
So the output is... consistently... nothing? – GigaWatt Mar 29 '12 at 14:49
4  
@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

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
1  
Shouldn't it not halt for counterexamples. So really, it will always output 1. – walpen Jun 3 '12 at 21:30

Perl

<>/0

produces for any input string:

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

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

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

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

Python 2

x=input()
print x^x

Only accepts integers. always outputs 0 by xoring itself

share|improve this answer

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
3  
@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

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

Ruby

def a(x)
  4
end

I somehow felt this was related:

enter image description here

share|improve this answer

Python

def f(x):    
    return 0*x
share|improve this answer
2  
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

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

Golfscript (4 chars)

~1\?

Accepts an integer. Outputs 1.

share|improve this answer

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

Python

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

Always returns 'infinity'.

share|improve this answer

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

Python

print input()*0

share|improve this answer

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

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

Python

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

Accepts any input

share|improve this answer

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.