That's quite an old puzzle. Just let's add some automation to it!
Brother Sobertino has a task to examine certain barrel of wine in the cellar - whether it is good enough for the upcoming feast or not.
Barrel is very large, and Sobertino got two buckets with him, with volumes of V and U gallons. However,
since he is not a drunkard, he only wants to measure small amount of T gallons for examination. The goal is
to get exactly this amount by the following manipulations with the buckets:
For example, if he has buckets of 3 and 5 gallons, and wants to measure out just 1 gallon, he can go
this sequence:
33 into bucket of 5 (now first is empty, second is 2 gallons short)3 again3 once more into bucket of 5 - only 2 really shall go, leaving 1 gallon in the
first bucket.So it takes 4 operations to measure 1 gallon with two buckets of 3 and 5.
Please find the minimal number of operations to get T gallons with buckets of U and V gallons.
Input data: just three values, U, V and T in a single line.
Answer should be a single value - required minimum number of operations.
Example:
input:
7 4 2
answer:
8