Javascript - Reducing a number to smaller numbers

  • Context: Java 
  • Thread starter Thread starter jonesrussell
  • Start date Start date
  • Tags Tags
    Javascript Numbers
Click For Summary

Discussion Overview

The discussion revolves around the problem of partitioning a given number into seven smaller numbers, with participants exploring different approaches and coding implementations in JavaScript. The focus includes both theoretical aspects of number partitioning and practical coding challenges.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant seeks advice on creating a function to break a number into seven parts, providing examples of expected outputs.
  • Another participant suggests a formula for partitioning a number into a specified number of parts, relating it to number theory concepts.
  • Participants discuss the implementation of the partitioning logic in JavaScript, with one sharing their initial code and the unexpected results it produces.
  • There is a suggestion to use the floor() method for integer division instead of round(), with some participants testing this change and reporting different outcomes.
  • Discrepancies in the results lead to further clarification on the logic of partitioning, with one participant realizing a mistake in their understanding of the number of partitions needed.
  • Participants refine their code based on feedback and corrections, leading to a working solution that meets the original requirements.

Areas of Agreement / Disagreement

Participants generally agree on the need for integer division and the correct approach to partitioning the number, but there are multiple iterations and corrections of the code, indicating some unresolved issues in the implementation details.

Contextual Notes

Some participants express uncertainty about the correctness of their code and the expected results, highlighting the need for careful attention to the logic of partitioning and the handling of edge cases.

Who May Find This Useful

Readers interested in JavaScript programming, number theory, or those facing similar coding challenges in partitioning numbers may find this discussion beneficial.

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 ·
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 10 ·
Replies
10
Views
1K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K