Skip to content

Instantly share code, notes, and snippets.

@dyoo
Last active January 9, 2025 12:04

Revisions

  1. dyoo revised this gist Dec 20, 2013. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion cantor_pairing.go
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,13 @@ func InvertedCantorPairing(z int) (int, int) {
    return x, y
    }

    func CantorPairing(k1, k2 int) int {
    return (k1+k2)*(k1+k2+1)/2 + k2
    }

    func main() {
    for i := 0; i < 100; i++ {
    x, y := InvertedCantorPairing(i)
    fmt.Println(i, x, y)
    fmt.Println(i, x, y, CantorPairing(x, y))
    }
    }
  2. dyoo renamed this gist Dec 20, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. dyoo created this gist Dec 20, 2013.
    22 changes: 22 additions & 0 deletions inverted_cantor_pairing.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // http://en.wikipedia.org/wiki/Pairing_function
    package main

    import (
    "fmt"
    "math"
    )

    func InvertedCantorPairing(z int) (int, int) {
    w := int(math.Floor((math.Sqrt(float64(8*z+1)) - 1) / 2))
    t := (w*w + w) / 2
    y := z - t
    x := w - y
    return x, y
    }

    func main() {
    for i := 0; i < 100; i++ {
    x, y := InvertedCantorPairing(i)
    fmt.Println(i, x, y)
    }
    }