Method: MoreMath::CantorPairingFunction.cantor_pairing

Defined in:
lib/more_math/cantor_pairing_function.rb

.cantor_pairing(*xs) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/more_math/cantor_pairing_function.rb', line 6

def cantor_pairing(*xs)
  if xs.size == 1 and xs.first.respond_to?(:to_ary)
    xs = xs.first.to_ary
  end
  case xs.size
  when 0, 1
    raise ArgumentError, "at least two arguments are required"
  when 2
    x, y, = *xs
    (x + y) * (x + y + 1) / 2 + y
  else
    cantor_pairing(cantor_pairing(*xs[0..1]), *xs[2..-1])
  end
end