Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a total, determine how many times a value will go into it

Tags:

c#

math

The Problem:

A box can hold 53 items. If a person has 56 items, it will require 2 boxes to hold them. Box 1 will hold 53 items and box 2 will hold 3.

How do I repeat the above where 53 is a constant, unchanging, value and 56 is a variable for each box:

Math.Ceiling(Convert.ToDecimal(intFeet / 53))

what I have so far for that is:

int TotalItems = 56; 
int Boxes = Math.Ceiling(Convert.ToDecimal(intFeet / 53));  

for (int i = 0; i < Boxes; i++)
{  
    int itemsincurrentbox=??  
}  
like image 689
KellCOMnet Avatar asked Jul 27 '09 23:07

KellCOMnet


People also ask

How many times does a number go into another Python?

The Python count() function works out how many times a value appears in a list or a string. It returns the number of times the value appears as an integer.

How do you count how many times a number goes into another in Java?

out. println(firstNumber + " goes into " + secondNumber + " " + total + " times. The remainder is " + remainder + "."); In java int\int=int , i.e. your total field already gives you the number of whole times.

How many times a number can be divided by 2 Python?

An integer n can be divided by 2: floor(log(n)/log(2)) times.


2 Answers

Where the integers capacity and numItems are your box capacity (53 in the example) and the total number of items you have, use the following two calculations:

int numBoxes = numItems / capacity;
int remainder = numItems % capacity;

This will give you the number of boxes that are filled (numBoxes), and the number of items in an additional box (remainder) if any are needed, since this value could be 0.

Edit: As Luke pointed out in the comments, you can get the same result with the .NET class library function Math.DivRem.

int remainder;
int numBoxes = Math.DivRem( numItems, capacity, out remainder );

This function returns the quotient and puts the remainder in an output parameter.

like image 120
Bill the Lizard Avatar answered Oct 22 '22 14:10

Bill the Lizard


simple, overly imperative example:

int itemsPerBox = 53;
int totalItems = 56;
int remainder = 0;
int boxes = Math.DivRem(totalItems, itemsPerBox, out remainder);
for(int i = 0; i <= boxes; i++){
    int itemsincurrentbox = i == boxes ? remainder : itemsPerBox;
}
like image 36
Luke Schafer Avatar answered Oct 22 '22 12:10

Luke Schafer