2
$\begingroup$

I want to construct a LOOP-computable program for the integer division (quotient):

x = a DIV b

The LOOP specification can be seen here: https://en.wikipedia.org/wiki/LOOP_(programming_language)

I don't know how to do this without a while.

$\endgroup$

closed as off-topic by David Richerby, Evil, xskxzr, Juho, hengxin Dec 22 '18 at 4:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – David Richerby, Evil, xskxzr, hengxin
If this question can be reworded to fit the rules in the help center, please edit the question.

1
$\begingroup$

The idea is to implement the following algorithm:

result = 0
while a >= 0:
  a = a - b
  result = result + 1
end while
return result - 1

As you mention, there is no while. However, since we have an a priori upper bound on the number of rounds, we can simulate the while as follows:

result = 0
repeat a + 1 times:
  if a >= 0:
    a = a - b
    result = result + 1
  end if
end repeat
return result - 1

It remains to implement the conditional and subtraction. In fact, you can't really implement the conditional, since all numbers are automatically non-negative. However, this is easy to fix by replacing the condition a >= 0 with the condition a > 0. I'll let you work out the minor changes required to implement this change.

$\endgroup$
1
$\begingroup$

Look for the unique integer $x$ such that $b x \le a$ and $b(x+1) > a$. With this approach, you don't need a WHILE statement.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.