Ethiopian multiplication: Difference between revisions
Content added Content deleted
imported>Maxima enthusiast No edit summary |
|||
Line 3,788: | Line 3,788: | ||
578 |
578 |
||
</syntaxhighlight> |
|||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima"> |
|||
/* Function to halve */ |
|||
halve(n):=floor(n/2)$ |
|||
/* Function to double */ |
|||
double(n):=2*n$ |
|||
/* Predicate function to check wether an integer is even */ |
|||
my_evenp(n):=if mod(n,2)=0 then true$ |
|||
/* Function that implements ethiopian function using the three previously defined functions */ |
|||
ethiopian(n1,n2):=block(cn1:n1,cn2:n2,list_w:[], |
|||
while cn1>0 do (list_w:endcons(cn1,list_w),cn1:halve(cn1)), |
|||
n2_list:append([cn2],makelist(cn2:double(cn2),length(list_w)-1)), |
|||
sublist_indices(list_w,lambda([x],not my_evenp(x))), |
|||
makelist(n2_list[i],i,%%), |
|||
apply("+",%%))$ |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||