Ethiopian multiplication: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: added halve and double as task requires)
Line 385: Line 385:
show( (17 ethiopicmult 34) );
show( (17 ethiopicmult 34) );
end</lang>
end</lang>

=={{header|Objective-C}}==
Using class methods except for the generic useful function <tt>iseven</tt>.
<lang objc>#import <stdio.h>
#import <objc/Object.h>

BOOL iseven(int x)
{
return (x&1) == 0;
}

@interface EthiopicMult : Object
+ (int)mult: (int)plier by: (int)plicand;
+ (int)halve: (int)a;
+ (int)double: (int)a;
@end

@implementation EthiopicMult
+ (int)mult: (int)plier by: (int)plicand
{
int r = 0;
while(plier >= 1) {
if ( !iseven(plier) ) r += plicand;
plier = [EthiopicMult halve: plier];
plicand = [EthiopicMult double: plicand];
}
return r;
}

+ (int)halve: (int)a
{
return (a>>1);
}

+ (int)double: (int)a
{
return (a<<1);
}
@end

int main()
{
printf("%d\n", [EthiopicMult mult: 17 by: 34]);
return 0;
}</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==