Egyptian division: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Tailspin}}: update to stricter typing) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 319:
{{trans|Python}}
<
assert(divisor != 0)
V (pwrs, dbls) = ([1], [divisor])
Line 336:
V (i, j) = (580, 34)
V (d, m) = egyptian_divmod(i, j)
print(‘#. divided by #. using the Egyption method is #. remainder #.’.format(i, j, d, m))</
{{out}}
Line 344:
=={{header|Action!}}==
<
PROC EgyptianDivision(CARD dividend,divisor Answer POINTER res)
Line 382:
EgyptianDivision(dividend,divisor,res)
PrintF("%U / %U = %U reminder %U",dividend,divisor,res.result,res.reminder)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Egyptian_division.png Screenshot from Atari 8-bit computer]
Line 391:
=={{header|Ada}}==
<syntaxhighlight lang="ada">
with Ada.Text_IO;
Line 422:
Ada.Text_IO.put_line ("Quotient="&q'Img & " Remainder="&r'img);
end Egyptian_Division;
</syntaxhighlight>
{{Out}}
<pre>Quotient= 17 Remainder= 2</pre>
=={{header|ALGOL 68}}==
<
# performs Egyptian division of dividend by divisor, setting quotient and remainder #
# this uses 32 bit numbers, so a table of 32 powers of 2 should be sufficient #
Line 464:
egyptian division( 580, 34, quotient, remainder );
print( ( "580 divided by 34 is: ", whole( quotient, 0 ), " remainder: ", whole( remainder, 0 ), newline ) )
END</
{{out}}
<pre>
Line 473:
Unfold to derive successively doubled rows, fold to sum quotient and derive remainder
<
-- eqyptianQuotRem :: Int -> Int -> (Int, Int)
Line 565:
end tell
return xs
end unfoldr</
{{Out}}
<pre>{17, 2}</pre>
Line 571:
=={{header|Arturo}}==
<
ensure -> and? dividend >= 0
divisor > 0
Line 607:
[quotient, remainder]: egyptianDiv dividend divisor
print [dividend "divided by" divisor "is" quotient "with remainder" remainder]</
{{out}}
Line 614:
=={{header|AutoHotkey}}==
<
divisor := 34
Line 635:
obj.pop() ; remove current row
}
MsgBox % divident "/" divisor " = " answer ( divident-accumulator > 0 ? " r" divident-accumulator : "")</
Outputs:<pre>580/34 = 17 r2</pre>
=={{header|BaCon}}==
<syntaxhighlight lang="c">
'---Ported from the c code example to BaCon by bigbass
Line 705:
</syntaxhighlight>
=={{header|C}}==
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
Line 757:
go(580, 32);
}
</syntaxhighlight>
=={{header|C sharp|C#}}==
<
using System;
using System.Collections;
Line 914:
}
}
</syntaxhighlight>
{{out| Program Input and Output : Instead of bold and strikeout text format, numbers are represented in different color}}
<pre>
Line 950:
=={{header|C++}}==
{{trans|C}}
<
#include <iostream>
Line 1,007:
return 0;
}</
{{out}}
<pre>580 / 34 = 17 remainder 2</pre>
Line 1,013:
=={{header|Common Lisp}}==
<
(defun egyptian-division (dividend divisor)
(let* ((doublings (reverse (loop for n = divisor then (* 2 n)
Line 1,030:
do (incf answer p)
(incf accumulator d))))
</syntaxhighlight>
{{out}}
Line 1,040:
=={{header|D}}==
<syntaxhighlight lang="d">
import std.stdio;
Line 1,099:
assert(remainder == 2);
}
</syntaxhighlight>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
Line 1,105:
{{libheader| System.Console}}Thanks for JensBorrisholt [https://github.com/JensBorrisholt/DelphiConsole].
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Egyptian_division;
Line 1,254:
Console.Write('Press any key to continue . . . ');
Console.ReadKey(true);
end.</
{{out}}<pre>
Line 1,285:
Press any key to continue . . .</pre>
=={{header|Erlang}}==
<
-export([ediv/2]).
Line 1,301:
accumulate(N, [T|Ts], [D|Ds], Q, C) when (C + D) =< N -> accumulate(N, Ts, Ds, Q+T, C+D);
accumulate(N, [_|Ts], [_|Ds], Q, C) -> accumulate(N, Ts, Ds, Q, C).
</syntaxhighlight>
{{out}}
<pre>
Line 1,309:
=={{header|F_Sharp|F#}}==
<
let egyptianDivision N G =
let rec fn n g = seq{yield (n,g); yield! fn (n+n) (g+g)}
Seq.foldBack (fun (n,i) (g,e)->if (i<=g) then ((g-i),(e+n)) else (g,e)) (fn 1 G |> Seq.takeWhile(fun (_,g)->g<=N)) (N,0)
</syntaxhighlight>
Which may be used:
<
let (n,g) = egyptianDivision 580 34
printfn "580 divided by 34 is %d remainder %d" g n
</syntaxhighlight>
{{out}}
<pre>
Line 1,326:
=={{header|Factor}}==
{{works with|Factor|0.98}}
<
IN: rosetta-code.egyptian-division
Line 1,344:
} 2cleave ;
580 34 ediv "580 divided by 34 is %d remainder %d\n" printf</
{{out}}
<pre>
Line 1,351:
=={{header|Forth}}==
<syntaxhighlight lang="forth">
variable tab-end
Line 1,375:
: .egypt ( m n -- )
cr 2dup swap . ." divided by " . ." is " e/mod swap . ." remainder " . ;
</syntaxhighlight>
{{Out}}
<pre>
Line 1,383:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,421:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>580 divided by 34 using Egytian division returns 17 mod(ulus) 2</pre>
Line 1,427:
=={{header|Go}}==
{{trans|Kotlin}}
<
import "fmt"
Line 1,469:
quotient, remainder := egyptianDivide(dividend, divisor)
fmt.Println(dividend, "divided by", divisor, "is", quotient, "with remainder", remainder)
}</
{{out}}
Line 1,478:
=={{header|Groovy}}==
{{trans|Java}}
<
/**
* Runs the method and divides 580 by 34
Line 1,521:
println(String.format("%d, remainder %d", answer, dividend - accumulator))
}
}</
{{out}}
<pre>17, remainder 2</pre>
Line 1,527:
=={{header|Haskell}}==
Deriving division from (+) and (-) by unfolding from a seed pair (1, divisor) up to a series of successively doubling pairs, and then refolding that series of 'two column rows' back down to a (quotient, remainder) pair, using (0, dividend) as the initial accumulator value. In other words, taking the divisor as a unit, and deriving the binary composition of the dividend in terms of that unit.
<
egyptianQuotRem :: Integer -> Integer -> (Integer, Integer)
Line 1,540:
main :: IO ()
main = print $ egyptianQuotRem 580 34</
{{Out}}
<pre>(17,2)</pre>
Line 1,546:
We can make the process of calculation more visible by adding a trace layer:
<
import Debug.Trace (trace)
Line 1,577:
main :: IO ()
main = print $ egyptianQuotRem 580 34</
{{Out}}
<pre>Number pair unfolded to series of doubling rows:
Line 1,591:
Another approach, using lazy lists and foldr:
<
powers = doublings 1
Line 1,602:
main :: IO ()
main = print $ egy 580 34</
{{Out}}
<pre>17</pre>
Line 1,610:
Implementation:
<
ansacc=: 1 }. (] + [ * {.@[ >: {:@:+)/@([,.doublings)
egydiv=: (0,[)+1 _1*ansacc</
Task example:
<
1 34
2 68
Line 1,625:
17 578
580 egydiv 34
17 2</
Notes:
Line 1,634:
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
Line 1,687:
}
</syntaxhighlight>
{{Out}}
<pre>17, remainder 2</pre>
Line 1,693:
=={{header|JavaScript}}==
===ES6===
<
'use strict';
Line 1,781:
// MAIN ---
return main();
})();</
{{Out}}
<pre>[17,2]</pre>
Line 1,787:
=={{header|Julia}}==
{{works with|Julia|0.6}}
<
N = 64
powers = Vector{Int}(N)
Line 1,819:
@test egyptiandivision(x, y) == divrem(x, y)
end
end</
{{out}}
Line 1,827:
=={{header|Kotlin}}==
<
data class DivMod(val quotient: Int, val remainder: Int)
Line 1,860:
val (quotient, remainder) = egyptianDivide(dividend, divisor)
println("$dividend divided by $divisor is $quotient with remainder $remainder")
}</
{{out}}
Line 1,869:
=={{header|Lua}}==
{{trans|Python}}
<
local pwrs, dbls = {1}, {divisor}
while dbls[#dbls] <= dividend do
Line 1,889:
local i, j = 580, 34
local d, m = egyptian_divmod(i, j)
print(i.." divided by "..j.." using the Egyptian method is "..d.." remainder "..m)</
{{out}}
<pre>580 divided by 34 using the Egyptian method is 17 remainder 2</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
EgyptianDivide[dividend_, divisor_] := Module[{table, i, answer, accumulator},
table = {{1, divisor}};
Line 1,915:
{answer, dividend - accumulator}
]
EgyptianDivide[580, 34]</
{{out}}
<pre>{17, 2}</pre>
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
Line 1,962:
ReadChar
END EgyptianDivision.</
=={{header|Nim}}==
<
func egyptianDivision(dividend, divisor: int): tuple[quotient, remainder: int] =
Line 1,995:
let divisor = 34
var (quotient, remainder) = egyptianDivision(dividend, divisor)
echo fmt"{dividend} divided by {divisor} is {quotient} with remainder {remainder}"</
{{out}}
<pre>
Line 2,003:
=={{header|Perl}}==
{{trans|Raku}}
<
my($dividend, $divisor) = @_;
die "Invalid divisor" if $divisor <= 0;
Line 2,022:
my($n,$d) = @$_;
printf "Egyption divmod %s %% %s = %s remainder %s\n", $n, $d, egyptian_divmod( $n, $d )
}</
{{out}}
<pre>Egyption divmod 580 % 34 = 17 remainder 2
Line 2,029:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">egyptian_division</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">dividend</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">divisor</span><span style="color: #0000FF;">)</span>
Line 2,051:
<span style="color: #000000;">egyptian_division</span><span style="color: #0000FF;">(</span><span style="color: #000000;">580</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 2,058:
=={{header|PicoLisp}}==
<
(de divmod (Dend Disor)
Line 2,085:
(let (A (rand 1 1000) B (rand 1 A))
(test (divmod A B) (egyptian A B)) ) )
(println (egyptian 580 34))</
{{out}}
Line 2,092:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
powers2_multiples(Dividend, [1], Powers, [Divisor], Multiples),
accumulate(Dividend, Powers, Multiples, 0, Quotient, 0, Acc),
Line 2,123:
main:-
test_egyptian_divide(580, 34).</
{{out}}
Line 2,132:
=={{header|Python}}==
===Idiomatic=== <!-- When compared to a Haskell translation -->
<
def egyptian_divmod(dividend, divisor):
Line 2,154:
i, j = 580, 34
print(f'{i} divided by {j} using the Egyption method is %i remainder %i'
% egyptian_divmod(i, j))</
'''Sample output'''
Line 2,168:
{{Trans|Haskell}}
<
from functools import reduce
Line 2,251:
# MAIN ----------------------------------------------------
if __name__ == '__main__':
main()</
{{Out}}
<pre>(17, 2)</pre>
Line 2,257:
=={{header|Quackery}}==
<
[ $ "Cannot divide by zero."
fail ]
Line 2,296:
say "." ] is task ( n n --> )
580 34 task</
{{out}}
Line 2,304:
=={{header|R}}==
<
pow2 = 0
row = 1
Line 2,340:
Egyptian_division(580, 34)
Egyptian_division(300, 2)
</syntaxhighlight>
{{out}}
Line 2,349:
=={{header|Racket}}==
<
(define (quotient/remainder-egyptian dividend divisor (trace? #f))
Line 2,389:
(module+ main
(quotient/remainder-egyptian 580 34 #t))</
{{out}}
Line 2,413:
===Normal version===
Only works with positive real numbers, not negative or complex.
<syntaxhighlight lang="raku"
my $accumulator = 0;
([1, $divisor], { [.[0] + .[0], .[1] + .[1]] } … ^ *.[1] > $dividend)
Line 2,424:
printf "%s divmod %s = %s remainder %s\n",
$n, $d, |egyptian-divmod( $n, $d )
}</
{{out}}
<pre>580 divmod 34 = 17 remainder 2
Line 2,436:
This is intended to be humorous and should not be regarded as good (or even sane) programming practice. That being said, 𓂽 & 𓂻 really are the ancient Egyptian symbols for addition and subtraction, and the Egyptian number notation is as accurate as possible. Everything else owes more to whimsy than rigor.
<syntaxhighlight lang="raku"
sub infix:<𓂽> { $^𓃠 + $^𓃟 }
sub infix:<𓂻> { $^𓃲 - $^𓆊 }
Line 2,458:
printf "%s divmod %s = %s remainder %s =OR= %s 𓅓 %s = %s remainder %s\n",
𓃾, 𓆙, |(𓃾 𓅓 𓆙), (𓃾, 𓆙, |(𓃾 𓅓 𓆙))».&𓁶;
}</
{{out}}
Line 2,467:
=={{header|REXX}}==
Only addition and subtraction is used in this version of the Egyptian division method.
<
numeric digits 1000 /*support gihugic numbers & be gung-ho.*/
parse arg n d . /*obtain optional arguments from the CL*/
Line 2,490:
ans= ans + pow.s /*calculate the (newer) running answer.*/
end /*s*/
return ans num-acc /*return the answer and the remainder. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,501:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 2,531:
see string(dividend) + " divided by " + string(divisor) + " using egytian division" + nl
see " returns " + string(answer) + " mod(ulus) " + string(dividend-accumulator)
</syntaxhighlight>
Output:
<pre>
Line 2,539:
=={{header|Ruby}}==
<
table = [[1, divisor]]
table << table.last.map{|e| e*2} while table.last.first * 2 <= dividend
Line 2,553:
puts "Quotient = %s Remainder = %s" % egyptian_divmod(580, 34)
</syntaxhighlight>
{{out}}
<pre>Quotient = 17 Remainder = 2
Line 2,559:
=={{header|Rust}}==
<
let dividend = dividend as u64;
let divisor = divisor as u64;
Line 2,584:
let (div, rem) = egyptian_divide(580, 34);
println!("580 divided by 34 is {} remainder {}", div, rem);
}</
{{out}}
<pre>580 divided by 34 is 17 remainder 2</pre>
Line 2,590:
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/sYSdo9u/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/3yry7OurSQS72xNMK0GMEg Scastie (remote JVM)].
<
private def divide(dividend: Int, divisor: Int): Unit = {
Line 2,618:
divide(580, 34)
}</
=={{header|Sidef}}==
{{trans|Ruby}}
<
var table = [[1, divisor]]
table << table[-1].map{|e| 2*e } while (2*table[-1][0] <= dividend)
Line 2,636:
}
say ("Quotient = %s Remainder = %s" % egyptian_divmod(580, 34))</
{{out}}
<pre>
Line 2,644:
=={{header|Swift}}==
<
@inlinable
public func egyptianDivide(by divisor: Self) -> (quo: Self, rem: Self) {
Line 2,677:
print("\(dividend) divided by \(divisor) = \(quo) rem \(rem)")
</syntaxhighlight>
{{out}}
Line 2,684:
=={{header|Tailspin}}==
<
templates egyptianDivision
def dividend: $(1);
Line 2,702:
[580"1", 34"1"] -> egyptianDivision -> 'Quotient: $.answer; Remainder: $: 580"1" - $.accumulator;' -> !OUT::write
</syntaxhighlight>
{{out}}
Line 2,708:
=={{header|VBA}}==
<
Private Type MyTable
Line 2,763:
Divise.Quotient = a.answer
Divise.Remainder = Deg.Dividend - a.accumulator
End Function</
{{out}}
<pre>Quotient = 17 Remainder = 2</pre>
Line 2,769:
=={{header|Visual Basic .NET}}==
{{trans|D}}
<
Function EgyptianDivision(dividend As ULong, divisor As ULong, ByRef remainder As ULong) As ULong
Line 2,816:
End Sub
End Module</
{{out}}
<pre>580 / 34 = 17 rem 2</pre>
Line 2,822:
=={{header|Vlang}}==
{{trans|Go}}
<
if dividend < 0 || divisor <= 0 {
Line 2,861:
quotient, remainder := egyptian_divide(dividend, divisor)?
println("$dividend divided by $divisor is $quotient with remainder $remainder")
}</
{{out}}
Line 2,870:
=={{header|Wren}}==
{{trans|Go}}
<
if (dividend < 0 || divisor <= 0) Fiber.abort("Invalid argument(s).")
if (dividend < divisor) return [0, dividend]
Line 2,897:
var divisor = 34
var res = egyptianDivide.call(dividend, divisor)
System.print("%(dividend) ÷ %(divisor) = %(res[0]) with remainder %(res[1]).")</
{{out}}
Line 2,905:
=={{header|zkl}}==
<
table:=[0..].pump(List, 'wrap(n){ // (2^n,divisor*2^n)
r:=T( p:=(2).pow(n), s:=divisor*p); (s<=dividend) and r or Void.Stop });
Line 2,913:
}
return(accumulator,dividend);
}</
<
println("%d %% %d = %s".fmt(dividend,divisor,egyptianDivmod(dividend,divisor)));
}</
{{out}}
<pre>
|