-1

Through applying a computer science algorithm in JavaScript, could you solve the following question?

The function accepts a numerator and denominator in the form of a positive or negative integer value.

You can not use '*', '/' or '%'.

function divide(num, denom) {
  // ...
}

divide(6, 2)
divide(-10, 5)
divide(7, 2)
divide(-5, 10)

I am curious to know if there is a known computer science algorithm out there that can solve this.

  • For numbers that evenly divide others, it's easy, just repeatedly add (or subtract) the divisor from the dividend. For numbers that don't evenly divide others, I guess you could manually program in long division (falling back on functions that recursively add or subtract instead of multiplying and dividing) – CertainPerformance Aug 03 '18 at 21:44
  • check [The approach implement Divide](https://www.geeksforgeeks.org/divide-two-integers-without-using-multiplication-division-mod-operator/); **Approach** : Keep subtracting the divisor from dividend until dividend becomes less than divisor. The dividend becomes the remainder, and the number of times subtraction is done becomes the quotient. **Efficient Approach** : Use bit manipulation in order to find the quotient. The divisor and dividend can be written as `dividend = quotient * divisor + remainder` – Sphinx Aug 03 '18 at 21:47
  • The simplest algorithm is [binary long division](https://en.wikipedia.org/wiki/Long_division#Non-decimal_radix), in which, as Wikipedia notes, each step consists only of a comparison and possibly a subtraction. – rici Aug 04 '18 at 05:21
  • on floating point you can approximate: [Floating Point Divider Hardware Implementation Details](https://stackoverflow.com/a/18398246/2521214) – Spektre Aug 04 '18 at 06:43

2 Answers2

1

I saw this question and had to take a crack at it, I've implemented a solution that can get decimal places up to a specified precision as well as handle negatives, it uses a recursive approach and some string manipulation.

// Calculate up to decimal point
const MAX_PRECISION = 6;

function divide(numerator, denominator, answer=0, decimals=MAX_PRECISION) 
{
  // Account for negative numbers
  if ((numerator < 0 || denominator < 0))
  {
     // If both are negative, then we return a positive, otherwise return a negative
     if (numerator < 0 && denominator < 0)
          return  divide(Math.abs(numerator), Math.abs(denominator))
     else return -divide(Math.abs(numerator), Math.abs(denominator));
  }

  // Base case return if evenly divisble or we've reached the specificed percision
  if (numerator == 0 || decimals == 0) return answer;

  // Calculate the decimal places
  if (numerator < denominator)
  {
    // Move the decinal place to the right
    const timesTen = parseInt(numerator + "0");

    // Calcualte decimal places up to the certain percision
    if (decimals == MAX_PRECISION)
         return parseFloat(answer + "." + divide(timesTen, denominator, 0, decimals-1));
    else return answer + "" + divide(timesTen, denominator, 0, decimals-1);
  }

  // Perform the calculations in a tail-recursive manor
  return divide(numerator-denominator, denominator, answer+1, decimals);
}

// Test Cases
console.log(divide(10,  2));
console.log(divide(10, -2));
console.log(divide(-7, -4));
console.log(divide( 1, -2));
console.log(divide(11,  3));
console.log(divide(22,  7));
Asleepace
  • 3,466
  • 2
  • 23
  • 36
0

You can try Something like:

function divide(num, denom) {
    var count = 0;
    while (num > 0) {
        num = num - denom
        if (num >= 0) {
            count ++;
        }
    }
    return count;
}
Shamseer
  • 682
  • 1
  • 11
  • 24