-23
\$\begingroup\$

Challenge

  • Input two integers.
    • Integer I/O must be decimal.
  • If the integers are equal, output a truthy value.
  • Otherwise, output a falsy value.

Clarifications

  • You will never receive non-integer inputs.
  • You will never receive an input that is outside the bounds [-2³¹, 2³¹).

Rules

  • Standard loopholes are disallowed.
  • This is . Shortest answer wins, but will not be selected.
\$\endgroup\$
12
  • 26
    \$\begingroup\$ I can't tell you the reason for the other downvotes, but mine is because I firmly believe that trivial questions like this are bad for the site. -- Peter Taylor \$\endgroup\$
    – DJMcMayhem
    May 31, 2017 at 20:29
  • 7
    \$\begingroup\$ A reminder: as this is ridiculously trivial in most languages, please vote according to difficulty. Just because it's a 1 byte builtin in some golfing languages doesn't mean those answers are better than well-golfed longer solutions in, say, Brain-Flak. \$\endgroup\$
    – Riker
    May 31, 2017 at 20:33
  • 6
    \$\begingroup\$ @All downvoters: If you're downvoting this then you have to downvote the add two numbers challenge and the multiply two numbers challenge too because they're "too trivial". \$\endgroup\$ May 31, 2017 at 21:01
  • 6
    \$\begingroup\$ @CalculatorFeline But not all trivial challenges are the same level of trivial. In most languages without built-ins to add two numbers or check for equality (especially those without numerical input), I would expect the equality check to be easier to implement. Example: my BF equality check is 45 bytes, while the shortest BF addition program is 224 bytes. \$\endgroup\$ May 31, 2017 at 21:10
  • 2
    \$\begingroup\$ "Decimal" is not a correct way to describe a numeric type like C int. It only makes sense when talking about string representation of numbers, or with decimal floating point (en.wikipedia.org/wiki/Decimal_floating_point) where the bit pattern representing the number uses base 10, not base 2. Or maybe a BCD integer. IIRC, IBM's POWER architecture has decimal float support, which is useful for some financial stuff, but most architectures only have IEEE binary32 / binary64. But anyway, a numeric type is probably best described as a binary integer, or simply "an integer". \$\endgroup\$ Jan 10, 2018 at 2:22

57 Answers 57

18
\$\begingroup\$

Brachylog, 0 bytes

Try it online!

One number from STDIN and one from command-line arguments.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ The right tool for the job. \$\endgroup\$
    – Adám
    Jan 9, 2018 at 14:17
17
\$\begingroup\$

brainfuck, 45 49 41 37 34 bytes

,[>,]>+<<[<]>[<,[->-<]>[>]>],[>]>.

Try it online!

Takes the two integers separated by a single null byte. Null bytes are falsy in BF and everything else is truthy, so this prints a single 0x01 byte for truthy and a single 0x00 for falsy.

This is my first time golfing in BF, so feel free to give me suggestions...

Explanation

,[>,]       Read in everything from STDIN until the null byte is reached
>+          Move one space to the right and increment; this will be a flag telling us
            the numbers are still equal
<<[<]>      Move back to the beginning of the input
[           For each character in the input:
  <,          Move to the left and input a character
  [->-<]      Subtract this char from the char at the same spot in the original number
  >[>]        If this is not zero (there is a difference between the two characters)
                move to the hole before the flag
  >           Move one character to the right
              If we're in the hole before the flag this moves us onto the flag;
              the loop runs once more and deposits us two spots to the right
]           Endwhile
            When the while loop finishes we will be one spot to the left of the flag
            or already to the right if the program found a difference between the numbers
            However: we will still be just to the left of the flag for cases like 24 vs 245
,           So we input another byte (the next digit of the second number or 0x00)
[>]         and move past the flag if it's non-zero
>.          Move right (onto the flag or past the flag) and output
\$\endgroup\$
2
  • \$\begingroup\$ Here's a TIO link to see be able to the output of the program in hexbytes: Try it online!. \$\endgroup\$
    – user41805
    Jun 1, 2017 at 6:29
  • \$\begingroup\$ @Cowsquack doesn’t work anymore \$\endgroup\$
    – Stan Strum
    Jan 11, 2018 at 1:49
13
\$\begingroup\$

///, 9 bytes

//f///t/f

Try it online!

Prints a t if they are equal, otherwise prints f.

Since there is no way to take input in ///, it is hard-coded.

/NUM1/f//NUM2/t/f

Explanation:

With example inputs of 12 and 121213. (Should return false.)

  1. Input. /12/f//121213/t/f
  2. Replace other occurrences of the first number with f. /ff13/t/f
  3. Replace the modified new number with a t. Since the modified string is not f, , f stays the same. f.
  4. Output. f.

With example inputs of 501 and 501. (Should return true.)

  1. Input. /501/f//501/t/f
  2. Replace other occurrences of the first number with f. /f/t/f
  3. Replace the modified new number with a t. Since the modified string is f,f changes to t.
  4. Output. t.

Credit to @user202729 for coming up with the better approach!

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

yup, 16 bytes

I struggled to find an esolang where this was non-trivial (and that was not already covered). I present to you, this:

**-{0~-}0~-|0~-#

Try it online!

This works as follows:

**-{0~-}0~-|0~-#
**                  take two inputs on the stack
  -                 subtract them
   {0~-}0~-         this is "absolute value":
   {   }              while TOS is > 0
    0~-               this is negate:
    0                   push 0                 [TOS, 0]
     ~                  swap                   [0, TOS]
      -                 subtract               [-TOS]
                    thus, this negates positive numbers
        0~-         negate again to get a positive number
                    this maps equal numbers to `0` and unequal numbers to their
                    absolute differences
           |        take ln(TOS). This gives `-Infinity` for equal numbers, and
                    some value >= 0 for other numbers.
            0~-     negate. this gives Infinity for equal numbers, and a number <= 0
                    for unequal numbers
               #    output as number

Now, {...} is yup's loop and is the closest thing to a condition. It repeats the inside so long as the TOS is positive and defined. Thus, any negative or 0 value is falsey, and any positive value is truthy. Infinity is truthy, being greater than 0.

\$\endgroup\$
6
\$\begingroup\$

Brain-Flak, 26 bytes

Straight from the wiki.
Please don't upvote trivial answers.

([{}]{})((){[()](<{}>)}{})

Try it online!

\$\endgroup\$
1
5
\$\begingroup\$

Verbosity, 507 503 bytes

Include<MetaFunctions>
Include<Output>
Include<Input>
Include<Integer>
Include<Boolean>
Input:DefineVariable<i; 0>
Output:DefineVariable<o; 0>
Boolean:DefineVariable<c; 0>
Integer:DefineVariable<a; Input:ReadEvaluatedLineFromInput<i>>
Integer:DefineVariable<b; Input:ReadEvaluatedLineFromInput<i>>
Boolean:DefineVariable<r; Boolean:ArgumentsAreEqual<c; a; b>>
Boolean:DefineVariable<q; Boolean:LogicalNot<c; r>>
Output:DisplayAsText<o; q>
DefineMain<> [
MetaFunctions:ExecuteScript<MetaFunctions@FILE>
]

Try it online!

Saved 4 bytes thanks to Mr. Xcoder

Haha, no-one shall lose to Verbosity! Outputs with a Boolean<> wrapper around the result. The Boolean:LogicalNot is required due to an ongoing bug with booleans.

The ungolfed version:

Include<MetaFunctions>
Include<Output>
Include<Input>
Include<Integer>
Include<Boolean>

Input:DefineVariable<STDIN; 0>
Output:DefineVariable<STDOUT; 0>
Boolean:DefineVariable<constant; 0>

Integer:DefineVariable<FirstInput; Input:ReadEvaluatedLineFromInput<STDIN>>
Integer:DefineVariable<SecondInput; Input:ReadEvaluatedLineFromInput<STDIN>>
Boolean:DefineVariable<result; Boolean:ArgumentsAreEqual<constant; FirstInput; SecondInput>>

Boolean:RedefineVariable<result; Boolean:LogicalNot<constant; result>>

Output:DisplayAsText<STDOUT; result>

DefineMain<> [
    MetaFunctions:ExecuteScript<MetaFunctions@FILE>
]

Try it online!

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

Retina, 9 bytes

^(.+)¶\1$

Counts the number of times the first line matches the second.

Try it online!

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

Retina, 5 bytes

Byte count assumes ISO 8859-1 encoding.

D`
¶$

Try it online!

Explanation

D`

Deduplicate with the implicit regex .+, i.e. if the two lines are identical, clear the second one (the separating linefeed remains though).

¶$

Try to match a linefeed followed by the end of the string. This is only possible if the first stage cleared the second line, i.e. if the two numbers were equal.

\$\endgroup\$
4
\$\begingroup\$

Python 3, 10 bytes

int.__eq__

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Damn, 30 seconds too late \$\endgroup\$
    – shooqie
    May 31, 2017 at 20:27
  • \$\begingroup\$ Please explain the downvote. Have you tested this? \$\endgroup\$ Jun 1, 2017 at 11:08
  • \$\begingroup\$ I couldn't make it work in python 2, so I specified it to python 3 \$\endgroup\$ Jun 1, 2017 at 14:19
3
\$\begingroup\$

Japt, 2 bytes

¥V

Try it online

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

Java, 41 36 12 bytes

-4 bytes thanks to @totallyhuman - Changed floats to ints

-1 byte by removing space between second method argument and comma.

-24 by converting the whole program to a lambda (woo).

(i,j)->i==j;

Takes the form of a java.util.function.BiFunction< Boolean, Integer, Integer > using an expression lambda.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ @totallyhuman Thanks! Got a bit confused since the description said "2 decimal integers" \$\endgroup\$
    – svp
    May 31, 2017 at 22:34
  • \$\begingroup\$ float isn't decimal either; it's IEEE binary32. "decimal" is a stupid way of describing binary integers. It only makes sense when talking about their string representation, or with decimal floating point (en.wikipedia.org/wiki/Decimal_floating_point), which isn't provided in hardware by most FPUs. IIRC, IBM's POWER architecture has decimal float support, which is useful for some financial stuff. \$\endgroup\$ Jan 10, 2018 at 2:06
3
\$\begingroup\$

TI-BASIC, 13 bytes

Prompt A,B
If A=B
Disp 1
Else
Disp 0

Pretty self-explanatory. Prompts for two numbers and prints 1 if they are euqal, 0 otherwise.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can shorten this to 8 bytes with Prompt A,B:A=B since the result of the = operator is 0 or 1 and will be implicitly displayed at the end of the program. \$\endgroup\$
    – kamoroso94
    Jan 31, 2018 at 14:31
3
\$\begingroup\$

Cascade, 5 4 bytes

#&
=

Turns out I didn't need the second & due to wrapping

Try it online!

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

Flurry -nin, 134 bytes

<<>[(<<>()>)<<>[({}){}]>][<><<>[<>{{}}]()>]()()[<><<>()>]>({})[<><<>(){}>[()[<><<>()>]]]{{}{<{}{()[<>{}[<><<>()>]]}>}{{}(<>()){}}()}{}

Run example

$ pgm="..." # the program above
$ ./flurry -nin -c $pgm 0 0
1
$ ./flurry -nin -c $pgm 123 45
0
$ ./flurry -nin -c $pgm 123 123
1

There is no task that is trivial in every language. Seriously.

Since Flurry's only representation of a number is Church numeral, this program takes only non-negative integers as input. Outputs 1 if two Church numerals represent the same number, 0 otherwise.

Implements the following function:

// (n+1-m) * (m+1-n), where a-b gives zero if a < b
// This gives 1 when m==n (both sides of * are 1)
// and 0 otherwise (one side is 0)
main = (\npm. <SK> (m p (succ n)) (n p (succ m))) n pred m

// succ n = n + 1
succ = S<SK>

// pred n = n - 1 (pred 0 = 0); implemented using pair construct
next-pair-helper = \fmn. f n (succ n) = \fm. S f succ = \f. K(S f succ)
= {()[<>{}[<><<>()>]]}
next-pair = \p. <p next-pair-helper>
= {<{}{()[<>{}[<><<>()>]]}>}
pred = \n. n next-pair zero-pair K
= {{}{<{}{()[<>{}[<><<>()>]]}>}{{}(<>()){}}()}
\$\endgroup\$
1
  • \$\begingroup\$ It's not trivial in 1+, though. \$\endgroup\$ Aug 21, 2020 at 12:16
2
\$\begingroup\$

Jelly, 1 byte

=

Try it online!

Explanation:

= Takes two arguments and returns a 1 if they are equal, and a 0 if they are not. 
  Implicit print.
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Alternatively, e tests for equality, and _ will subtract the input values from one another, yielding 0 for equals and any other value for different number. \$\endgroup\$
    – steenbergh
    May 31, 2017 at 22:47
  • 1
    \$\begingroup\$ Also an answer for MATL \$\endgroup\$
    – Suever
    May 31, 2017 at 23:29
  • \$\begingroup\$ Works in APL and J too. \$\endgroup\$
    – Adám
    Jan 9, 2018 at 14:19
2
\$\begingroup\$

Ohm, 1 byte

E

Try it online!

Because = is too mainstream.

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

OCaml, 3 bytes

(=)

Brackets are necessary, plain = is a syntax error

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

sed, 14 bytes

Includes +1 for -r

s/^(.+),\1$//

sed doesn't have truthy/falsy values, so I use the empty string as truty and everything else as falsy. This make sense because /^$/ is the simplest (fully matching) if statement.

Try it online!

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

Haskell, 4 bytes

(==)

Try it online! (The function has to be named)

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

JavaScript (ES6), 12 bytes

x=>y=>!(x-y)

Because 0 is false and all other numbers are true in JavaScript, if x-y equals 0, a not ! of that 0 will return true, and a not ! of any other number will return false. Two bytes longer than simple comparison x==y as shown in @Shaggy's answer.

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

MSM, 31 bytes

'?ddF',',',.....T',',':'?....':

MSM is stack based, so the two input numbers are expected to be on top of the stack, i.e. on the right of the string. MSM has neither numbers nor booleans so we are free to choose a (reasonable) representation:

T      True
F      False
123    numbers are just sequences of ascii digits. There's no literal
       representation in MSM source code, so you have to construct them
       digit by digit:  321.. -> 123 (remember: . is concatenation)

TIO doesn't support MSM out of the box, so I've included the JS interpreter from the esolang page.

Try it online!

How it works: (I use a and b for the two numbers on the stack).

Excerpt from the MSM command reference:

'   quote, push next char on the stack, even if it is a command
?   skip next command if the two top elements of the stack are equal
,   drop
:   expand string at top of the stack and push each char of it on the stack
.   concatenate two top elements

everything else is pushed

Stack trace:

' ? d d F ' , ' , ' , . . . . . T ' , ' , ' : ' ? . . . . ' : a b
d d F ' , ' , ' , . . . . . T ' , ' , ' : ' ? . . . . ' : a b ?

The next 6 chars are pushed on the stack and concatenated with 5 dots

T ' , ' , ' : ' ? . . . . ' : a b ? ,,,Fdd

The next 5 chars are pushed on the stack and concatenated with 4 dots

' : a b ? ,,,Fdd ?:,,T

: and the two numbers are pushed

? ,,,Fdd ?:,,T : a b

Now it gets interesting. If the two numbers are equal, ,,,Fdd is skipped

?:,,T : a b           -- ?:,,T as a whole is not a command, so it's pushed
: a b ?:,,T           -- expand
a b ? : , , T         -- push a b
? : , , T a b         -- a b are still equal, so skip :
, , T a b             -- drop a b
T                     -- MSM stops, output is True

If the two numbers are not equal, don't skip ,,,Fdd, but push it:

,,,Fdd ?:,,T : a b
: a b ,,,Fdd ?:,,T    -- expand 
a b ,,,Fdd ? : , , T  -- push up to ?
? : , , T a b ,,,Fdd  -- number b is never equal to ,,,Fdd so expand
, , T a b , , , F d d -- drop two dummy values
T a b , , , F         -- push T a b
, , , F T a b         -- drop b a T
F                     -- stop    
\$\endgroup\$
2
\$\begingroup\$

Cubix, 11 bytes

O0O1II-!//@

Try it online!

This was harder than expected. Outputs 0 for falsy and 10 for truthy.

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

Hexagony, 20 bytes

-4 Bytes thanks to Jo King by using conditional wrapping.

?}?".@!\!.!_1/@_-<~.

Try it online!

More readable version:

   ? } ? "
  . @ ! \ !
 . ! _ 1 / @
_ - < ~ . . .
 . . . . . .
  . . . . .
   . . . .

Prints 0 for truthy and 1 for falsy.
There's a no-op at the end to keep it at a sidelength of 4.

It might be possible to reduce the sidelength by one and safe a few more bytes.


If I can print 0 for truthy and anything else for falsy, this would be a valid solution aswell:

  ? } ?
 / - ! @
" / . . .
 . . . .
  . . .

This is basically just subtraction and seems like cheating to me.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Jan 31, 2018 at 14:48
  • \$\begingroup\$ 20 bytes by using the built-in branching instead of another <. Technically it's 19 bytes, but an extra . is needed to keep it side length 4. I'll have a look into a side length 3 version \$\endgroup\$
    – Jo King
    Feb 14, 2018 at 15:12
2
\$\begingroup\$

Whitespace, 60 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][S S S T  N
_Push_1][S N
S _Duplicate_1][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][T    S S T   _Subtract][N
T   S N
_If_0_Jump_to_Label_EQUAL][T    N
S T _Print_as_integer][N
N
N
_Exit_program][N
S S N
_Create_Label_EQUAL][S S S T    N
_Push_1][T  N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Outputs 1/0 as truthy/falsey values. 3 bytes could be saved by removing NNN if 1/01 as truthy/falsey values are allowed.

Pseudo-code:

Integer i = STDIN as number
Integer j = STDIN as number
If(i == j):
  Call function EQUAL()
Print 0 to STDOUT
Exit program

function EQUAL:
  Print 1 to STDOUT
  Exit automatically with error
\$\endgroup\$
2
\$\begingroup\$

Caboose, 24 bytes

print(input()==input());

TIO

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

Keg, 1 byte(SBCS on Keg wiki)

=

Explanation:

=# Test whether they are equal
\$\endgroup\$
1
  • \$\begingroup\$ 1 byte \$\endgroup\$
    – lyxal
    Nov 16, 2019 at 8:07
2
\$\begingroup\$

Flurry, 30 bytes

{}{{}{}}[{}{{{}}}[<>()]](){{}}

You can test it with the interpreter:

$ for i in {0..5}; do
$   for j in {0..5}; do
$     ./Flurry -nin -c '{}{{}{}}[{}{{{}}}[<>()]](){{}}' $i $j
$   done | xargs
$ done
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1

In Flurry, popping from the stack returns the I combinator (λa. a), which also happens to be the Church numeral representation for the number 1. Thus we can compute N != M using the following method:

  • Push 0 to the stack.
  • Push 1 to the stack N times.
  • Pop from the stack M times and ignore the value.
  • Pop from the stack and return the value.

This is how the program works conceptually, but we can combine the first two steps and the second two steps using a few tricks.

  • {{}} represents the I combinator (1 = I = λa. a).
  • {{{}}} is a function that pushes its argument to the stack and returns the I combinator (λa. (push a; λb. b))
  • [<>()] represents zero (0 = S K = λab. b).
  • By applying the input number to both of these terms, we obtain the expression n{{{}}}[<>()], which has the following semantics:
    • If n > 1, push 0 to the stack, followed by n - 1 ones, and return 1.
    • If n = 0, push nothing to the stack and return 0.
  • {{}{}} is a function that takes an argument a, pops a value b from the stack, and returns ba. In this case, a and b are both guaranteed to be either zero or one, so it effectively returns 0 if (a, b) == (1, 0) and 1 otherwise.
  • By applying the input number to this term, and to the result of the previous term, we obtain the expression n {{}{}} [m {{{}}} 0], which has the following semantics:
    • If m = 0, [m {{{}}} 0] pushes nothing and returns 0. Since the stack is empty, {{}{}} always pops one, so its return value is always 1. Thus the entire expression is 0 if n = 0 and 1 otherwise.
    • If m > 0, [m {{{}}} 0] pushes 0 followed by n - 1 ones and returns 1. Thus {{}{}} always returns the value popped from the stack, and n {{}{}} 1 pops n times and returns the last value popped. The entire expression is 0 if n = m and 1 otherwise.

The last step is to take the resulting number (representing whether the values are unequal) and negate it by applying it to () and {{}}.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 10 bytes

x=>y=>x==y
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 1 byte

Q

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 15 bytes

lambda x,y:x==y

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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