ActionScript challenge (im struggling)

I am struggling to write a piece of script which is part of my fencing estmator.
If any one can help me I would be well chuffed!!

We sell fencing in 3m, 2.4m and 1.8m panels.
I need to write a function which finds the best (least panels) combination of sections for the length of fence.

eg 40m = 13no 3m sections and 1no 1.8m section (total 40.8m but the closest you can get in sold panels).

I can work out what the total length would be in 3m’s and in 2.4m’s and in 1.8m’s but I can figure out how to write the code to decide the best & closest combination?

some thing like this (this is straight out my head not code)???:-

Totallength/3 = LengthIn3mBays
LengthIn3mBays - LengthIn3mBays(rounded down to whole number) = LeftOver
if {(LeftOver > 1.8 )
LastBay = 2.4
else if {
LeftOver < 1.8
LastBay = 1.8
}
}

any help would be wicked!! :violin: