| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export function triplets({ minFactor, maxFactor, sum }) { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 7 | const result = []; |
| 8 | |
| 9 | // Edge Case: Invalid input parameters where minFactor > maxFactor |
| 10 | if (minFactor > maxFactor) { |
| 11 | return result; |
| 12 | } |
| 13 | |
| 14 | // Edge Case: Invalid input parameters where sum is not provided but needed |
| 15 | if (sum === undefined) { |
| 16 | // When sum is not provided, find all triplets in the given range |
| 17 | for (let a = minFactor || 1; a <= maxFactor; a++) { |
| 18 | for (let b = a + 1; b <= maxFactor; b++) { |
| 19 | const cSquared = a * a + b * b; |
| 20 | const c = Math.sqrt(cSquared); |
| 21 | |
| 22 | // Edge Case: c must be an integer and within the specified range |
| 23 | if (Number.isInteger(c) && c <= maxFactor) { |
| 24 | result.push(new Triplet(a, b, c)); |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | } else { |
| 29 | // When sum is provided, find triplets that satisfy a + b + c = sum |
| 30 | // Edge Case: sum must be positive |
| 31 | if (sum <= 0) { |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | // Using the constraint a + b + c = sum and a < b < c, we can derive: |
| 36 | // a < sum/3, b < sum/2 |
| 37 | const maxA = Math.floor((sum - 3) / 3); |
| 38 | const maxB = Math.floor((sum - 1) / 2); |
| 39 | |
| 40 | // Set effective bounds based on provided factors or calculated bounds |
| 41 | const effectiveMinFactor = minFactor || 1; |
| 42 | const effectiveMaxFactor = maxFactor || Math.max(maxA, maxB); |
| 43 | |
| 44 | for (let a = effectiveMinFactor; a <= Math.min(maxA, effectiveMaxFactor); a++) { |
| 45 | // Edge Case: Ensure a is within the specified maxFactor |
| 46 | if (a > effectiveMaxFactor) break; |
| 47 | |
| 48 | for (let b = Math.max(a + 1, effectiveMinFactor); b <= Math.min(maxB, effectiveMaxFactor); b++) { |
| 49 | // Edge Case: Ensure b is within the specified maxFactor |
| 50 | if (b > effectiveMaxFactor) break; |
| 51 | |
| 52 | const c = sum - a - b; |
| 53 | |
| 54 | // Edge Case: c must be greater than b and within the specified range |
| 55 | if (c <= b || c > effectiveMaxFactor) continue; |
| 56 | |
| 57 | // Check if it's a Pythagorean triplet |
| 58 | if (a * a + b * b === c * c) { |
| 59 | result.push(new Triplet(a, b, c)); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return result; |
| 8 | 66 | } |
| 9 | 67 | |
| 10 | 68 | class Triplet { |
| 11 | 69 | constructor(a, b, c) { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 70 | // Edge Case: Validate that a, b, c are positive integers |
| 71 | if (!Number.isInteger(a) || !Number.isInteger(b) || !Number.isInteger(c) || a <= 0 || b <= 0 || c <= 0) { |
| 72 | throw new Error('Triplet values must be positive integers'); |
| 73 | } |
| 74 | |
| 75 | // Edge Case: Validate that a < b < c |
| 76 | if (!(a < b && b < c)) { |
| 77 | throw new Error('Triplet values must satisfy a < b < c'); |
| 78 | } |
| 79 | |
| 80 | this.a = a; |
| 81 | this.b = b; |
| 82 | this.c = c; |
| 13 | 83 | } |
| 14 | 84 | |
| 15 | 85 | toArray() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 86 | return [this.a, this.b, this.c]; |
| 17 | 87 | } |
| 18 | 88 | } |
| 89 | |
| 90 | // Handled Edge Cases: Invalid input parameters where minFactor > maxFactor, Invalid input parameters where sum is not provided but needed, sum must be positive, c must be an integer and within the specified range, a is within the specified maxFactor, b is within the specified maxFactor, c must be greater than b and within the specified range, Validate that a, b, c are positive integers, Validate that a < b < c |