Javascript - Reducing a number to smaller numbers

In summary, if you want strictly seven numbers, then it will always follow the form: {n / 7, ... 5 more times, n % 7}
  • #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.
 
Technology news on Phys.org
  • #2
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:
  • #3
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:
  • #4
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.
 
  • #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
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
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!
 

1. How can I reduce a number to a smaller number in Javascript?

In Javascript, you can use the Math.floor() function to round a number down to the nearest integer. You can also use the toFixed() function to round a number to a specified number of decimal places.

2. Can I use a loop to repeatedly reduce a number in Javascript?

Yes, you can use a loop, such as a for loop or a while loop, to repeatedly apply a reduction function to a number until it reaches a desired value. This can be useful for tasks such as converting a large number into a more manageable format.

3. How can I convert a number to a percentage in Javascript?

To convert a number to a percentage in Javascript, you can use the toFixed() function to round the number to the desired number of decimal places, and then multiply it by 100 to get the percentage value. For example, var percentage = (0.5).toFixed(2) * 100; will convert 0.5 to 50%.

4. Is there a way to reduce a number to a specific range in Javascript?

Yes, you can use the Math.min() and Math.max() functions to limit a number to a specific range. For example, var num = Math.min(Math.max(5, 10), 20); will set num to 10, as it is the closest value to 5 that is within the range of 10 and 20.

5. Can I use the modulus operator to reduce a number in Javascript?

Yes, you can use the modulus (%) operator to get the remainder after dividing a number by another number. This can be useful for tasks such as checking if a number is even or odd, or for reducing a number to a specific range. For example, var num = 7 % 3; will set num to 1, as 1 is the remainder when 7 is divided by 3.

Similar threads

  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
10
Views
728
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
4
Views
744
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
769
  • Programming and Computer Science
Replies
1
Views
672
  • Programming and Computer Science
Replies
2
Views
875
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top