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?