2010-03-06, 09:08 AM
Mm, interesting. I never thought of the binary approach.
Edit: But it actually doesn't save time if you want to calculate each p_n anyway. Hm...
I tested this on my system and it took about 6.2 seconds to compute hits(1000, 2000, 10000). Quadratic in HP means it takes ten minutes to work with 100000 HP.
Edit: But it actually doesn't save time if you want to calculate each p_n anyway. Hm...
Code:
function convolute(a, b)
local p = {
min = a.min + b.min,
max = a.max + b.max,
}
for n = p.min, p.max do
local sum = 0
for k = math.max(a.min, n-b.max), math.min(a.max, n-b.min) do
sum = sum + a[k]*b[n-k]
end
p[n] = sum
end
return p
end
Time complexity
I tested this on my system and it took about 6.2 seconds to compute hits(1000, 2000, 10000). Quadratic in HP means it takes ten minutes to work with 100000 HP.

