Skip to content

Commit 2bddc8d

Browse files
committed
refa: solve overseparation of logic
1 parent 43f9e09 commit 2bddc8d

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

src/project-euler/009-special-pythagorean-triplet/README.md

-8
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,3 @@ The function signature is
2323
```typescript
2424
function specialPythagoreanTriplet(targetSum: number): number
2525
```
26-
27-
## Resources
28-
29-
- [Project Euler Problem 009][1]
30-
- [Project Euler Problem 009 at freeCodeCamp][2]
31-
32-
[1]: https://projecteuler.net/problem=9
33-
[2]: https://www.freecodecamp.org/learn/coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet

src/project-euler/009-special-pythagorean-triplet/special-pythagorean-triplet-naive.js

+22-37
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,34 @@
1-
// Check if triplet holds invariant a² + b² = c²
2-
const isSquareValid = ([a, b, c]) => a ** 2 + b ** 2 === c ** 2
3-
4-
// Check if triplet holds invariant a + b + c = targetSum
5-
const isSumValid = ([a, b, c], targetSum) => a + b + c === targetSum
6-
7-
// Check if triplet holds invariant a < b < c
8-
const isRelValid = ([a, b, c]) => a < b && b < c
9-
10-
// Throws an error if triplet doesn't hold sum or relation invariants
11-
const validateTriplet = (triplet, targetSum) => {
12-
if (!isSumValid(triplet, targetSum) || !isRelValid(triplet)) {
13-
throw new Error(`Invalid triplet ${triplet}`)
14-
}
15-
}
16-
17-
// Computes next triplet iteration, while holding sum and relation invariants
18-
const increaseTriplet = (triplet, targetSum) => {
19-
const [a, b, c] = triplet
20-
21-
validateTriplet(triplet, targetSum)
22-
23-
if (b + 2 < c) {
24-
triplet[1] = b + 1
25-
triplet[2] = c - 1
26-
} else {
27-
triplet[0] = a + 1
28-
triplet[1] = a + 2
29-
triplet[2] = targetSum - triplet[0] - triplet[1]
30-
}
31-
}
32-
331
/**
342
* Compute the product of the pythagorean triplet where a + b + c = targetSum.
3+
*
354
* @param {number} targetSum Sum of triplet must equal this value.
365
* @throws {Error} If an invalid triplet is ever reached.
376
* @returns {number} Product of triplet found, where a + b + c = targetSum.
387
*/
398
const specialPythagoreanTriplet = (targetSum) => {
40-
const triplet = [1, 2, targetSum - 3]
41-
42-
while (!isSquareValid(triplet)) {
43-
increaseTriplet(triplet, targetSum)
9+
const tri = [1, 2, targetSum - 3]
10+
11+
while (!(tri[0] ** 2 + tri[1] ** 2 === tri[2] ** 2)) {
12+
const [a, b, c] = tri
13+
14+
const isSumTarget = a + b + c === targetSum
15+
const isAscending = a < b && b < c
16+
17+
if (!isSumTarget || !isAscending) {
18+
throw new Error(`Invalid triplet ${[a, b, c]}`)
19+
}
20+
21+
if (b + 2 < c) {
22+
tri[1] = b + 1
23+
tri[2] = c - 1
24+
} else {
25+
tri[0] = a + 1
26+
tri[1] = a + 2
27+
tri[2] = targetSum - tri[0] - tri[1]
28+
}
4429
}
4530

46-
return triplet.reduce((acc, num) => acc * num)
31+
return tri[0] * tri[1] * tri[2]
4732
}
4833

4934
module.exports = {

0 commit comments

Comments
 (0)