tags:

views:

88

answers:

6

Feeling pretty brain-dead right now. I can, of course, brute-force this, but I feel like there has to be a simple function to return this number. Concerning native functions, I'm using PHP and/or Python.

For example: there exists containers that hold X (5) breadsticks each, and I need to feed Y (25) people Z (3) breadsticks each.

I need to return the number of containers I have to acquire to feed these people. (There may or may not be remainder breadsticks).

EDIT: Clarified some intention.

+3  A: 

It sounds like you want arithmetic:

min_containers = y*z/x

If you have situations that may give a remainder:

min_full_containers = floor(y*z/x)
remaining_items = y*z%x
JoshD
On the first couple tries, it seems that this gives 1-too-few containers. I may not have been clear enough about my intention with "full containers". I will edit the question.
anonymous coward
Then do ceiling(y*z/x). You did ask for full containers, if you have remaining pieces, you will need a partially filled container, so ceiling is what you want.
JoshD
+1  A: 
#python
import math

int(math.ceil(float(Y) * Z / X))
Ned Batchelder
-1 That's one import and 3 function calls too many
John Machin
A: 

($people * $breadSticksPerPerson) / $holders isn't correct?

EDIT: Sorry, misread your question, posted the correct solution in comments

Gordon
In this case, I am determining `$holders`, it is not yet known.
anonymous coward
Oh, in that case, it would be $holders = ($people * $breadsticksPerPerson) / BREADSTICKS_PER_HOLDER;
Gordon
@Gordon: -1 This can be correct only if PHP integer division rounds up.
John Machin
+1  A: 

Ned's answer is correct. It is also common to avoid the function call overhead to math.ceil() by doing the following:

minContainers = int((y*z+(x-1))/x);
par
-1 It's unfortunately all too common to muck about with floating point for an integer-only problem.
John Machin
Please don't neg comments unless they are wrong. My answer is correct.
par
Functionally correct but horrible
John Machin
If you're referring to Ned's post, yes, it wastes time. You'll note though that my solution is the same as yours in three lines of code instead of one. I'm not sure ow do you figure it "mucks about with floating point"? There's only integer math going on there.
par
@par: 3 lines of code was to give explanation instead of the x,y,z. Not same. In Python 3, `int` / `int` => `float`. My solution works for both Python 2 and Python 3. If you think there's only integer math, you don't need the `int()`.
John Machin
int/int = float. Now your criticism makes sense, I've got a C background not Python so that's an unexpected implicit typecast for me. The int() cast was to show the OP that's what is intended. The double-slash floor truncation operator is handy but looks like a C++ comment to me ;)
par
+1  A: 

In Python, use // (integer floor division, introduced in Python 2.2) and force it to round up:

number_required = y * z
container_holds = x
reqd_containers = (number_required + container_holds - 1) // container_holds

or if you require the so-called "professional programmer" version instead of the explanatory version:

n=(y*z+(x-1))//x;

or if you are really afraid of carpal tunnel syndrome, chop the two redundant parentheses and the semicolon:

n=(y*z+x-1)//x

Note: this solution works on both Python 2 (where 10 / 3 -> 3) and Python 3 (where 10 / 3 -> 3.3333333333333335)

Other "solutions" not only use unnecessary function calls but also fail with large numbers:

# wrong in Python 3; works with Python 2.3 to 2.7
# int overflow with Pythons up to 2.2
>>> int((100000000000000000 + 2)/3)
33333333333333332 # last digit should be 4

# wrong with Python 2.3 onwards; int overflow with earlier versions
>>> import math
>>> int(math.ceil(float(100000000000000000) / 3))
33333333333333332L
John Machin
-1, too verbose. Carpal tunnel is a very real danger for professional programmers.
par
@ John, thanks, this returns the correct result. Sorry about the dumb question. Is there a mathematical term for what I'm trying to achieve here? Something along the lines of "GCD/LCD", etc?
anonymous coward
Pigeonhole principle.
par
Division with rounding.
JoshD
@anonymous coward: term is "ceiling function" ...http://en.wikipedia.org/wiki/Floor_and_ceiling_functions
John Machin
+2  A: 
def f(X, Y, Z):
  d, r = divmod(Y * Z, X)
  return d + bool(r)
Dingo