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.

So, i forgot what 1+1 was.

I checked on the calculator and it said 3, and I got it wrong.

Can you please make a reliable calculator than can calculate only 1+1?

Remember, this is code-trolling.

The one with more upvotes win.

share|improve this question
People do not particularly appreciate code-trolling anymore. Please try to think of different puzzles instead. –  hosch250 Feb 23 at 1:55
By the way, either you need a new calculator or a new battery for your old calculator. –  hosch250 Feb 23 at 2:20
Can it be a new comment? –  Ismael Miguel Feb 23 at 2:44
@user2509848 Some people do not particularly appreciate code-trolling anymore. Even you posted an answer anyway (and your answer made me laugh). –  Victor Feb 23 at 2:58
Well, @Victor, glad to make you laugh. I posted a spoiler on it, just in case people thought it worked like a scripting language (it works very different). –  hosch250 Feb 23 at 5:44
add comment

closed as too broad by Doorknob, copy, hosch250, TheDoctor, The Guy with The Hat Mar 3 at 20:06

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.

9 Answers

Befunge 98


This will do exactly what you requested. Just input the two numbers separated by a + and it will add them, producing 3 as expected.

  • Main troll: it only adds 1+1, if any other input is given, it will end with error code 1. I quote (emphasis added):

    Can you please make a reliable calculator than can calculate only 1+1?

  • it also adds to 3 by adding 1 to the output afterwards

  • although the OP's writer is told to separate the numbers with a +, any non-digit character will work.
share|improve this answer
All it matters is that it can say calculate 1+1. –  Ismael Miguel Feb 23 at 2:42
Wonderful answer. This is perfectly understandable, comprehensible and obvious for a beginner that can't even calculate 1+1. –  Victor Feb 23 at 2:42
^ this is not funny –  Ismael Miguel Feb 23 at 5:43
add comment


read -p 'Enter first number: ' first
read -p 'Enter second number: ' second
(( ! $((first-1)) && ! $((second-1)) )) && echo $(((first+second)) || echo "$(( RANDOM%first + RANDOM%second ))"


  • It reliably computer the addition for inputs 1 and 1.
  • It reliably produces random results for any other inputs.
  • The calculator produces 0 when input values are large. Can you figure how large?
share|improve this answer
The only thing that matters is that it can reliably calculate 1+1 –  Ismael Miguel Feb 23 at 6:00
@IsmaelMiguel Did I misread the question or does it work as expected? –  devnull Feb 23 at 6:03
You misread it. –  Ismael Miguel Feb 23 at 6:20
@IsmaelMiguel That's what I was trying to figure. I guess that it needs to reliably calculate 1+1. Check the update. –  devnull Feb 23 at 6:22
add comment


echo "What is the first number?"
read response
echo "What is the second number?"
read response
ret=$(( $(response || echo 1) + $(response || echo 1) )) && 1
echo "The answer is: $ret."


  1. The first response is irrelevant. This is because of a shared variable.
  2. If the second response is greater than 0, it returns 1.
    • Yeah, that usage of echo 1 to defend against textual (in contrast to numerical) works a little too well (and is probably too simple to be true...).
  3. It is only obvious that 0 + 0 won't work. Any other answer outside of 1 + 1 will not work, but it isn't completely obvious why beyond the first troll.

I feel that this is sufficiently underhanded.

share|improve this answer
Will it give a reliable result? –  Ismael Miguel Feb 23 at 5:21
Only for 1+1 because of the &&/|| operators. And it prints 2 consistently for 1+1 (or at least it should...I don't have a computer to test it on right now). –  impinball Feb 23 at 5:27
It is reliably inaccurate for anything other than 1 + 1. It does actually print 2 for 1+1. Should that clarify? –  impinball Feb 23 at 5:29
As long as it is reliable to calculate 1+1, it's perfect –  Ismael Miguel Feb 23 at 5:44
add comment

JavaScript...slightly more evil than my first

var first = Number(prompt("First number:"));
var second = Number(prompt("Second number"));
var ret = first << 1/(1/second);
alert("The answer of " + first + " + " + second + " is: " + ret);


  1. The double flipping of fractions throw an error any time a zero is encountered.

  2. The right bit shift completely screws up any number besides 1 in 1+1 and 0 with anything (which is impossible with the above code).

share|improve this answer
add comment

Here it is, in javascript.

a = prompt("Give the first number");
op = prompt("Give the operation");
b = prompt("Give the second number");
res = prompt("Give the result");
res = 1 + 1;
alert("The result is " + res);
share|improve this answer
+1 for: give the result. Nice calculator. –  Mhmd Feb 23 at 10:38
add comment


I'll just nod and pretend I am listening,
but the answer will always be: 2
' print.

I have prettyprinted the code to make it easier to understand :-)=>

PS: I though, 42 was the answer to all questions!

share|improve this answer
Isn't 42 the meaning of life? –  Ismael Miguel Feb 23 at 2:39
life, universe and everything. –  blabla999 Feb 23 at 2:40
No. It is the answer to the ultimate question of life, the universe, and everything. –  Quincunx Feb 24 at 19:55
nitpickers! Anyway, the universe does include 1+1 ;-) LOL –  blabla999 Feb 24 at 22:58
add comment


That calculation is correct. Use this calculator to check your work:

public class Calculator {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        System.out.println(calculator.calculate("1 + 1"));
    private static final String PI = "3.14159";
    int[] numbers;

    public Calculator() {
        java.util.ArrayList<Integer> digitList = new java.util.ArrayList<>();
        for (char c : PI.toCharArray()) {
            digitList.add(c - '0' < 0 ? '.' : c - '0');
        numbers = new int[digitList.size()];

        for (int i = 0; i < digitList.size(); i++) {
            numbers[i] = digitList.get(i);

    public int calculate(String expression) {
        String[] split = expression.split("[^\\d+/*-]+");
        int val1 = Integer.parseInt(split[0]);
        int val2 = Integer.parseInt(split[2]);

        switch (split[1]){
            case "+":
                return numbers[val1 + val2];
            case "-":
                return numbers[val1 - val2];
            case "*":
                return numbers[val1 * val2];
            case "/":
                return numbers[val1 / val2];
        return numbers[(val1 * val2 >>> 32) - 1];

As you can see, 1 + 1 results in 3.


  • uses a list of "numbers" to do the computations
  • uses the first few digits of pi to choose the numbers.
  • uses a ternary conditional which beginning programmers don't understand.
  • uses an ArrayList and Collections.sort, to the more confusion of the beginning programmer
  • uses a String to define the expression
  • uses String#split complete with a regex (which most beginners don't know).
  • adds, subtracts, multiplies, and divides by accessing the appropriate index of the array of "numbers" (e.g. 1 + 1 results in the index 1 + 1; 2 * 2 results in the index 4)
  • guaranteed to get ArrayIndexOutOfBoundsExceptions when the input changes much
  • if the switch statements are all not executed, throws an ArrayIndexOutOfBoundsException by accessing the array index of -1, but by using the "confusing" method of zeroing the inputs (val1 * val2 is a 32 bit number, so >>> 32 forces it to 0) and subtracting 1
share|improve this answer
+1 for the numbers[(val1 * val2 >>> 32) - 1]. –  Victor Feb 23 at 5:53
add comment


This should help you:

using namespace std;

int main() {

Many languages used here would make an observer who didn't know C++ think it printed the number 22. No so, it prints 98:

In C++, characters can be added to each other. This code, cout<<'1'+'1'; actually adds the ASCII values of '1' and '1' (49) to make 98. Test it out on ideone.com: http://ideone.com/VQAbIe.

This is another possible version:

using namespace std;

int main() {

Given the syntax of most languages used here, it may appear that this prints the string "1+1". Not quite. It actually prints 3222321:

In C++, '' quotation marks always specifies a character, not a string. Strings are always specified with "" quotation marks. The code cout<<'1+1'; tells it to output the single character '1+1', which does not exist. This trick can be used with up to 4 characters in character quotation marks. I do not know why it outputs that specific number, but it has something to do with character codes. Test it out on ideone.com: http://ideone.com/RgbpDJ.

share|improve this answer
Are you sure this will give me the right result? –  Ismael Miguel Feb 23 at 2:39
I am not sure of anything anymore. I thought 1+1 was 2, but if the calculator says 3... –  hosch250 Feb 23 at 3:05
But I got it wrong in the test. –  Ismael Miguel Feb 23 at 3:06
@IsmaelMiguel Oh. Well, you will likely get it wrong on the next test too, unless it is a computerized test that calculates it the same way I do. –  hosch250 Feb 23 at 3:08
Stupid calculators... They aren't as good as they used to be in the 1890's. –  Ismael Miguel Feb 23 at 3:13
show 2 more comments


import hashlib
h = hashlib.new("sha256")
dig = h.hexdigest()
onelist = []
for i in dig:
    if i == "1":
    if len(onelist) == 2:
print int(onelist[0])+int(onelist[1])
share|improve this answer
add comment

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