Ethiopian multiplication: Difference between revisions
Content added Content deleted
(→{{header|J}}: Removed second section as contrary to the comment, it does not obey the letter of the task description as it fails to use the three functions.) |
|||
Line 731: | Line 731: | ||
import "fmt" |
import "fmt" |
||
func halve(i int) int { return i |
func halve(i int) int { return i/2 } |
||
func double(i int) int { return i |
func double(i int) int { return i*2 } |
||
func isEven(i int) bool { return |
func isEven(i int) bool { return i%2 == 0 } |
||
func ethMulti(i, j int) (r int) { |
func ethMulti(i, j int) (r int) { |
||
for ; i > 0; i, j = halve(i), double(j) { |
for ; i > 0; i, j = halve(i), double(j) { |
||
if !isEven(i) { |
if !isEven(i) { |
||
r += j |
r += j |
||
} |
|||
} |
} |
||
⚫ | |||
} |
|||
⚫ | |||
} |
} |
||
func main() { |
func main() { |
||
fmt.Printf("17 ethiopian 34 = %d\n", ethMulti(17, 34)) |
fmt.Printf("17 ethiopian 34 = %d\n", ethMulti(17, 34)) |
||
} |
|||
</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |