class PairingFunction

Public Class Methods

pair(x, y) click to toggle source
# File lib/pairing_function.rb, line 2
def self.pair(x, y)
  (((x + y)**2) + (3 * x) + y) / 2
end
unpair(z) click to toggle source
# File lib/pairing_function.rb, line 6
def self.unpair(z)
  c = (((Math.sqrt((8 * z) + 1)) - 1) / 2).floor
  x = z - ((c * (c + 1)) / 2)
  y = (c * (c + 3) / 2) - z

  return x, y
end