1

Related question: Calculating truck cargo capacity in a game

In my related question I have 3 different types of trucks, but I've decided to make it more simplified and just have a simple number that holds how many trucks each player has.

I need to find out how many trucks I need for a given cargo.

Each truck can carries 50 crates (truckCapacity)

Player 1 has 0 trucks (numberOfTrucks)

The cargo I want to move is 10 crates (cargo)

I've tried the following;

sum = truckCapacity / cargo
= 5

This is wrong, if I am moving 10 crates, it should be 1. As I only need 1 truck to move 10 crates

I've tried

sum = truckCapacity - cargo
= 40

This doesn't tell me how many trucks I need though.

I tried, as per the accepted answer in my related question,

current_capacity(truck) = total_capacity(truck) - loaded_crates(truck)

But this only loads one truck at a time, and doesn't tell me how many extra trucks I need to purchase.

What I'm after is;

I feed in the cargo and it returns with how much trucks it needs to move the said cargo.

It should also work if I have lots of cargo to move. So, if I want to move 500 crates, it should tell me that 10 trucks are needed (50 crates per truck * 10 trucks = 500 crates moved)

Things like how much space is left over in a truck is not important.

Community
  • 1
  • 1
zardon
  • 2,910
  • 6
  • 37
  • 58
  • Did you try the recommendation of ceiling(cargo/truckCapacity)? If both cargo and truckCapacity have integer types, you should cast on of them to double for the divide. For example, if you have truckCapacity 50 and a 125 crate cargo, cargo/truckCapacity is 2.5, and the ceiling increases that to 3. – Patricia Shanahan Nov 09 '12 at 15:22

4 Answers4

6

The correct answer has already been given, but I want to show you why it is correct and how you can come up with it by yourself given what you already know:

In your own calculation, you say:

50 crates per truck * 10 trucks = 500 crates moved

Symbolically, this reads:

cratesPerTruck * trucks = crates

This says, that given a capacity and the amount of available trucks, you can calculate how many crates you can move.

You can now solve for trucks:

cratesPerTruck * trucks = crates                        -- divide both sides by cratesPerTruck
cratesPerTruck / cratesPerTruck * trucks = crates / cratesPerTruck
1 * trucks = crates / cratesPerTruck
trucks = crates / cratesPerTruck

There is your answer.

Intuition

There are two common interpretations of division:

  1. distribution
  2. fitting objects into another1

Fitting: We can also ask ourselves, how many times a number fits into another.
For instance, we need to span a chain across a distance of 50 meters, but we can only buy chains of 2 meters. How many chains do we need to chain together?

50 / 2 = 25: Two meters fit 25 times into 50 meters, so we need 25 two-meter chains.

Applying fitting to your example: Let's just say every crate of yours is one meter in length, and you have 50 of them in total. Secondly, every truck of yours has a loading area that is 10 meters in length. You can imagine the loading areas to be lined up behind each other, so in total you need to know how many times one loading area fits into your 50 meters of crates.

50/10 = 5 trucks are needed.

Distribution: Suppose we have 10 apples and 5 people. We distribute the apples among the people, how many apply does every person get?

10/5 = 2, so everybody gets two apples.

Applying distribution to your example is less intuitive2, but it does work.
Let's say a truck has a capacity for 4 crates, so there is spot 1, 2, 3 and 4.

If you were to stack the crates onto those four spots, the amount of levels you get is equal to how many trucks you need to transport the entire load.

If we have 24 crates to distribute among the spots, there will be 6 crates stacked on each spot, meaning you need 6 trucks in total.

Dimensional Analysis

cratesPerTruck describes a ratio – as hinted by per – the same way speed does which describes how many meters per second you walk:

  • crates divided by trucks
  • meters divided by seconds

As you can see from this simple word substitution, these units describe a division:

crates per truck

If you divide an number of crates by crates divided by trucks, you get the following:

dimensional analysis

Dividing by a fraction is the same as multiplying by it's inverse. The [crates] then cancel, leaving us with [trucks].


Footnotes:
1Or drop me a comment with a better idea.

phant0m
  • 16,595
  • 5
  • 50
  • 82
3

Division is the way to go, but you divided in the wrong direction. If you know cargo and cargo_per_truck, then:

trucks = cargo / cargo_per_truck

So, given 500 crates at 50 crates per truck, you need 500/50 = 10 trucks.

Of course, this will give you fractional trucks - with 10 crates, you need 10/50 = 0.2 trucks - so you need to always round up:

trucks = ceil(cargo / cargo_per_truck)
Chowlett
  • 45,935
  • 20
  • 116
  • 150
  • It might be also worth mentioning the effects of integer calculations in division. In some languages (c# for example) 10/50 would be 0 because it would calculate it as an integer. Of course in c# you can't use `Math.Ceiling` on an integer. Its certainly a pothole worth mentioning though. – Chris Nov 09 '12 at 13:35
  • @Chris With integers, you can do `(cargo + (cargo_per_truck - 1)) / cargo_per_truck`. – phant0m Nov 09 '12 at 14:03
3

It's ceiling(cargo/truckCapacity).

In mathematics and computer science, the floor and ceiling functions map a real number to the largest previous or the smallest following integer, respectively. More precisely, floor(x) = is the largest integer not greater than x and ceiling(x) = is the smallest integer not less than x. http://en.wikipedia.org/wiki/Floor_and_ceiling_functions

wxyz
  • 709
  • 3
  • 8
0

You could try using:

(5021 / 50) mod 1

where 5021 is your cargo and 50 your capacity. you will get your amount of trucks by cargo/capacity. The mod part will check out what is left over after extracting trucks, 1 by 1.

This value will in this case either return 0 or 1 since its an integer it only shows whole numbers.

So what you want is check if you have anything left. If not (the mod returned 0) you need cargo/capacity = trucksNeeded. If there is (the mod returned 1) you need (cargo/capacity) + 1 = trucksNeeded.

in short: trucksNeeded = (cargo/capacity) + (cargo/capacity mod 1)

Teun Pronk
  • 1,367
  • 12
  • 24