Generate next rational number

347 Views Asked by At

How can i generate the next rational number into 2 integer variables. For example, If i have x=3 and y = 2 the next rational number is x=3 and y=3. The number generation need to be like the next order:

See the needed order

Someone has an idea how can I implement it? Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

n is numerator, d is denominator:

if (n%2 == d%2) {
  n++;
  if (d > 1) d--;
} else {
  d++;
  if (n > 1) n--;
}