RosettaCodeData/Task/Egyptian-division/FreeBASIC/egyptian-division.basic
2023-07-01 13:44:08 -04:00

39 lines
820 B
Text

' version 09-08-2017
' compile with: fbc -s console
Data 580, 34
Dim As UInteger dividend, divisor, answer, accumulator, i
ReDim As UInteger table(1 To 32, 1 To 2)
Read dividend, divisor
i = 1
table(i, 1) = 1 : table(i, 2) = divisor
While table(i, 2) < dividend
i += 1
table(i, 1) = table(i -1, 1) * 2
table(i, 2) = table(i -1, 2) * 2
Wend
i -= 1
answer = table(i, 1)
accumulator = table(i, 2)
While i > 1
i -= 1
If table(i,2)+ accumulator <= dividend Then
answer += table(i, 1)
accumulator += table(i, 2)
End If
Wend
Print Str(dividend); " divided by "; Str(divisor); " using Egytian division";
Print " returns "; Str(answer); " mod(ulus) "; Str(dividend-accumulator)
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End