# Make a reliable calculator capable of calculation 1+1 [closed]

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.

-
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

## closed as too broad by Doorknob, copy, hosch250, TheDoctor, The Guy with The HatMar 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.

# Befunge 98

``````&&:1-!2*j1q\:1-!2*j1q+1+.@
``````

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

# bash

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

Trolls:

• 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?
-
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

# Bash

``````echo "What is the first number?"
echo "What is the second number?"
ret=\$(( \$(response || echo 1) + \$(response || echo 1) )) && 1
``````

Trolls:

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.

-
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

# 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);
``````

Trolls:

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

-

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);
``````
-
+1 for: give the result. Nice calculator. –  Mhmd Feb 23 at 10:38

# Smalltalk

``````'
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 :-)=>

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

-
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

# Java

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');
}
java.util.Collections.sort(digitList);
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`.

Trolls:

• 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 `ArrayIndexOutOfBoundsException`s 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
-
+1 for the `numbers[(val1 * val2 >>> 32) - 1]`. –  Victor Feb 23 at 5:53

# C++

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

int main() {
cout<<'1'+'1';
}
``````

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:

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

int main() {
cout<<'1+1';
}
``````

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.

-
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

# Python

``````import hashlib
h = hashlib.new("sha256")
h.update("")
dig = h.hexdigest()
onelist = []
for i in dig:
if i == "1":
onelist.append(i)
if len(onelist) == 2:
break
print int(onelist[0])+int(onelist[1])
``````
-