5
\$\begingroup\$

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.

\$\endgroup\$
4
  • 6
    \$\begingroup\$ x/x for 1 doesn't work with 0/0 \$\endgroup\$
    – ajax333221
    Commented Mar 27, 2012 at 17:26
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 29, 2012 at 0:59
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 31, 2012 at 0:05
  • \$\begingroup\$ +1, because when I first read the question I wanted to -1 and flag, but then I read the answers it provoked. \$\endgroup\$
    – vsz
    Commented May 12, 2012 at 21:56

29 Answers 29

21
\$\begingroup\$

Python

This question is boring. I'd rather sleep.

from time import sleep
sleep(input())

Input a number.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ So the output is... consistently... nothing? \$\endgroup\$
    – Mr. Llama
    Commented Mar 29, 2012 at 14:49
  • 9
    \$\begingroup\$ @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. \$\endgroup\$
    – boothby
    Commented Mar 29, 2012 at 17:28
13
\$\begingroup\$

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.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Shouldn't it not halt for counterexamples. So really, it will always output 1. \$\endgroup\$
    – walpen
    Commented Jun 3, 2012 at 21:30
11
\$\begingroup\$

Perl

<>/0

produces for any input string:

Illegal division by zero at test5.pl line 1, <> line 1.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ it's not code golf \$\endgroup\$ Commented Mar 29, 2012 at 1:00
11
\$\begingroup\$

Ruby

def a(x)
  4
end

I somehow felt this was related:

enter image description here

\$\endgroup\$
10
\$\begingroup\$

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.

\$\endgroup\$
7
\$\begingroup\$

J

=

Usage:

   =10
1

Always outputs 1.

Or how about an unhappy smiley verb:

{:0[

Usage:

   {:0[ 147
0

Always ouputs 0.

\$\endgroup\$
2
  • \$\begingroup\$ That's not what = does. You're thinking of =~. Practically (but not semantically) they're the same thing when your input is one integer. \$\endgroup\$
    – Eelvex
    Commented Oct 25, 2012 at 14:24
  • \$\begingroup\$ @Eelvex Yes, I was thinking of =~. I've removed the incorrect bit from my answer. \$\endgroup\$
    – Gareth
    Commented Oct 25, 2012 at 14:34
5
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ um... isn't it UB to point to the argument space? For one, it's not possible with regcall. \$\endgroup\$ Commented Nov 30, 2013 at 14:47
  • \$\begingroup\$ Maybe. Then why would you use regcall? \$\endgroup\$
    – daniero
    Commented Nov 30, 2013 at 23:14
  • \$\begingroup\$ 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. \$\endgroup\$
    – daniero
    Commented Nov 30, 2013 at 23:16
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Python 2

x=input()
print x^x

Only accepts integers. always outputs 0 by xoring itself

\$\endgroup\$
3
\$\begingroup\$

Ruby:

p !gets

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

\$\endgroup\$
4
  • \$\begingroup\$ What about empty strings? \$\endgroup\$
    – Timtech
    Commented Nov 30, 2013 at 14:53
  • \$\begingroup\$ @Timtech p !"" #=> false. \$\endgroup\$ Commented Jan 11, 2014 at 1:33
  • \$\begingroup\$ Hmm, wasn't aware of that. \$\endgroup\$
    – Timtech
    Commented Jan 11, 2014 at 13:09
  • \$\begingroup\$ What else did you expect? Everything in Ruby that's not false or nil is truthy, the empty string's no exception. \$\endgroup\$ Commented Jan 11, 2014 at 23:43
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Golfscript

.=

Accepts any input and always prints 1.

\$\endgroup\$
2
  • \$\begingroup\$ It doesn't really use the input though. \$\endgroup\$
    – MrZander
    Commented Mar 27, 2012 at 22:12
  • 5
    \$\begingroup\$ @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. \$\endgroup\$
    – hammar
    Commented Mar 27, 2012 at 22:18
2
\$\begingroup\$

Python

def f(x):    
    return 0*x
\$\endgroup\$
3
  • 6
    \$\begingroup\$ f(1e+300/1e-300) returns nan. \$\endgroup\$ Commented Mar 29, 2012 at 20:26
  • 1
    \$\begingroup\$ You just blew my mind. \$\endgroup\$
    – Anti Earth
    Commented Mar 30, 2012 at 0:15
  • \$\begingroup\$ 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! \$\endgroup\$ Commented Mar 30, 2012 at 8:27
0
\$\begingroup\$

JavaScript

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

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

Will always return false, even with the input 1

\$\endgroup\$
0
\$\begingroup\$

Golfscript (4 chars)

~1\?

Accepts an integer. Outputs 1.

\$\endgroup\$
0
\$\begingroup\$

Perl

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

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

\$\endgroup\$
0
\$\begingroup\$

Python

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

Always returns 'infinity'.

\$\endgroup\$
0
\$\begingroup\$

Q

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

{1}
\$\endgroup\$
0
\$\begingroup\$

Python

print input()*0

\$\endgroup\$
0
0
\$\begingroup\$

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.

\$\endgroup\$
2
0
\$\begingroup\$

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).

\$\endgroup\$
0
\$\begingroup\$

Python

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

Accepts any input

\$\endgroup\$
0
\$\begingroup\$

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:

­
\$\endgroup\$
0
\$\begingroup\$

Common Lisp

(unless (read))

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

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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".

\$\endgroup\$
1
  • \$\begingroup\$ I forgot to add, the input must be a number. \$\endgroup\$
    – Timtech
    Commented Nov 30, 2013 at 15:03
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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;
}
\$\endgroup\$
0
\$\begingroup\$

Befunge

&@

always returns nothing.


And a less pointless version:

&>:v
 -
 ^1_.@

Always returns 0

\$\endgroup\$

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