Ethiopian multiplication: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,892: | Line 1,892: | ||
12 tests, 12 assertions, 0 failures, 0 errors</pre> |
12 tests, 12 assertions, 0 failures, 0 errors</pre> |
||
=={{header|Scala}}== |
|||
The first and second are only slightly different and use functional style. |
|||
The third uses a for loop to yield the result. |
|||
<lang scala> |
|||
def ethiopian(i:Int, j:Int):Int= |
|||
pairIterator(i,j).filter(x=> !isEven(x._1)).map(x=>x._2).foldLeft(0){(x,y)=>x+y} |
|||
def ethiopian2(i:Int, j:Int):Int= |
|||
pairIterator(i,j).map(x=>if(isEven(x._1)) 0 else x._2).foldLeft(0){(x,y)=>x+y} |
|||
def ethiopian3(i:Int, j:Int):Int= |
|||
{ |
|||
var res=0; |
|||
for((h,d) <- pairIterator(i,j) if !isEven(h)) res+=d; |
|||
res |
|||
} |
|||
def isEven(x:Int)=(x&1)==0 |
|||
def halve(x:Int)=x>>>1 |
|||
def double(x:Int)=x<<1 |
|||
// generates pairs of values (halve,double) |
|||
def pairIterator(x:Int, y:Int)=new Iterator[(Int, Int)] |
|||
{ |
|||
var i=(x, y) |
|||
def hasNext=i._1>0 |
|||
def next={val r=i; i=(halve(i._1), double(i._2)); r} |
|||
} |
|||
</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |