Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
How to generate 3 unique fractions without being reducable in Javascript?

I need to generate 3 fractions with these rules:

  • denominator is the same
  • numerator must be unique and between 1 and denominator * 3
  • Fraction must not be reducable (for example: 2 / 6 can be reduced to 1 /3 which ins't allowed)

I already have a function to generate unique numertors

static randomUnique(range: number, count: number) {
  let nums: number[] = [];
  while (nums.length < count) {
    nums.push(Math.floor(Math.random() * (range) + 1));
  }
  return [...nums];
}

and a function to check if the fraction is reducable

static isSimplified(numOne: number, numTwo: number) {
        let numerator = numOne;
        let denominator = numTwo;
        for (let i = Math.max(numOne, numTwo); i > 1; i--) {
            if ((numOne % i === 0) && (numTwo % i === 0)) {
                numOne /= i;
                numTwo /= i;
            }
        }
        return numOne === numerator && numTwo === denominator ? false : true;
    }

The problem is that the generated numerators can be reducable so I changed the randomUnique function to this

static randomUnique(denominator: number, count: number) {
    let nums: number[] = [];
    let numerator = 0;

    while ((nums.length < count)) {
        numerator = Math.floor(Math.random() * (denominator * 3) + 1);
        if (!FractionAxisLogic.isSimplified(numerator, denominator))
            nums.push(numerator);
    }
    return [...nums];
}

And now the problem that numerators aren't unique

How can I fix this please?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I fixed this by changing let nums: number[] = []; to using javascript Set() which is an object for storing unique values.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda