Javascript - Reducing a number to smaller numbers

  • #1

Main Question or Discussion Point

I am looking for advice on how to approach a problem. I would like to take a given number and break it into 7 parts.

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.
 

Answers and Replies

  • #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])
 
Last edited by a moderator:
  • #3
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
Sorry it took so long to answer. I had some hw due.

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
I was actually doing my initial testing with floor() but I don't get the expected results either.

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
Yup, but [2, 2, 2, 2, 2, 2, 4] = 16
 
  • #8
Ah!

partition = (maxPoints % n) + Math.floor(maxPoints / n);
 
  • #9
724
0
OH! Crap. :redface:

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
Ah!

partition = (maxPoints % n) + Math.floor(maxPoints / n);
Yep, that'll do it.
 
  • #12
This is what I've ended up with,

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!
 

Related Threads on Javascript - Reducing a number to smaller numbers

Replies
4
Views
1K
Replies
5
Views
868
  • Last Post
Replies
1
Views
1K
Replies
11
Views
1K
  • Last Post
Replies
3
Views
3K
Replies
21
Views
3K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
4
Views
3K
  • Last Post
Replies
1
Views
1K
Top