N + r*N + r*r*N + r*r*r*N
= (N + r*N + r*r*N + r*r*r*N)*(1-r)/(1-r)
= (N + (r*N-r*N) + (r*r*N-r*r*N) + (r*r*r*N-r*r*r*N) + ...)/(1-r)
= N/(1-r)
####
my $max_size = 0;
my $size = 0;
my $recopies = 0;
my $last_pow = 1;
while (++$size) {
if ($size >= $max_size) {
use integer;
my $old_size = $size - 1;
$max_size = $size + $old_size/5;
$recopies += $old_size;
}
if (not $size%2 and ($size>>1) == $last_pow) {
my $avg = $recopies/$size;
printf("% 8d: % 9d recopies. Avg %1.5f per element\n",
$size, $recopies, $avg);
$last_pow = $last_pow + $last_pow;
}
}
##
##
2: 1 recopies. Avg 0.50000 per element
4: 6 recopies. Avg 1.50000 per element
8: 28 recopies. Avg 3.50000 per element
16: 81 recopies. Avg 5.06250 per element
32: 195 recopies. Avg 6.09375 per element
64: 390 recopies. Avg 6.09375 per element
128: 783 recopies. Avg 6.11719 per element
256: 1332 recopies. Avg 5.20312 per element
512: 2726 recopies. Avg 5.32422 per element
1024: 5605 recopies. Avg 5.47363 per element
2048: 11566 recopies. Avg 5.64746 per element
4096: 23916 recopies. Avg 5.83887 per element
8192: 41278 recopies. Avg 5.03882 per element
16384: 85513 recopies. Avg 5.21930 per element
32768: 177228 recopies. Avg 5.40857 per element
65536: 367397 recopies. Avg 5.60603 per element
131072: 761722 recopies. Avg 5.81148 per element
262144: 1316176 recopies. Avg 5.02081 per element
524288: 2729094 recopies. Avg 5.20533 per element
1048576: 5658908 recopies. Avg 5.39676 per element
2097152: 11734158 recopies. Avg 5.59528 per element
4194304: 24331784 recopies. Avg 5.80115 per element
8388608: 42045207 recopies. Avg 5.01218 per element