F=n=>{
l=1e8;
ifv=Math.sqrt(n>=0n>0?n:-n) {/2|0;
p='|';
v=Math.sqrtif (n/2)|0;
for(b=v>>2|1;b<v+9;b++b=v>>2|1;b<=v+v+9;b++)
[w=[w=n>0?(n-b)/(2*b)|0:(-n-b)/(2*b-1)|0,w+1].map(r=>(
t=2*r*b+b, d=n-t, n<0?t=r-t:0,
q='|'q=n>0?'|'.repeat(2*b+1)+'-|'.repeat(r)+'-'.repeat(2*b),
q=t<n?q+'||--'.repeat(n-t):q+'|-'.repeat(t-n),
m=q.length,m<l&&(l=m,p=q)
))
}
else
{
v=Math.sqrt(-n/2)|0;
for(b=v>>2|1;b<v+9;b++)
[w=(-n-b)/(2*b-1)|0,w+1].map(r=>(
t=-(2*r*b-r+b),
q='|''|'.repeat(2*b)+'-|'.repeat(r)+'-'.repeat(2*b-1),
q=t<nq+=t<n?q+'||'||--'.repeat(n-t):q+'|'|-'.repeat(t-n),
m=q.length,m<l&&(l=m,p=q)
))
}
return p
}
// Test --------------------------------------------
FreeTest=_=>{var n=IN.valuevalue|0,s=F(n)
LEN.value=s.length,CK.value=Check(s),ST.innerHTML=s}
Check=t=>{var s=[],a,b;
[...t].forEach(c=>c=='|'? s.push(s.length):(b=s.pop(),a=s.pop(),s.push(a-b)))
return s.pop()}
ot=[];
tot=0;
[-8607,-6615,-6439,-4596,-4195,-1285,-72,
12,254,1331,3366,3956,5075,5518,5971,7184,7639,8630,9201,9730]
.forEach(x=>{
s=F(x), k=Check(s), l=s.length, tot+=l,
ot.push(`<tr><td>${x}</td><td>${l}</td><td>${k}</td><td class='S'>${s}</td></tr>`)
})
ot.push(`<tr><th>Total:</th><th>${tot}</th></tr>`)
TOUT.innerHTML=ot.join("")
td.S { font-size: 8px; font-family: courier}
pre { font-size: 8px; }
input { width: 6em }
Free test: <input id="IN" value=1073741824>
<button onclick="FreeTest()">-></button>
Len:<input readonly id="LEN"> Check:<input readonly id="CK">
<pre id=ST></pre>
<table><thead>
<tr><th>Number</th><th>Len</th><th>Check</th><th>Sticks</th></tr>
</thead>
<tbody id=TOUT></tbody>
</table>