Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Factorial
#2
Interesting! Didn't even know that this is now also available in Basic.

As an example, the Egyptian or Russian pawn multiplication with the shift operator (there are only problems with the formatting of the output - it doesn't always look good):

Code: (Select All)
'Schiebeoperatoren in QBasic64 - 5. Sept. 2022

Option _Explicit

Dim As Long a, b, d1, d2, sum

Cls
Print
Print "Multipliziert zwei Zahlen nach der 'Aegyptischen Multiplikation"
Print "Auch 'russische Bauernmultiplikation' genannt"
Print
Input "Zahl 1: ", a
Input "Zahl 2: ", b
Print: Print

'Nur fuer unten folgende Ausgabe
d1 = a: d2 = b
sum = 0

While a <> 0
  If a Mod 2 = 1 Then 'Wenn 'a' ungerade, dann 'b' addieren.
    Print Using "#####"; b
    sum = sum + b
  End If
  'Schiebeoperator: x >> 1=x/2, x << 1=x*2
  a = _SHR(a, 1)
  b = _SHL(b, 1)
Wend
Print: Print

Print Using "Das Produkt von ###  * #### = #####"; d1, d2, sum

End

[Image: Bauernmultiplikation2022-09-05.jpg]
Reply


Messages In This Thread
Factorial - by Jack - 09-05-2022, 04:01 AM
RE: Factorial - by Kernelpanic - 09-05-2022, 12:02 PM
RE: Factorial - by Jack - 09-05-2022, 12:12 PM
RE: Factorial - by Kernelpanic - 09-05-2022, 12:51 PM



Users browsing this thread: 1 Guest(s)