- #1

- 6

- 0

For example, the function call,

BreakIntoSmallerNumbers(15);

would return the following array: [3, 2, 2, 2, 2, 2, 2]

BreakIntoSmallerNumbers(3); returns [1, 1, 1]

BreakIntoSmallerNumbers(); returns [11, 11, 11, 11, 11, 11, 10]

and so on.

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Java
- Thread starter jonesrussell
- Start date

- #1

- 6

- 0

For example, the function call,

BreakIntoSmallerNumbers(15);

would return the following array: [3, 2, 2, 2, 2, 2, 2]

BreakIntoSmallerNumbers(3); returns [1, 1, 1]

BreakIntoSmallerNumbers(); returns [11, 11, 11, 11, 11, 11, 10]

and so on.

- #2

- 724

- 0

If you want strictly seven numbers, then it will always follow the form:

{n / 7, ... 5 more times, n % 7}

(Of course, there are more ways to partition a number, but this is the pattern you use above.)

That can be generalized:

You want a set(array, or whatever) with "a" terms, that sums to "b":

{b / a, ... a - 1 more times, b % a}

(Again, more ways to partition, but this is the pattern you were using.)

Your question is loosely related to the concept of partitions from number theory. Here's the Wikipedia article if you wish to learn more about them. http://en.wikipedia.org/wiki/Partition_(number_theory [Broken])

{n / 7, ... 5 more times, n % 7}

(Of course, there are more ways to partition a number, but this is the pattern you use above.)

That can be generalized:

You want a set(array, or whatever) with "a" terms, that sums to "b":

{b / a, ... a - 1 more times, b % a}

(Again, more ways to partition, but this is the pattern you were using.)

Your question is loosely related to the concept of partitions from number theory. Here's the Wikipedia article if you wish to learn more about them. http://en.wikipedia.org/wiki/Partition_(number_theory [Broken])

Last edited by a moderator:

- #3

- 6

- 0

If you want strictly seven numbers, then it will always follow the form:

{n / 7, ... 5 more times, n % 7}

(Of course, there are more ways to partition a number, but this is the pattern you use above.)

That can be generalized:

You want a set(array, or whatever) with "a" terms, that sums to "b":

{b / a, ... a - 1 more times, b % a}

(Again, more ways to partition, but this is the pattern you were using.)

Your question is loosely related to the concept of partitions from number theory. Here's the Wikipedia article if you wish to learn more about them. http://en.wikipedia.org/wiki/Partition_(number_theory [Broken])

I'm attempting to write this as a function and am not getting my expected results:

Code:

```
var maxPoints = 18;
// partition points into an array set
var n = 7; // how many partitions
var pointSet = [];
for (var i=0; i<n; i++)
{
var partition = 0;
if (i < n-1) {
partition = Math.round(maxPoints / n);
} else {
if (pointSet.sum() < maxPoints)
partition = (maxPoints % n);
}
pointSet.push(partition);
}
console.log(pointSet);
```

This is resulting in:

Code:

`[3, 3, 3, 3, 3, 3, 0]`

which isn't what I was trying for, but adds up. However, with maxPoints = 20, it doesn't hold up at all, resulting in:

Code:

`[3, 3, 3, 3, 3, 3, 6]`

Any tips on where I'm going wrong?

Last edited by a moderator:

- #4

- 724

- 0

You have to do integer division. To do that, use JS's floor() method instead of round. round() rounds to the nearest, whereas floor() truncates.

- #5

- 6

- 0

Taking the same function, replacing round() with floor(),

maxPoints = 18 results in [2, 2, 2, 2, 2, 2, 4]

- #6

- 724

- 0

7 numbers that add to 18, isn't that what you wanted?

- #7

- 6

- 0

Yup, but [2, 2, 2, 2, 2, 2, 4] = 16

- #8

- 6

- 0

Ah!

partition = (maxPoints % n) + Math.floor(maxPoints / n);

partition = (maxPoints % n) + Math.floor(maxPoints / n);

- #9

- 724

- 0

I see what's causing it now. I said "a - 1 MORE times". Which means your "n-1" should be just "n." :)

Code:

```
var maxPoints = 18;
// partition points into an array set
var n = 7; // how many partitions
var pointSet = [];
for (var i=0; i<n; i++)
{
var partition = 0;
if (i < [b]n[/b]) {
partition = Math.round(maxPoints / n);
} else {
if (pointSet.sum() < maxPoints)
partition = (maxPoints % n);
}
pointSet.push(partition);
}
console.log(pointSet);
```

- #10

- 724

- 0

Wait... that's 8 numbers... I need to think before I post.

- #11

- 724

- 0

Yep, that'll do it.Ah!

partition = (maxPoints % n) + Math.floor(maxPoints / n);

- #12

- 6

- 0

Code:

```
var n = 7; // how many partitions
var pointSet = [];
for (var i=0; i<n; i++)
{
var partition = 0;
(i < n-1) ? partition = Math.floor(maxPoints / n) : partition = (maxPoints % n) + Math.floor(maxPoints / n);
pointSet.unshift(partition);
}
return pointSet;
```

This works for my purposes, and if I need to adjust I have a better understanding now.

Thanks!

Share: