Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!