Only works with positive real numbers, not negative or complex.
Copy sub egyptian-divmod (Real $dividend is copy where * >= 0, Real $divisor where * > 0) {
my $accumulator = 0;
([1, $divisor], { [.[0] + .[0], .[1] + .[1]] } โฆ ^ *.[1] > $dividend)
. reverse . map : { $dividend -= .[1], $accumulator += .[0] if $dividend >= .[1] }
$accumulator, $dividend;
}
#TESTING
for 580,34, 578,34, 7532795332300578,235117 -> $n, $d {
printf "%s divmod %s = %s remainder %s\n" ,
$n, $d, |egyptian-divmod( $n, $d )
}
Copy 580 divmod 34 = 17 remainder 2
578 divmod 34 = 17 remainder 0
7532795332300578 divmod 235117 = 32038497141 remainder 81
As a preceding version was determined to be "let's just say ... not Egyptian" we submit an alternate which is hopefully more "Egyptian". Now only handles positive Integers up to 10 million, mostly due to limitations on Egyptian notation for numbers.
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.
Copy my (\๐ค, \๐, \๐, \๐ฐ) = (0, 1, 10, 10e7);
sub infix :<๐ฝ> { $^๐ + $^๐ }
sub infix :<๐ป> { $^๐ฒ - $^๐ }
sub infix :<๐> { $^๐ < $^๐ข }
sub ๐ถ (Int \๐) {
my \๐ข = [ยซ '' ๐บ ๐ป ๐ผ ๐ฝ ๐พ ๐ฟ ๐ ๐ ๐ยป], [ยซ '' ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ยป],
[ยซ '' ๐ข ๐ฃ ๐ค ๐ฅ ๐ฆ ๐ง ๐จ ๐ฉ ๐ชยป], [ยซ '' ๐ผ ๐ฝ ๐พ ๐ฟ ๐ ๐ ๐ ๐ ๐ยป],
[ยซ '' ๐ญ ๐ฎ ๐ฏ ๐ฐ ๐ฑ ๐ฒ ๐ณ ๐ด ๐ตยป], [ '๐' Xx ^๐], [ '๐จ' Xx ^๐];
([~] ๐.polymod( ๐ xx * ). map ( { ๐ข[$++;$_] } ). reverse ) || '๐ค'
}
sub infix :<๐
> (Int $๐ is copy where ๐ค ๐ป ๐ ๐ * ๐ ๐ฐ, Int \๐ณ where ๐ค ๐ * ๐ ๐ฐ) {
my $๐ฆ = ๐ค;
([๐,๐ณ], { [.[๐ค] ๐ฝ .[๐ค], .[๐] ๐ฝ .[๐]] } โฆ ^$๐ ๐ *.[๐])
. reverse . map : { $๐ ๐ป= .[๐], $๐ฆ ๐ฝ= .[๐ค] if .[๐] ๐ ($๐ ๐ฝ ๐) }
$๐ฆ, $๐;
}
#TESTING
for 580,34, 578,34, 2300578,23517 -> \๐พ, \๐ {
printf "%s divmod %s = %s remainder %s =OR= %s ๐
%s = %s remainder %s\n" ,
๐พ, ๐, |(๐พ ๐
๐), (๐พ, ๐, |(๐พ ๐
๐))ยป.&๐ถ;
}
Copy 580 divmod 34 = 17 remainder 2 =OR= ๐ฆ๐ ๐
๐๐ฝ = ๐๐ remainder ๐ป
578 divmod 34 = 17 remainder 0 =OR= ๐ฆ๐๐ ๐
๐๐ฝ = ๐๐ remainder ๐ค
2300578 divmod 23517 = 97 remainder 19429 =OR= ๐จ๐จ๐๐๐๐ฆ๐๐ ๐
๐ฎ๐พ๐ฆ๐๐ = ๐๐ remainder ๐ญ๐๐ฅ๐๐