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.
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It only takes a minute to sign up.
Sign up to join this communityI 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.
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.
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.