Ethiopian multiplication: Difference between revisions
Content added Content deleted
(smalltalk) |
(C) |
||
Line 57: | Line 57: | ||
:[http://www.ncetm.org.uk/blogs/3064 Ethiopian multiplication] |
:[http://www.ncetm.org.uk/blogs/3064 Ethiopian multiplication] |
||
:[http://www.bbc.co.uk/dna/h2g2/A22808126 Russian Peasant Multiplication] |
:[http://www.bbc.co.uk/dna/h2g2/A22808126 Russian Peasant Multiplication] |
||
=={{header|C}}== |
|||
<lang c>#include <stdio.h> |
|||
#include <stdbool.h> |
|||
void halve(int *x) { *x >>= 1; } |
|||
void doublit(int *x) { *x <<= 1; } |
|||
bool iseven(const int x) { return (x%2) == 0; } |
|||
int ethiopian(int plier, |
|||
int plicand, const bool tutor) |
|||
{ |
|||
int result=0; |
|||
if (tutor) |
|||
printf("ethiopian multiplication of %d by %d\n", plier, plicand); |
|||
while(plier >= 1) { |
|||
if ( iseven(plier) ) { |
|||
if (tutor) printf("%4d %6d struck\n", plier, plicand); |
|||
} else { |
|||
if (tutor) printf("%4d %6d kept\n", plier, plicand); |
|||
result += plicand; |
|||
} |
|||
halve(&plier); doublit(&plicand); |
|||
} |
|||
return result; |
|||
} |
|||
int main() |
|||
{ |
|||
printf("%d\n", ethiopian(17, 34, true)); |
|||
return 0; |
|||
}</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |