Skip to main content
deleted 219 characters in body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
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>
F=n=>{
  l=1e8;
  if(n>=0) {
    v=Math.sqrt(n/2)|0;
    for(b=v>>2|1;b<v+9;b++)
    [w=(n-b)/(2*b)|0,w+1].map(r=>(
      t=2*r*b+b,
      q='|'.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<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.value,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>
F=n=>{
  l=1e8;
  v=Math.sqrt(n>0?n:-n)/2|0;
  p='|';
  if (n)
    for(b=v>>2|1;b<=v+v+9;b++)
    [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=n>0?'|'.repeat(2*b+1)+'-|'.repeat(r)+'-'.repeat(2*b)
      :'|'.repeat(2*b)+'-|'.repeat(r)+'-'.repeat(2*b-1),
      q+=t<n?'||--'.repeat(n-t):'|-'.repeat(t-n),
      m=q.length,m<l&&(l=m,p=q)
    ))
  return p
}

// Test --------------------------------------------

FreeTest=_=>{var n=IN.value|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>
added 78 characters in body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90

Edit 2 Bug fix, same results. I have an idea, but cant't nail it down.

Bytes: ~460, quite golfed. It works on 32 bit integers, run time near 0.

F=n=>{
  l=1e8;
  if(n>=0) {
    v=Math.sqrt(n/2)|0;
    for(b=v>>4|1;b<v+9;b++b=v>>2|1;b<v+9;b++)
    [v=[w=(n-b)/(2*b)|0,v+1]w+1].map(r=>(
      t=2*r*b+b,
      q='|'.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>>4|1;b<v+9;b++b=v>>2|1;b<v+9;b++)
    [v=[w=(-n-b)/(2*b-1)|0,v+1]w+1].map(r=>(
      t=-(2*r*b-r+b),
      q='|'.repeat(2*b)+'-|'.repeat(r)+'-'.repeat(2*b-1),
      q=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.value,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>

Bytes: ~460, quite golfed. It works on 32 bit integers, run time near 0.

F=n=>{
  l=1e8;
  if(n>=0) {
    v=Math.sqrt(n/2)|0;
    for(b=v>>4|1;b<v+9;b++)
    [v=(n-b)/(2*b)|0,v+1].map(r=>(
      t=2*r*b+b,
      q='|'.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>>4|1;b<v+9;b++)
    [v=(-n-b)/(2*b-1)|0,v+1].map(r=>(
      t=-(2*r*b-r+b),
      q='|'.repeat(2*b)+'-|'.repeat(r)+'-'.repeat(2*b-1),
      q=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.value,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>

Edit 2 Bug fix, same results. I have an idea, but cant't nail it down.

Bytes: ~460, quite golfed. It works on 32 bit integers, run time near 0.

F=n=>{
  l=1e8;
  if(n>=0) {
    v=Math.sqrt(n/2)|0;
    for(b=v>>2|1;b<v+9;b++)
    [w=(n-b)/(2*b)|0,w+1].map(r=>(
      t=2*r*b+b,
      q='|'.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<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.value,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>
added 7 characters in body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90

Edit As I said in my explanation, I'm not good at solving integer equations. My guess of b value was not so good, so I have widened the range of values to try. And (wow) I'm leading by now.And (wow) I'm leading by now.

Edit As I said in my explanation, I'm not good at solving integer equations. My guess of b value was not so good, so I have widened the range of values to try. And (wow) I'm leading by now.

Edit As I said in my explanation, I'm not good at solving integer equations. My guess of b value was not so good, so I have widened the range of values to try. And (wow) I'm leading by now.

typo in explanation
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
Loading
added 200 characters in body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
Loading
edited body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
Loading
added 1243 characters in body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
Loading
added 1243 characters in body
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
Loading
Source Link
edc65
  • 32.2k
  • 3
  • 36
  • 90
Loading