How to resolve the algorithm Ethiopian multiplication step by step in the Smalltalk programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Ethiopian multiplication step by step in the Smalltalk programming language
Table of Contents
Problem Statement
Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.
Method:
For example: 17 × 34 Halving the first column: Doubling the second column: Strike-out rows whose first cell is even: Sum the remaining numbers in the right-hand column: So 17 multiplied by 34, by the Ethiopian method is 578.
The task is to define three named functions/methods/procedures/subroutines:
Use these functions to create a function that does Ethiopian multiplication.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ethiopian multiplication step by step in the Smalltalk programming language
Source code in the smalltalk programming language
Number extend [
double [ ^ self * 2 ]
halve [ ^ self // 2 ]
ethiopianMultiplyBy: aNumber withTutor: tutor [
|result multiplier multiplicand|
multiplier := self.
multiplicand := aNumber.
tutor ifTrue: [ ('ethiopian multiplication of %1 and %2' %
{ multiplier. multiplicand }) displayNl ].
result := 0.
[ multiplier >= 1 ]
whileTrue: [
multiplier even ifFalse: [
result := result + multiplicand.
tutor ifTrue: [
('%1, %2 kept' % { multiplier. multiplicand })
displayNl
]
]
ifTrue: [
tutor ifTrue: [
('%1, %2 struck' % { multiplier. multiplicand })
displayNl
]
].
multiplier := multiplier halve.
multiplicand := multiplicand double.
].
^result
]
ethiopianMultiplyBy: aNumber [ ^ self ethiopianMultiplyBy: aNumber withTutor: false ]
].
(17 ethiopianMultiplyBy: 34 withTutor: true) displayNl.
You may also check:How to resolve the algorithm Repeat a string step by step in the DCL programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the Scala programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Vala programming language
You may also check:How to resolve the algorithm File input/output step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Parametric polymorphism step by step in the Seed7 programming language