Created
September 15, 2018 00:34
-
-
Save jecolasurdo/edde1e830ec574294c5a933b83fc0acd to your computer and use it in GitHub Desktop.
pairing function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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