2

for context here is my problem:

I have a table of parts. A part is produced from a set of materials, let's call the constant materials variable z. So this table is a table of parts all produced from a set of materials. Each row in this table is a new part. There is also a column indicating the amount of parts that will be produced from 1 set of materials.

So if I have:

var z = 1 //just some constant

var x = 5 //the amount of parts produced from a single set of materials

var y = 23 //the total amount of parts that have to be made 

I know the amount of parts produced from a set of materials, and I know the amount of parts that need to be produced. I can never produce less parts than are needed, so I know that 4 sets of materials will produce me 20 parts and I would still be 3 short. If i use 5 sets of materials I can produce 25 parts with a remainder of 2 parts.

My issue is that I tried to solve this using mod, but I'm making a mistake somewhere

//Fun
    const fun = async () => {
        try {

            
            let x = 23;
            let y = 5;
            const result = x/y

            if(x%y == 0) {
                console.log(x, ' divided by', y, ' has remainder of: ', x%y);
                console.log(y, ' divided by', x, ' has remainder of: ', y%x);

            }
            else {
                console.log(x, ' divided by', y, ' has remainder of: ', x%y); 
                console.log(y, ' divided by', x, ' has remainder of: ', y%x);
            }
 
        } catch (err) {
            console.log(err.message);
        }
    }

So to further add clarity I would always want to find the maximum amount of times a number can be divided by something, and if it has a remainder then record the remainder. Would a possible solution be to distinguish between positive or negative remainders? Any help is appreciated, thank you !!!

test3r123
  • 37
  • 2
  • 9
  • What is your expected result? – Kinglish Mar 24 '22 at 18:38
  • @Kinglish so If I want to divide 23 by 5, I would want to return 25 with a remainder of 2. If I want to divide 33 by 2, I would want to return 34 with a remainder of 1. Essentially I would either want a remainder of 0, or the amount of times to go over the number with a remainder – test3r123 Mar 24 '22 at 18:43
  • Max: `Math.ceil(A/B)`, Remainder: `A%B` – Chase Jones Mar 24 '22 at 20:13

3 Answers3

4

Math.floor( A/B ), where A is the desired number and B is the number of pieces in a set, will give you the number of divisions before remainder (since a division is just the number of times B can be subtracted from A, then we use Math.floor to round down), (A%B) will give you the remainder thereafter.

Chase Jones
  • 121
  • 6
  • 1
    You gave me an idea to try Math.ceil(), will update the OP if things workout – test3r123 Mar 24 '22 at 18:41
  • Awesome, Math.ceil will effectively give you the max number of sets needed to complete the number of parts required. So doing `Math.ceil(A/B) - A%B` will give you what is left over after completing all the required parts – Chase Jones Mar 24 '22 at 18:45
1

If you want to see how many times X can be divided by Y, you do

yz =x

z*log(y) = log(x)

z = log(x)/log(y)

and from here you either floor(z) or ceil(z) depending on your problem.

romanu 3
  • 26
  • 3
  • 1
    That's not quite right. plugging 25 in for y and 5 in for x should yield 5, as 5 goes into 25 five times, but this equation yields 2. Here you're getting how many times must y be multiplied by itself to equal x. – Chase Jones Mar 24 '22 at 20:30
1

This is probably not what you're looking for, but it's a shorthand way of getting to where you're going.

const fun = (x, y) => {
  let r = x % y; // store our modulus here
  return !r ? [x, 0] : [(y - r + x), y - r];
  // if there is no remainder, return the dividend and zero: x, 0
  // otherwise, the next whole number result is found by
  // subtracting the modulus from the divisor and adding the dividend: (y - r + x)
  // and the difference between the new divisor and the old divisor is the divisor minus the modulus: (y - r)
}

I have it returning in an array but you can easily convert that into your string format with join(',')

const fun = (x, y) => {
  let r = x % y;
  return !r ? [x, 0] : [(y - r + x), y - r];
}
console.log(fun(23, 5))
console.log(fun(33, 2))
Kinglish
  • 23,358
  • 3
  • 22
  • 43