Marijus
Marijus

Reputation: 4375

Dividing a number into 12 integers equally

I want to divide an integer N, which is a number of work days to 12 months as equally as possible so if I take a any period of 2,3,4,5,6 months the work days are still divided as equally as possible in that period. Lecturer said that I have to use some kind of rounding up algorithm, however I can't think of anything. Any algorithm suggestions or links would help a lot.

Upvotes: 0

Views: 1406

Answers (2)

sampson-chen
sampson-chen

Reputation: 47317

You start with how many days per months you need to work AT LEAST:

days = floor(N / 12) 

(where floor(...) means to round down.)

And then you have some remainder:

remainder = N % 12 

(where % means modulo)

Notice that remainder is definitely less than 12, because we used modulo

So spread these remainder days across the 12 months however you like.

(Note this approach generalizes to any number of months, just substitute in the # of months wherever we used 12 above)

Upvotes: 1

Steve Jessop
Steve Jessop

Reputation: 279335

You need to choose a number k so that some of the months have k work-days put in them, and the others have k+1. That's as close as you can get to an equal division without splitting work-days. The number of months with k+1 in them is equal to N modulo 12 (N % 12 in many programming languages).

Upvotes: 2

Related Questions