The handshake problem
JavaScript ES6, 16

n=&gt;n%1?0:~-n*n/2


Test



f=n=&gt;n%1?0:~-n*n/2

function test()
{
  var i=I.value
  O.textContent=f(i)
}  
&lt;input id=I oninput=test()&gt;&lt;span id=O&gt;&lt;/span&gt;