Egyptian division: Difference between revisions

(add OCaml)
Line 1,784:
{{Out}}
<pre>[17,2]</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
<syntaxhighlight lang=jq>
def egyptianDivide($dividend; $divisor):
if ($dividend < 0 or $divisor <= 0) then "egyptianDivide: invalid argument(s)" | error
elif ($dividend < $divisor) then [0, $dividend]
else
{ powersOfTwo: [1],
doublings: [$divisor],
doubling: (2 * $divisor)
}
| until(.doubling > $dividend;
.powersOfTwo += [.powersOfTwo[-1]*2]
| .doublings += [.doubling]
| .doubling *= 2 )
| .answer = 0
| .accumulator = 0
| .i = (.doublings|length)-1
| until( .i < 0 or .accumulator == $dividend;
if (.accumulator + .doublings[.i] <= $dividend)
then .accumulator += .doublings[.i]
| .answer += .powersOfTwo[.i]
else .
end
| .i += -1)
| [.answer, $dividend - .accumulator]
end;
 
def task($dividend; $divisor):
egyptianDivide($dividend; $divisor)
| "\($dividend) ÷ \($divisor) = \(.[0]) with remainder \(.[1]).";
 
task(580; 34)
</syntaxhighlight>
{{output}}
<pre>
580 ÷ 34 = 17 with remainder 2.
</pre>
 
=={{header|Julia}}==
2,455

edits