Egyptian division: Difference between revisions

no edit summary
imported>Maxima enthusiast
No edit summary
No edit summary
 
(8 intermediate revisions by 6 users not shown)
Line 639:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|QBasic}} But the arrays are subscripted from 0.
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 REM Egyptian division
110 DIM Table(0 TO 31, 0 TO 1)
120 LET Dividend = 580
130 LET Divisor = 34
140 REM ** Division
150 LET I = 0
160 LET Table(I, 0) = 1
170 LET Table(I, 1) = Divisor
180 DO WHILE Table(I, 1) < Dividend
190 LET I = I + 1
200 LET Table(I, 0) = Table(I - 1, 0) * 2
210 LET Table(I, 1) = Table(I - 1, 1) * 2
220 LOOP
230 LET I = I - 1
240 LET Answer = Table(I, 0)
250 LET Accumulator = Table(I, 1)
260 DO WHILE I > 0
270 LET I = I - 1
280 IF Table(I, 1) + Accumulator <= Dividend THEN
290 LET Answer = Answer + Table(I, 0)
300 LET Accumulator = Accumulator + Table(I, 1)
310 END IF
320 LOOP
330 REM ** Results
340 PRINT Dividend; "divided by"; Divisor; "using Egytian division";
350 PRINT " returns"; Answer; "remainder"; Dividend - Accumulator
360 END
</syntaxhighlight>
{{out}}
<pre>
580 divided by 34 using Egytian division returns 17 remainder 2
</pre>
 
==={{header|Applesoft BASIC}}===
The [[#MSX_BASIC|MSX BASIC]] solution works without any changes.
 
==={{header|BaCon}}===
<syntaxhighlight lang="c">'---Ported from the c code example to BaCon by bigbass
Line 733 ⟶ 773:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|QBasic}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="vbnet">100 dim table(32,2)
110 let dividend = 580
120 let divisor = 34
130 let i = 1
140 let table(i,1) = 1
150 let table(i,2) = divisor
160 do while table(i,2) < dividend
170 let i = i+1
180 let table(i,1) = table(i-1,1)*2
190 let table(i,2) = table(i-1,2)*2
200 loop
210 let i = i-1
220 let answer = table(i,1)
230 let accumulator = table(i,2)
240 do while i > 1
250 let i = i-1
260 if table(i,2)+accumulator <= dividend then
270 let answer = answer+table(i,1)
280 let accumulator = accumulator+table(i,2)
290 endif
300 loop
310 print str$(dividend);" divided by ";str$(divisor);" using Egytian division";
320 print " returns ";str$(answer);" mod(ulus) ";str$(dividend-accumulator)
330 end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Line 776 ⟶ 845:
{{out}}
<pre>580 divided by 34 using Egytian division returns 17 mod(ulus) 2</pre>
 
==={{header|GW-BASIC}}===
{{trans|QBasic}}
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 DIM T(32,2)
120 LET A = 580
130 LET B = 34
140 LET I = 1
150 LET T(I,1) = 1
160 LET T(I,2) = B
170 WHILE T(I,2) < A
180 LET I = I+1
190 LET T(I,1) = T(I-1,1)*2
200 LET T(I,2) = T(I-1,2)*2
210 WEND
220 LET I = I-1
230 LET R = T(I,1)
240 LET S = T(I,2)
250 WHILE I > 1
260 LET I = I-1
270 IF T(I,2)+S <= A THEN LET R = R+T(I,1): LET S = S+T(I,2)
280 WEND
290 PRINT STR$(A);" divided by ";STR$(B);" using Egytian division";
300 PRINT " returns ";STR$(R);" mod(ulus) ";STR$(A-S)
310 END</syntaxhighlight>
 
==={{header|Just BASIC}}===
Same code as [[#QBasic|QBasic]]
 
==={{header|Minimal BASIC}}===
{{trans|ANSI BASIC}}
{{works with|BASICA}}
{{works with|Commodore BASIC|3.5}}
{{works with|MSX BASIC|2.1}}
<syntaxhighlight lang="basic">
10 REM Egyptian division
20 DIM T(31,1)
30 REM D1 - dividend; D2 - divisor
40 LET D1 = 580
50 LET D2 = 34
60 REM ** Division
70 LET I = 0
80 LET T(I,0) = 1
90 LET T(I,1) = D2
100 IF T(I,1) >= D1 THEN 160
110 LET I = I+1
120 LET T(I,0) = T(I-1,0)*2
130 LET T(I,1) = T(I-1,1)*2
140 GOTO 100
150 REM A - answer; C - accumulator
160 LET I = I-1
170 LET A = T(I,0)
180 LET C = T(I,1)
190 IF I <= 0 THEN 250
200 LET I = I-1
210 IF T(I,1)+C > D1 THEN 190
220 LET A = A+T(I,0)
230 LET C = C+T(I,1)
240 GOTO 190
250 REM ** Results
260 PRINT D1; "divided by"; D2;
270 PRINT "using Egyptian division";
280 PRINT " returns"; A; "remainder"; D1-C
290 END
</syntaxhighlight>
{{out}}
<pre>
580 divided by 34 using Egyptian division returns 17 remainder 2
</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|Applesoft BASIC}}
{{works with|GW-BASIC}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">110 DIM T(32,2)
120 A = 580
130 B = 34
140 I = 1
150 T(I,1) = 1
160 T(I,2) = B
170 IF T(I,2) < A THEN I = I+1 : T(I,1) = T(I-1,1)*2 : T(I,2) = T(I-1,2)*2
180 IF T(I,2) >= A THEN GOTO 200
190 GOTO 170
200 I = I-1
210 R = T(I,1)
220 S = T(I,2)
230 IF I > 1 THEN I = I-1 : IF T(I,2)+S <= A THEN R = R+T(I,1): S = S+T(I,2)
240 IF I <= 1 THEN GOTO 260
250 GOTO 230
260 PRINT STR$(A);" divided by ";STR$(B);" using Egytian division";
270 PRINT " returns ";STR$(R);" mod(ulus) ";STR$(A-S)
280 END</syntaxhighlight>
 
==={{header|PureBasic}}===
Line 814 ⟶ 981:
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|Run BASIC}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="qbasic">DIM table(32, 2)
dividend = 580
Line 843 ⟶ 1,013:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Run BASIC}}===
Same code as [[#QBasic|QBasic]]
 
==={{header|True BASIC}}===
Line 928 ⟶ 1,101:
{{out}}
<pre>580 / 34 = 17 rem 2</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Egyptian division"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
DIM T[32,2]
A = 580
B = 34
I = 1
T[I,1] = 1
T[I,2] = B
DO WHILE T[I,2] < A
INC I
T[I,1] = T[I-1,1]*2
T[I,2] = T[I-1,2]*2
LOOP
DEC I
R = T[I,1]
S = T[I,2]
DO WHILE I > 1
DEC I
IF T[I,2]+S <= A THEN
R = R+T[I,1]
S = S+T[I,2]
END IF
LOOP
PRINT A;" divided by";B;" using Egytian division";
PRINT " returns";R;" mod(ulus)"; A-S
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 1,537 ⟶ 1,744:
 
Press any key to continue . . .</pre>
=={{header|EasyLang}}==
{{trans|Phix}}
<syntaxhighlight>
proc egyptdiv a b . .
p2 = 1
dbl = b
while dbl <= a
p2s[] &= p2
dbls[] &= dbl
dbl *= 2
p2 *= 2
.
for i = len p2s[] downto 1
if acc + dbls[i] <= a
acc += dbls[i]
ans += p2s[i]
.
.
print a & " / " & b & " = " & ans & " R " & abs (acc - a)
.
egyptdiv 580 34
</syntaxhighlight>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">-module(egypt).
Line 2,291 ⟶ 2,521:
- : int * int = (17, 578)
</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
myrow(powers_of2, doublings) =
{
if (dividend > doublings,
myrow(2 * powers_of2, 2 * doublings);
if (accumulator + doublings <= dividend,
answer += powers_of2;
accumulator += doublings;
);
);
};
 
egyptian_divrem(dividend, divisor) =
{
local(answer, accumulator, row);
answer = 0;
accumulator = 0;
myrow(1, divisor);
[answer, dividend - accumulator];
}
 
divisor=34;
dividend=580;
print1(egyptian_divrem(dividend, divisor));
</syntaxhighlight>
{{out}}
<pre>
[17, 2]
</pre>
 
 
=={{header|Perl}}==
Line 2,724 ⟶ 2,987:
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.
 
Note: if the below is just a mass of "unknown glyph" boxes, try [https://www.google.com/get/noto/help/install/ installing] Googles free [https://wwwfonts.google.com/get/noto/#sans-egypfonts?noto.lang=egy_Egyp&noto.script=Egyp Noto Sans Egyptian Hieroglyphs font].
 
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.
Line 3,108 ⟶ 3,371:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var egyptianDivide = Fn.new { |dividend, divisor|
if (dividend < 0 || divisor <= 0) Fiber.abort("Invalid argument(s).")
if (dividend < divisor) return [0, dividend]
Line 3,178 ⟶ 3,441:
580 / 34 = 17 with remainder 2.
</pre>
 
=={{header|Zig}}==
{{trans|C}}
<syntaxhighlight lang="zig">const std = @import("std");
 
pub fn egyptianDivision(dividend: u64, divisor: u64) [2]u64 {
const SIZE = 64;
var powers = [_]u64{0} ** SIZE;
var doublings = [_]u64{0} ** SIZE;
 
var i: u64 = 0;
 
while (i < SIZE) {
powers[i] = std.math.shl(u64, 1, i);
doublings[i] = std.math.shl(u64, divisor, i);
if (doublings[i] > dividend) {
break;
}
i += 1;
}
 
var accumulator: u64 = 0;
var answer: u64 = 0;
i -= 1;
while (i >= 0) {
if (accumulator + doublings[i] <= dividend) {
accumulator += doublings[i];
answer += powers[i];
}
if (i > 0) {
i -= 1;
} else {
break;
}
}
var remainder = dividend - accumulator;
return .{ answer, remainder };
}
 
test "Expect 10, 0 from egyptianDivision(20, 2)" {
var output = egyptianDivision(20, 2);
try std.testing.expect(output[0] == 10);
try std.testing.expect(output[1] == 0);
}
 
test "Expect 580 divided by 34 is 17 and the remainder is 2" {
var output = egyptianDivision(580, 34);
try std.testing.expect(output[0] == 17);
try std.testing.expect(output[1] == 2);
}
 
pub fn main() !void {
var result = egyptianDivision(20, 2);
std.debug.print("20 divided by 2 is {} with remainder {}\n", .{ result[0], result[1] });
}
</syntaxhighlight>
{{out}}
<pre>20 divided by 2 is 10 with remainder 0</pre>
 
=={{header|zkl}}==
2

edits