Ethiopian Multiplication
Python 2, 203 202 187 133 bytes
a,b=input()
s=0
while a:print'%3s%9s'%(a,'[ %%dd] '[a%2::2]%b);s+=[0,b][a%2];a/=2;b*=2
print'%10s==\n%11s'%(''.rjust(len(`s`),'='),s)
Try it online!
If I can use *
for string multiplication ('='*R
) and as a 'selector' (b*(a%2)
instead of [0,b][a%2]
), I get:
118 bytes
a,b=input()
s=0
while a:print'%3s%9s'%(a,'[ %%dd] '[a%2::2]%b);s+=a%2*b;a/=2;b*=2
print'%10s==\n%11s'%('='*len(`s`),s)
Try it online!
Explanation:
a,b=input() #Get input
L=len(`a`) #Get length of first number for adjusting text
l=[] #Output list
s=0 #Sum
while a:
B=['[%d]',' %d '][a%2]%b #B is either '[b]' or ' b ' depending on if a is odd/even
l+=[(`a`,B)] #Add a,B to output list
s+=[0,b][a%2] #Add b to sum if a is odd
a/=2; #Halve a
b*=2; #Double b
R=len(B) #Length of last B for adjusting output
l+=[('',''.rjust(R,'='))] #Add double line ==== to output list
l+=[('','%d '%s)] #Add sum to output list
for x,y in l:
print x.rjust(L),y.rjust(R) #Print adjusted numbers
Charcoal, 91 bytes
≔⟦⟧τ≔⁰σNθNηWθ«⊞τ⪫ Iθ⊞υ⪫⎇﹪θ² ¦[]Iη≔⁺σ∧﹪θ²ησ≔÷θ²θ≔⁺ηηη»⊞υ…=⁺²LIσ⊞υ⪫ Iσ←E⮌τ⮌ιM⌈EυLιLυ←E⮌υ⮌ι
Try it online! Link is to verbose version of code. Explanation:
≔⟦⟧τ≔⁰σ
Sets t
to the empty list and s
to 0
. (u
already defaults to the empty list.)
NθNη
Inputs the two numbers.
Wθ«
Repeats while q
is nonzero.
⊞τ⪫ Iθ
Wrap q
in padding and append it to the list t
.
⊞υ⪫⎇﹪θ² ¦[]Iη
Wrap h
either in padding or []
depending on whether q
is odd, and append it to the list u
.
≔⁺σ∧﹪θ²ησ
Add h
to s
if q
is odd.
≔÷θ²θ
Integer divide q
by 2.
≔⁺ηηη»
Add h
to itself.
⊞υ…=⁺²LIσ
Append a suitable string of =
signs to the list u
.
⊞υ⪫ Iσ
Append the padded sum s
to the list u
.
←E⮌τ⮌ι
Rotate the list t
by 180° and print it up-side down, thus right-justifying it.
M⌈EυLιLυ←E⮌υ⮌ι
Move the cursor so that when u
is right-justified its top left corner lines up with the top-right corner we just reached, and print u
right-justified.
Java (OpenJDK 8), 353 316 267 214 210 bytes
(a,b)->{int g=0;for(;a>0;g+=a%2*b,a/=2,b*=2)System.out.printf("%1$8d%2$10s\n",a,a%2<1?"["+b+"]":b+" ");System.out.printf("%1$19s%2$18s","".valueOf(new char[(int)Math.log10(g)+3]).replace("\0","=")+"\n",g+" ");}
Try it online!