Is it possible to solve a puzzle using lcm?

135 Views Asked by At

I was solving one programming puzzle, which says that:

Somebody asks you how much competitors do you have and you answer that K part started with first problem, M with second, N with third while D think where to start. If it's true statement you should find number of competitors(S) or say that statement is incorrect.

We can create an equation and solve it:

S/K + S/M + S/N + D = S
S = D * K * N * M / (K * N * M - N * M - K * M - K * N )

If denominator <= 0 or numerator % denominator <> 0 we can say that statement is incorrect, otherwise it's true.

I also assumed that I can solve this task using lcm in such a way:

S = lcm(lcm(K, N), M)

and if

S/K + S/N + S/M + D == S

than statement is correct, otherwise it's false.

But in this case I can't pass all the tests. Can you tell me if my assumption is incorrect?

1

There are 1 best solutions below

5
On BEST ANSWER

You can solve your equation for $S$: $$S=\frac D{\left(1-\frac1K-\frac1N-\frac1M\right)}.$$

The first thing to check is that $S$ is an integer, which can be done with integer arithmetic exactly as you described.

If it is an integer, then you also have to check that each of $S/K$, $S/N$ and $S/M$ are integers, or equivalently that $S \mathrm{\ mod\ } K$, $S \mathrm{\ mod\ } N$, and $S \mathrm{\ mod\ } M$ are all zero. This is the same as checking that $\mathrm{lcm}(K,N,M)$ divides $S$, so in principle you can do this part with lcm.