[Community Puzzle] Unit Fractions

Easy but funny puzzle. I tried to solve it using golang, to learn it. Thank you !

1 Like

I ran into a similar problem you had. How did you solve it?

I only saw brut force resolutions ^^
It would be interesting to see an optimized solution based on the prime factorization of n :
x and y are both >n let say x = n+a

1/n = 1/(n+a) + 1/k

k = 1/(1/n-1/(n+a))
k = 1/(a/((n(n+a)))*
k = n(n+a)/a*
k = n + n^2/a

=> 1/n = 1/(n+a) + 1/(n+n^2/a)

So if x = n+a, y = n+n^2/a

a needs to be a divisor of n^2, so finding all divisors of n^2 (by combinations of it’s prime factors) provides the solutions :slight_smile:

1 Like

The biggest enemy in this was dealing with C data types.
For anybody writing this in C, beware that your values will not fit into floats or ints, you have to use doubles and long longs.

Also incorrect format specifiers will silently fail later test cases, but pass earlier ones. Make sure you’re using %lld and not %ld or %d.

The puzzle was really annoying cause the divisions in js kept giving inaccurate results. For anyone who is attempting it and struggling with the puzzle because of divisions, try manipulating the equation you have so that all the values you need to find can be obtained through multiplication.