| 1 | 1 | def triplets_with_sum(number): |
| 2 | | pass |
| 2 | result = [] |
| 3 | |
| 4 | # Edge Case: Number is less than 3 (minimum sum for a triplet of natural numbers) |
| 5 | if number < 3: |
| 6 | return result |
| 7 | |
| 8 | # Edge Case: Number is not divisible by 2 or 3, which can help optimize but not eliminate possibilities |
| 9 | # We still need to check all valid combinations |
| 10 | |
| 11 | # Using Euclid's formula approach for efficiency |
| 12 | # For a Pythagorean triplet: a = m² - n², b = 2mn, c = m² + n² |
| 13 | # Where m > n > 0, gcd(m,n) = 1, and m and n are not both odd |
| 14 | # And a + b + c = 2m(m + n) |
| 15 | |
| 16 | # But we'll use a more direct approach for clarity and correctness |
| 17 | |
| 18 | # Since a < b < c and a + b + c = number |
| 19 | # We have a < number/3, b < number/2 |
| 20 | |
| 21 | # Edge Case: Ensuring we don't have negative ranges |
| 22 | a_limit = number // 3 |
| 23 | for a in range(3, a_limit + 1): |
| 24 | # Edge Case: Ensuring b > a and c > b with valid ranges |
| 25 | b_start = a + 1 |
| 26 | b_limit = (number - a) // 2 |
| 27 | |
| 28 | # Edge Case: When b_limit is less than b_start, no valid b exists |
| 29 | if b_limit < b_start: |
| 30 | continue |
| 31 | |
| 32 | for b in range(b_start, b_limit + 1): |
| 33 | c = number - a - b |
| 34 | # Edge Case: Ensuring c > b |
| 35 | if c <= b: |
| 36 | continue |
| 37 | # Check if it's a Pythagorean triplet |
| 38 | if a*a + b*b == c*c: |
| 39 | result.append([a, b, c]) |
| 40 | |
| 41 | return result |
| 42 | |
| 43 | # Handled Edge Cases: Number less than 3, b_limit less than b_start, c not greater than b |