0

inspired by these posts: stackoverflow post 1, stackoverflow post 2, geeksforgeeks post

I wanted to write an algorithm in R to divide two integers, giving the integer quotient and remainder without dividing or multiplying.

However, I am struggling to translate the code to R. Here is what I got so far:

Division_alternative <- function(dividend, divisor) {
      # Calculate sign of divisor 
      if (dividend < 0 | divisor < 0) {
        sign <- -1
      } else {
        sign <- 1
      }
    # Transform to positive 
    dividend = abs(dividend) 
    divisor = abs(divisor) 
    # Initialize the quotient 
    quotient = 0
    while (dividend >= divisor) {  
      print(sign*quotient)
      dividend - divisor 
      quotient + 1 }
    } 

 a = 25
 b = 4
 print(Division_alternative(a, b))

I am not sure what is wrong with the code thus far, that it wouldn't return anything. Anyone a clue?

Sarah
  • 137
  • 9
  • 2
    You are not doing any assignment in your `while` loop – Jrakru56 Nov 15 '18 at 20:25
  • 2
    What exactly are you expecting it to return? `while` loops don't return anything and you don't have any values after that tor a `return()` call to actually return. – MrFlick Nov 15 '18 at 20:28

1 Answers1

1

Using proper assignment and making our function return something, we get:

 Division_alternative <- function(dividend, divisor) {
 ##Handle only positive cases

  stopifnot((dividend > 0 && divisor >0)) 

  quotient = 0
  while (dividend >= divisor) {  
    # print(sign*quotient)
    dividend <- dividend - divisor 
    quotient <- quotient + 1 }

  return(list(dividend, quotient))

} 

a = 25
b = 4
print(Division_alternative(a, b))

I am only handling the positive cases since it is the easiest case. I'll let you figure the logic on how to make it work in the other 3 cases since that's a) the fun in doing those things, b) I am not a CS major and never implemented a modulus and remainder function from scratch.

Jrakru56
  • 1,211
  • 9
  • 16
  • That's almost correct, except for one bug: the sign-derivation is just as wrong as the one used in the question. You need xor instead of a simple or. Also please add an explanation of what you changed to make the code work. –  Nov 15 '18 at 20:30
  • @Paul, I just fixed the syntax mistake. – Jrakru56 Nov 15 '18 at 20:41
  • please also add an explanation of what you've done to [improve the answer](https://stackoverflow.com/help/how-to-answer). Also using a simple xor to determine the sign would be a major improvement compared to simply terminating the function if any parameter is negative. –  Nov 15 '18 at 20:52
  • @Paul, can you show me how a `xor` would make it work? I am probably missing something but just changing the sign will not be enough to a correct answer. – Jrakru56 Nov 15 '18 at 21:24
  • 1
    if(xor(dividend < 0, divisor < 0)) { # sign is -1 ... should do the trick. I've written my own complete version which can be found [here](http://tpcg.io/ZJvU0e). Feel free to include whatever you find usefull into your answer. –  Nov 16 '18 at 12:23