Javascript - Reducing a number to smaller numbers

  • #1
jonesrussell
6
0
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
TylerH
729
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
jonesrussell
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
TylerH
729
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
jonesrussell
6
0
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
TylerH
729
0
7 numbers that add to 18, isn't that what you wanted?
 
  • #7
jonesrussell
6
0
Yup, but [2, 2, 2, 2, 2, 2, 4] = 16
 
  • #8
jonesrussell
6
0
Ah!

partition = (maxPoints % n) + Math.floor(maxPoints / n);
 
  • #9
TylerH
729
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
TylerH
729
0
Wait... that's 8 numbers... I need to think before I post.
 
  • #11
TylerH
729
0
Ah!

partition = (maxPoints % n) + Math.floor(maxPoints / n);
Yep, that'll do it.
 
  • #12
jonesrussell
6
0
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!
 

Suggested for: Javascript - Reducing a number to smaller numbers

Replies
5
Views
242
Replies
10
Views
530
  • Last Post
Replies
6
Views
449
  • Last Post
6
Replies
187
Views
5K
Replies
0
Views
331
Replies
10
Views
330
Top