5

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.

4
  • 6
    x/x for 1 doesn't work with 0/0
    – ajax333221
    Commented Mar 27, 2012 at 17:26
  • 5
    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. Commented Mar 29, 2012 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. Commented Mar 31, 2012 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
    Commented May 12, 2012 at 21:56

29 Answers 29

21

Python

This question is boring. I'd rather sleep.

from time import sleep
sleep(input())

Input a number.

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

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.

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

Perl

<>/0

produces for any input string:

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

Ruby

def a(x)
  4
end

I somehow felt this was related:

enter image description here

10

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.

7

J

=

Usage:

   =10
1

Always outputs 1.

Or how about an unhappy smiley verb:

{:0[

Usage:

   {:0[ 147
0

Always ouputs 0.

2
  • 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
    Commented Oct 25, 2012 at 14:24
  • @Eelvex Yes, I was thinking of =~. I've removed the incorrect bit from my answer.
    – Gareth
    Commented Oct 25, 2012 at 14:34
5

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.

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

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.

3

Python 2

x=input()
print x^x

Only accepts integers. always outputs 0 by xoring itself

3

Ruby:

p !gets

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

4
  • What about empty strings?
    – Timtech
    Commented Nov 30, 2013 at 14:53
  • @Timtech p !"" #=> false. Commented Jan 11, 2014 at 1:33
  • Hmm, wasn't aware of that.
    – Timtech
    Commented Jan 11, 2014 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. Commented Jan 11, 2014 at 23:43
3

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.

2

Golfscript

.=

Accepts any input and always prints 1.

2
  • It doesn't really use the input though.
    – MrZander
    Commented Mar 27, 2012 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
    Commented Mar 27, 2012 at 22:18
2

Python

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

JavaScript

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

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

Will always return false, even with the input 1

0

Golfscript (4 chars)

~1\?

Accepts an integer. Outputs 1.

0

Perl

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

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

0

Python

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

Always returns 'infinity'.

0

Q

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

{1}
0

Python

print input()*0

0
0

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.

2
0

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

0

Python

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

Accepts any input

0

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:

­
0

Common Lisp

(unless (read))

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

0

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.

0

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

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

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.

0

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;
}
0

Befunge

&@

always returns nothing.


And a less pointless version:

&>:v
 -
 ^1_.@

Always returns 0

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