Hacker News new | ask | show | jobs
by bperlman 1497 days ago
I'm confused: If Sandy tells Peter her sum, why doesn't Peter use the quadratic formula to solve for the x,y pair algebraically?

x + y = s

x * y = p

x = s - y

x = p/y

substitute for x

p/y = s - y

p = sy - y*2

y*2 - sy + p = 0

# use the quadratic formula to solve for y

y = (-b ± √(b²-4ac)) / (2a)

In python:

import numpy as np

x = np.random.randint(1,100)

y = np.random.randint(1,100)

s = x+y

p = xy

a = 1

b = -1s

c = p

y_solved = (-1b + (b*2 - 4ac)*.5)/(2a),(-1b - (b*2 - 4ac)*.5)/(2a)

print(x,y,y_solved)

1 comments

That's the point, Sandy doesn't tell Peter her sum, nor vice versa. Sandy only knows the sum, Peter only knows the product, and they both know that the other doesn't (yet) know the answer. It allows them (in turns) to eliminate all the pairs that produce unique sums/products, until Peter ends up with one single pair.