I have a specific requirement to create 3 fractions with the following conditions:
- The denominator remains constant
- The numerator must be unique and fall within the range of 1 to three times the denominator
- The fraction should not be reducible (e.g., 2/6 can be simplified to 1/3, which is not allowed)
Currently, I possess a function that can generate unique numerators:
static randomUnique(range: number, count: number) {
let nums: number[] = [];
while (nums.length < count) {
nums.push(Math.floor(Math.random() * (range) + 1));
}
return [...nums];
}
Additionally, I have a method to determine if a fraction is simplifiable:
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 issue arises when the generated numerators can potentially be reduced. Hence, I modified the randomUnique
function as follows:
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];
}
However, now an issue has emerged where the numerators
are no longer unique.
Could you provide guidance on how to address this concern?