Skip to content

Instantly share code, notes, and snippets.

@jecolasurdo
Created September 15, 2018 00:34
Show Gist options
  • Save jecolasurdo/edde1e830ec574294c5a933b83fc0acd to your computer and use it in GitHub Desktop.
Save jecolasurdo/edde1e830ec574294c5a933b83fc0acd to your computer and use it in GitHub Desktop.
pairing function
func pairingForInts(k ...int64) int64 {
if len(k) == 0 {
return -1
}
if len(k) == 1 {
return k[0]
}
var p float64
for n := 0; n+1 < len(k); n++ {
var k1 float64
if n == 0 {
k1 = float64(k[n])
} else {
k1 = p
}
k2 := float64(k[n+1])
p = 0.5*(k1+k2)*(k1+k2+1) + k2
}
return int64(p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment