0

I want to split a number into $n$ parts and then take another number (which is less than or equal to the first number) and see which of the fractions the other number belongs to.

For example, split $100$ into $4$ parts. Which part does $36$ belong to? The second part, $26-50$.

For the record, I'm going to be using this in JavaScript so pseudo-code would be great but not required.

  • @mapierce271 I haven't tried anything, other than just coming to realization that this is the solution that would work for me. I'm not sure how to approach the solution. – Spencer Carnage Dec 23 '14 at 23:37

2 Answers2

0

Perhaps something like this?

function returnNthPart(value,max,nparts){
    return Math.ceil(nparts*value/max);
}
flawr
  • 16,533
  • 5
  • 41
  • 66
0

There is an $\mathcal{O}(1)$ solution to this problem, if $n$ signifies the section that it belongs to, and $N$ is the initial number

$$n=\left\lceil\frac{N \cdot k}{q}\right\rceil,$$

Where $k$ is the initial number (in your case $100$) and $q$ is the number of parts. In JavaScript this would be something like:

function divisionSection( value, initialNumber, numParts ) {
      return Math.ceil( value * initialNumber / numParts ); 
}
Thomas Russell
  • 10,425
  • 5
  • 38
  • 66