21
\$\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
    Commented Jun 1, 2021 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\$ Commented Jun 1, 2021 at 11:55
  • 2
    \$\begingroup\$ May we assume that a≠b? \$\endgroup\$ Commented Jun 1, 2021 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
    Commented Jun 1, 2021 at 15:49
  • 2
    \$\begingroup\$ Must I separate integers and an equal sign with a space? \$\endgroup\$
    – user100411
    Commented Jun 2, 2021 at 12:20

14 Answers 14

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
  • \$\begingroup\$ 54 bytes \$\endgroup\$
    – Arnauld
    Commented Jun 1, 2021 at 12:18
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
    Commented Jun 1, 2021 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\$ Commented Jun 1, 2021 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\$ Commented Jun 1, 2021 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\$ Commented Jun 1, 2021 at 22:00
  • \$\begingroup\$ This should be Ok though...? \$\endgroup\$ Commented Jun 2, 2021 at 0:06
  • \$\begingroup\$ @DominicvanEssen Very nice! \$\endgroup\$ Commented Jun 2, 2021 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
    Commented Jun 1, 2021 at 15:54
  • \$\begingroup\$ @WheatWizard I did not look at the sandbox before posting. \$\endgroup\$
    – user100690
    Commented Jun 1, 2021 at 18:15
  • \$\begingroup\$ With Python 3.8 you can get 64 bytes. \$\endgroup\$ Commented Jun 1, 2021 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
    Commented Jun 1, 2021 at 20:23
  • \$\begingroup\$ @ovs Nice one - thanks! :D \$\endgroup\$
    – Noodle9
    Commented Jun 1, 2021 at 20:56
  • \$\begingroup\$ 71 bytes \$\endgroup\$ Commented Jun 1, 2021 at 22:05
  • 1
    \$\begingroup\$ 61 bytes \$\endgroup\$
    – Makonede
    Commented Jun 2, 2021 at 0:16
  • 1
    \$\begingroup\$ 54 bytes with Python 3.8's walrus operator. \$\endgroup\$ Commented Jun 2, 2021 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
    Commented Jun 4, 2021 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
    Commented Jun 8, 2021 at 4:24
  • \$\begingroup\$ @Ausername That would be attempting to concatenate an integer and a string, which Python doesn't allow. Thanks anyway! \$\endgroup\$ Commented Jun 8, 2021 at 10:50
  • \$\begingroup\$ Sorry, I'm used to Javascript. \$\endgroup\$
    – emanresu A
    Commented Jun 8, 2021 at 11:02
  • \$\begingroup\$ @Ausername Oh, ok. Javascript has a lot of strange type conversions (e.g. []+[] gives ""), so that makes sense. \$\endgroup\$ Commented Jun 8, 2021 at 11:23
0
\$\begingroup\$

GolfScript, 37 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Desmos, 207 characters

Try it online!

Organized for readability

Code:

<input 1>
<input 2>
<input 3>
a=ans_0
b=ans_1
c=ans_2
m=-(a+b)/2                           #Parenthesises placed here to show that addition occurs before addition; they don't appear in actual graph
d=-((a-m)^2-c)
l=[a,a+m,(a+m)^2,(a+m)^2+d]
l_1=[b,b+m,(b+m)^2,(b+m)^2+d]
(-1,[0,-1,-2,-3])
(0,[0,-1,-2,-3])
(1,[0,-1,-2,-3])
(4,0)
(4,-1)
(4,-2)
(4,-3)
                                    #LABELS (From higher to lower)
${l}
=
${l_1}
(given)
(${m})
(`^2`)
(${d})

Explanation:

a and b are the starting numbers, and c is the target number. Like my other programs, ans_n correspond to the line number, acting as inputs.

m is the midpoint of a and b. The reason that it is subtracted from a and b is so that they become equidistant from 0. NOTE: m is negative simply for formatting the proof; simply treat it as a positive and that all operations involving it are subtractions.

d is simply the distance between c and (a-m)^2. The squaring ensures that the term is positive, making both sides of the equation equal to each other. From there, the distance is added on to the result, making both sides of the equation equal to c.

l and l_1 are lists. This is used to provide the labels. From left to right, the terms in the lists are as follows: the input, the input minus m, the square of (the input minus m), and the square of (the input minus m) plus d.

The first point, (-1,[0,-1,-2,-3]), uses ${l} as a label. The list within the point creates multiple copies of the point at those y values, and the label uses the corresponding value from l to make it have the corresponding value on the graph. For example, (-1,0) would have a as its label. Likewise, the third point, (1,[0,-1,-2,-3]), would use l_1 to produce its labels in a similar manner. The second point, however, would simply have the label be =.

Point (4,0) has the label (given), which is self explanatory.

Point (4,-1) has the label (${m}), which uses m to produce the label of (m).

Point (4,-2) has the label (`^2`). The backticks are used to format the ^2 as a square and not as ^2.

Point (4,-3) has the label (${d}), which uses d to produce the label of (d).

\$\endgroup\$
2
  • \$\begingroup\$ a=ans_0 (and the other ans's) doesn't paste correctly; you will need to add a backslash in front of every single one of them. For example, a=\ans_0 \$\endgroup\$
    – Aiden Chow
    Commented Jul 7, 2022 at 1:26
  • \$\begingroup\$ Also could you provide the code without any comments in them? I'm not seeing how you are getting 207 bytes. \$\endgroup\$
    – Aiden Chow
    Commented Jul 7, 2022 at 1:29

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.