Java Javascript - Reducing a number to smaller numbers

  • Thread starter Thread starter jonesrussell
  • Start date Start date
  • Tags Tags
    Javascript Numbers
AI Thread Summary
The discussion centers around creating a function to partition a given number into seven parts, ensuring the sum matches the original number. The proposed function, BreakIntoSmallerNumbers, illustrates how to divide a number into equal parts, with the last part accounting for any remainder. The general formula for partitioning is outlined as dividing the total by the number of parts, with adjustments for the remainder. A coding example reveals issues with the initial implementation, particularly regarding the use of rounding versus flooring methods for division, which affects the accuracy of the partitions. The conversation highlights the importance of integer division and correcting the logic to ensure the correct number of partitions is generated. Ultimately, the final code successfully partitions the number as intended, demonstrating a clearer understanding of the problem and solution.
jonesrussell
Messages
6
Reaction score
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.
 
Technology news on Phys.org
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 )
 
Last edited by a moderator:
TylerH said:
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 )

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:
Sorry it took so long to answer. I had some homework 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.
 
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]
 
7 numbers that add to 18, isn't that what you wanted?
 
Yup, but [2, 2, 2, 2, 2, 2, 4] = 16
 
Ah!

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

Similar threads

Replies
12
Views
2K
Replies
9
Views
1K
Replies
10
Views
1K
Replies
4
Views
1K
Replies
10
Views
3K
Replies
2
Views
2K
Replies
2
Views
1K
Back
Top