20
\$\begingroup\$

Introduction

How to prove that 1 = 2:

1 = 2 
2 = 4 (*2)
-1 = 1 (-3)
1 = 1 (^2)

You can just multiply both sides by 0, but that's cheating. This is just bending the rules a little bit.

Your challenge:

Write a program/function that, when given two integers, outputs a "proof" that the two are equal.

Rules

  • Your program's output must start with a = b, where a and b are the integers, and end with c = c, where c can be any number. Don't output the operations.
  • Your output can only use integers.
  • The only operations you can use are addition, multiplication, division, subtraction, and exponentiation, and you cannot multiply, divide or exponentiate by 0.
  • Each line must be one of the above operations done to both sides of the equation in the previous line.

For example, you could go:

4 = 9
5 = 10 (+1)
1 = 2 (/5)
8 = 16 (*8)
-4 = 4 (-12)
16 = 16 (^2)

Scoring

This is , shortest wins!

\$\endgroup\$
9
  • \$\begingroup\$ Sandboxed. Going to sleep now, will try to clarify stuff later. \$\endgroup\$
    – emanresu A
    Jun 1 at 11:46
  • 4
    \$\begingroup\$ Alternatively phrased, this is equivalent to finding the steps from an input \$(a, b)\$ to \$(-n, n)\$ for some integer \$n\$, where the "steps" are the 5 provided operators \$\endgroup\$ Jun 1 at 11:55
  • 2
    \$\begingroup\$ May we assume that a≠b? \$\endgroup\$ Jun 1 at 11:57
  • 7
    \$\begingroup\$ From the sandbox: It's worth mentioning that for any a=b as input you can always do the steps a=b;2a=2b (*2); a-b=b-a (-(a+b));a^2-2ab+b2=a^2-2ab+b2 (^2). Sometimes you can do faster but there is not much reason to do anything more complex than (*2)->(-(a+b))->(^2). \$\endgroup\$
    – Wheat Wizard
    Jun 1 at 15:49
  • 2
    \$\begingroup\$ Must I separate integers and an equal sign with a space? \$\endgroup\$ Jun 2 at 12:20

13 Answers 13

11
\$\begingroup\$

JavaScript (Node.js), 57 54 bytes

a=>b=>a+`=${b}
${a+a}=${b+b}
${a-=b}=${-a}
${a*=a}=`+a

Try it online!

How?

There always exists a multiple of \$0.5\$ which, when subtracted from each number, creates a pair of the form \$-a = a\$. If \$a-x=c\$, then \$b-(a-x+b)=-c\$, so then if \$a-x+b=x\$, then \$x=\frac{(a+b)}2\$. This might not be an integer, so multiply by 2 beforehand.

-3 bytes thanks to Arnauld.

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

05AB1E, 13 12 bytes

xIÂ-Dn)'=δý»

Try it online!

A port of my Jelly answer. I thought that 05AB1E's stack-based functionality would be shorter, but it isn't great at joining.

-1 byte thanks to ovs!

xIÂ-Dn)'=δý» - Full program. Takes [a, b] on the stack
x            - Push [2a, 2b];   Stack = [[a, b], [2a, 2b]]
 I           - Push [a, b];     Stack = [[a, b], [2a, 2b], [a, b]]
  Â          - Bifurcate;       Stack = [[a, b], [2a, 2b], [a, b], [b, a]]
   -         - Subtract;        Stack = [[a, b], [2a, 2b], [a-b, b-a]]
    D        - Duplicate;       Stack = [[a, b], [2a, 2b], [a-b, b-a], [a-b, b-a]]
     n       - Square;          Stack = [[a, b], [2a, 2b], [a-b, b-a], [(a-b)², (b-a)²]]
      )      - Wrap into array; Stack = [[[a, b], [2a, 2b], [a-b, b-a], [(a-b)², (b-a)²]]]
       '=    - Push '='
         δý  - Join each with '='
           » - Join array by newlines
\$\endgroup\$
2
  • \$\begingroup\$ A few 12-byters. '=δý uses double-vectorize (δ), the other ones use that » joins inner lists by spaces. \$\endgroup\$
    – ovs
    Jun 1 at 14:52
  • \$\begingroup\$ @ovs Nice! I think I'll go with the first one, δ`` seems a lot easier to understand than either #` or :, especially in the context of » \$\endgroup\$ Jun 1 at 15:00
7
\$\begingroup\$

R, 64 51 bytes

-7 bytes thanks to Dominic van Essen.

cat(sep=c("=","
"),x<-scan(),2*x,y<-2*x-sum(x),y^2)

Try it online!

The steps followed are:

a = b
2a = 2b (*2)
a-b = b-a (-(a+b))
(a-b)^2 = (b-a)^2 (^2)
\$\endgroup\$
4
  • \$\begingroup\$ 55 bytes by changing step 3 to 'subtract both sides from a+b' instead of 'subtract a+b from both sides' and rearranging a bit... \$\endgroup\$ Jun 1 at 21:45
  • \$\begingroup\$ @DominicvanEssen Thanks, but the way I read the rules, I think that would require 2 steps (first multiply by -1, then add a+b). Putting sep up front is a great idea! \$\endgroup\$ Jun 1 at 22:00
  • \$\begingroup\$ This should be Ok though...? \$\endgroup\$ Jun 2 at 0:06
  • \$\begingroup\$ @DominicvanEssen Very nice! \$\endgroup\$ Jun 2 at 13:11
6
\$\begingroup\$

Jelly, 14 bytes

,Ḥ;_U,²$Ɗj€”=Y

Try it online!

Uses ophact's observation, that for any \$a, b\$, it is sufficient to yield the 4 equations

$$ a = b \\ 2a = 2b \\ a-b = b-a \\ (a-b)^2 = (b-a)^2 $$

How it works

,Ḥ;_U,²$Ɗj€”=Y - Main link. Takes [a, b] on the left
 Ḥ             - Double, yielding [2a, 2b]
,              - Pair; [[a, b], [2a, 2b]]
        Ɗ      - Previous three links as a monad f([a, b]):
    U          -   Reverse; [b, a]
   _           -   Subtract; [a-b, b-a]
       $       -   Previous two links as a monad f([a-b, b-a]]):
      ²        -     Square; [(a-b)², (b-a)²]
     ,         -     Pair; [[a-b, b-a], [(a-b)², (b-a)²]]
  ;            - Concatenate; [[a, b], [2a, 2b], [[a-b, b-a], [(a-b)², (b-a)²]]
         j€”=  - Join each with "="
             Y - Join with newlines
\$\endgroup\$
3
\$\begingroup\$

Python 2, 79 78 bytes

for x in'input()','2*a,2*b','a-b>>1,b-a>>1','a*a,'*2:a,b=eval(x);print a,'=',b

Try it online!


Python 3, 70 bytes

def f(a,b):x=a-b;return f'{a}={b}\n{2*a}={2*b}\n{x}={-x}\n{x*x}={x*x}'

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I actually pointed out this method in the sandbox before ophact. But early bird gets the worm. \$\endgroup\$
    – Wheat Wizard
    Jun 1 at 15:54
  • \$\begingroup\$ @WheatWizard I did not look at the sandbox before posting. \$\endgroup\$
    – user100690
    Jun 1 at 18:15
  • \$\begingroup\$ With Python 3.8 you can get 64 bytes. \$\endgroup\$ Jun 1 at 22:09
3
\$\begingroup\$

Python 2, 84 \$\cdots\$ 73 71 bytes

Saved 5 bytes thanks to ovs!!!
Saved 2 bytes thanks to dingledooper!!!

k=a,b=input()
d=a-b
for a,b in k,[2*a,2*b],[d,-d],[d*d]*2:print a,'=',b

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ 73 bytes as a full program (or 75 if you keep it as function). \$\endgroup\$
    – ovs
    Jun 1 at 20:23
  • \$\begingroup\$ @ovs Nice one - thanks! :D \$\endgroup\$
    – Noodle9
    Jun 1 at 20:56
  • \$\begingroup\$ 71 bytes \$\endgroup\$ Jun 1 at 22:05
  • 1
    \$\begingroup\$ 61 bytes \$\endgroup\$
    – Makonede
    Jun 2 at 0:16
  • 1
    \$\begingroup\$ 54 bytes with Python 3.8's walrus operator. \$\endgroup\$ Jun 2 at 3:17
3
\$\begingroup\$

C (clang), 65 bytes

#define r;printf("%d=%d\n",a
f(a,b){r,b)r+a,b+b)r-=b,-a)r*=a,a);}

Try it online! Uses ophact's approach.

Using clang instead of gcc saves a byte, as clang handles the undefined behavior in the second printf differently than gcc:

printf("%d=%d\n",a-=b,-a);

The order of the arguments' evaluation is unspecified. In gcc, the -a is evaluated before a-=b; thus, the second argument is not affected by the subtraction and must be b-a to get the proper value. However, in clang, the a-=b is evaluated first so the second argument is affected by the subtraction so -a is the correct value.


C (gcc), 76 74 66 bytes

-8 bytes thanks to tsh

#define r;printf("%d=%d\n",a
f(a,b){r,b)r+a,b+b)r-=b,b-a)r*=a,a);}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 66: #define r;printf("%d=%d\n",a f(a,b){r,b)r+a,b+b)r-=b,b-a)r*=a,a);} \$\endgroup\$
    – tsh
    Jun 4 at 8:21
2
\$\begingroup\$

Charcoal, 20 bytes

≔⁻⊗θΣθηE⟦θ⊗θηXη²⟧⪫ι=

Try it online! Link is to verbose version of code. Takes input as a list [a, b]. Explanation: Based on @ophact's approach.

≔⁻⊗θΣθη

Vectorised subtract the sum of the list from its double, thus giving [a-b, b-a].

E⟦θ⊗θηXη²⟧⪫ι=

Loop over the lists [a, b], 2[a, b], [a-b, b-a] and [a-b, b-a]², joining each list with = and implicitly printing them on separate lines.

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

C# (Visual C# Interactive Compiler), 50 bytes

Same as the javascript but using C#'s "superior code golfing" interpolation

a=>b=>a+@$"={b}
{a+a}={b+b}
{a-=b}={-a}
{a*=a}="+a

Try it online!

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

Vyxal j, 12 bytes

:d?Ḃ-:²Wƛ\=j

Try it Online!

Why write your own original answer when you can just port 05ab1e amiright? :p

Explained

:d?Ḃ-:²Wƛ\=j
:d           # [a, b], [2a, 2b]
  ?Ḃ-        # [a - b], [b - a]
     :²      # that, but squared
       Wƛ\=j # join each on "=" and then join that on newlines with the j flag
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 48 bytes

port of ophact's answer

a\b="$a=$b
$(2a)=$(2b)
$(a-=b)=$(-a)
$(a*=a)=$a"

Try it online!

alternative answer, 48 bytes too

output is a list of strings

a\b=join.([a=>b,2a=>2b,(a-=b)=>-a,a*a=>a*a],'=')

Try it online!

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

Python 3, 72 bytes

f=lambda a,b:f"{a}={b}\n{2*a}={2*b}\n{a-b}={b-a}\n{(a-b)**2}={(a-b)**2}"

Uses ophact's observation to answer in 4 steps.

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Nice! You can save two bytes by going a+f"= at the start and ="+(a-b)**2 at the end. \$\endgroup\$
    – emanresu A
    Jun 8 at 4:24
  • \$\begingroup\$ @Ausername That would be attempting to concatenate an integer and a string, which Python doesn't allow. Thanks anyway! \$\endgroup\$ Jun 8 at 10:50
  • \$\begingroup\$ Sorry, I'm used to Javascript. \$\endgroup\$
    – emanresu A
    Jun 8 at 11:02
  • \$\begingroup\$ @Ausername Oh, ok. Javascript has a lot of strange type conversions (e.g. []+[] gives ""), so that makes sense. \$\endgroup\$ Jun 8 at 11:23
0
\$\begingroup\$

GolfScript, 37 bytes

~:%;:$' = ':&%n$2*&%2*n$%-&%$-.n\2?.&\

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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