Generate next rational number
-
24-06-2021 - |
Pergunta
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:
Someone has an idea how can I implement it? Thanks
Solução
n is numerator, d is denominator:
if (n%2 == d%2) {
n++;
if (d > 1) d--;
} else {
d++;
if (n > 1) n--;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow