Cantor's Enumeration

Of Positive Rationals

Rational NaturalToRational(int n)
{ int s=(1+(int)sqrt(8*n+1))/2;
  int t=n-(s*s-s)/2;
  return Rational(t, s-t); }

int RationalToNatural(Rational r)
{ return ((r.top+r.bot)*(r.top+r.bot)+r.top-r.bot)/2; }

Rational NextAfter(Rational r)
{ int t=r.top+1, b=r.bot-1, s=r.top+r.bot;
  if (t>=s) { t=0; b=s+1; }
  return Rational(t,b); }