Close formula for the following iterative process

31 Views Asked by At

I'm trying to get a formula which results in the number of merge steps needed to merge several intermediate files. The code comments which I'm studying say:

Assuming more than factor segments, the first pass should attempt to bring the total number of segments - 1 to be divisible by the factor - 1 (each pass takes X segments and produces 1) to minimize the number of merges.

So, for example, supposing that I have 20 segments and a factor of 10, one will have:

20 - (((20 - 1) mod (10 - 1)) + 1) = 19 // 18 left plus one merged

19 - 10 = 9 + 1 //9 left plus one merged and finally

10 - 10 = 0 // all segments merged

How can I formulate this? Do I need a recursive equation?

Thanks,

1

There are 1 best solutions below

0
On BEST ANSWER

$$ \text{numruns}=\begin{cases}\frac{\text{numseg}}{\text{factor}}+1&\text{if }(\text{numseg}-1)\%(\text{factor}-1)=0\\ \left\lceil\frac{\text{numseg}-((\text{numseg}-1)\%(\text{factor}-1))}{\text{factor}}\right\rceil+1&\text{otherwise}\end{cases} $$