Finished
44//
55
66export 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;
866}
967
1068class Triplet {
1169 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;
1383 }
1484
1585 toArray() {
16 throw new Error('Remove this line and implement the function');
86 return [this.a, this.b, this.c];
1787 }
1888}
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
Test NameStatus
triplets whose sum is 12
Pass
triplets whose sum is 108
Pass
triplets whose sum is 1000
Pass
no matching triplets for 1001
Pass
returns all matching triplets
Pass
several matching triplets
Pass
returns triplets with no factor smaller than minimum factor
Pass
returns triplets with no factor larger than maximum factor
Pass
returns triplets with factors in range
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.