[Gllug] OT: algorithm needed

Dylan dylan at dylan.me.uk
Sun Aug 10 21:13:42 UTC 2008


Please ignore my last post - I typed before I engaged my brain!

On Sunday 10 August 2008, salsaman at xs4all.nl wrote:
> Hi,
> here is one for the maths lovers.
>
> I am trying to devise an algorithm to read in blocks from disk. There is a
> total read size, and a max block size.
>
> On each read I read in max_block_size or less bytes, until the total is
> reached.
>
> However, I want the last read to be as close to max_block_size as possible.
>
> E.g. if total==100, max==45, I could read:
>
> 45, 45, 10.
>
> However it would be preferable to read:
>
> 40, 40, 20, or better yet:
>
> 35, 35, 30
>
>
> Also, I want to do this without pre-calculating,

I assume you mean RE-calculating - you surely need to pre-calculate the 
necessary values:

number-of-blocks = ROUND-UP(total-read-size / max-block-size)
main-block-size = ROUND-UP(total-read-size / number-of-blocks)
temp-total = number-of-blocks * main-block-size
overshoot = temp-total - total-read-size
last-block-size = main-block-size - overshoot

there may well be a more elegant way of doing this, and it's definitely not 
optimal I shouldn't think.

Dx

> i.e the next block size 
> must be calculated from total and max.
>
> Any ideas ? Is there a simple algorithm to do this ?
>
>
> Gabriel.
> http://lives.sourceforge.net



-- 
“ ‘... but there is so much else behind what I say. It makes itself known to 
me so slowly, so incompletely! ...’ ”
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list