DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #218 - Possible Sides of a Non-Right Triangle

Your strict math teacher is teaching you about right triangles and the Pythagorean Theorem --> a^2 + b^2 = c^2 whereas a and b are the legs of the right triangle and c is the hypotenuse of the right triangle.

On the test, however, the question asks: What are the possible integer lengths for the other side of the triangle?. Since you never learned anything about that in class, you realize she meant What are the possible integer lengths for the other side of the right triangle?.

Because you want to address the fact that she asked the wrong question and the fact that you're smart at math, you've decided to answer all the possible values for the third side EXCLUDING the possibilities for a right triangle in increasing order.

Return your answer as a list of all the possible third side lengths of the triangle without right triangles in increasing order.

Examples

side_len(1, 1) --> [1]
side_len(3, 4) --> [2, 3, 4, 6]
side_len(4, 6) -->[3, 4, 5, 6, 7, 8, 9]

Tests

side_len(5, 12)
side_len(8, 10)
side_len(3, 4)

Good luck!


This challenge comes from tonylicoding on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (7)

Collapse
 
empereol profile image
Empereol

TypeScript

/**
 * Return a list of all possible third side lengths (integers) without right
 * trangles.
 *
 * @param sideA Integer
 * @param sideB Integer
 *
 * @throws {TypeError} If either sideA or sideB is not an integer.
 *
 * @example
 * sideLen(1, 1) → [1]
 * sideLen(3, 4) → [2, 3, 4, 6] // 5 is removed as it's a right triangle
 * sideLen(4, 6) → [3, 4, 5, 6, 7, 8, 9]
 */
function sideLen(sideA: number, sideB: number): number[] {
  if (!Number.isInteger(sideA) || !Number.isInteger(sideB)) {
    throw new TypeError('Both provided parameters must be integers');
  }

  const min = Math.abs(sideA - sideB);
  const max = sideA + sideB;
  const hypot = Math.hypot(sideA, sideB);
  const lengths: number[] = [];

  for (let i = min + 1; i < max; i++) {
    if (i !== hypot) {
      lengths.push(i);
    }
  }

  return lengths;
}
Collapse
 
aminnairi profile image
Amin

I think this is the first time I see someone do runtime type checking in TypeScript. Plus, your code helped me understand the goal of the challenge of today. Thanks for that!

Collapse
 
vidit1999 profile image
Vidit Sarkar

Here is a Python one liner,

side_len = lambda a, b: [c for c in range(abs(b-a) + 1, b+a) if not (lambda arr: arr[0] + arr[1] == arr[2])(sorted([a*a, b*b, c*c]))]

And here is the detailed version of above,

def checkPythagorean(a: int, b: int, c: int) -> bool:
    arr = sorted([a*a, b*b, c*c])
    return arr[0] + arr[1] == arr[2]

def side_len(a: int , b: int) -> list:
    lst = []
    for c in range(abs(b-a)+1, b+a):
        if not checkPythagorean(a, b, c):
            lst.append(c)
    return lst

Outputs

print(side_len(1, 1)) # output -> [1]
print(side_len(3, 4)) # output -> [2, 3, 4, 6]
print(side_len(4, 6)) # output -> [3, 4, 5, 6, 7, 8, 9]
print(side_len(5, 12)) # output -> [8, 9, 10, 11, 12, 14, 15, 16]
print(side_len(8, 10)) # output -> [3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
Collapse
 
differentmatt profile image
Matt Lott

JavaScript

const side_len = (a, b) => {
  const results = []
  for (let i = Math.abs(b - a) + 1; i < a + b; i++) {
    if (Math.hypot(a, b) !== i) results.push(i)
  }
  return results
}

const test = (a, b, expected) => {
  const result = side_len(a, b)
  console.log(`a: ${a} b: ${b} e: ${expected} r: ${result}`)
}

test(1, 1, [1])
test(3, 4, [2, 3, 4, 6])
test(4, 6, [3, 4, 5, 6, 7, 8, 9])
test(6, 8, [3, 4, 5, 6, 7, 8, 9, 11, 12, 13])
test(5, 12, [8, 9, 10, 11, 12, 14, 15, 16])
test(8, 10, [3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
Collapse
 
cipharius profile image
Valts Liepiņš

Haskell

import Data.Ratio (numerator, denominator)

side_len :: Int -> Int -> [Int]
side_len a b
  | cd == 1   = [b'-a'+1 .. c'-1] <> [c'+1 .. a'+b'-1]
  | otherwise = [b'-a'+1 .. a'+b'-1]
  where
    a' = a `min` b
    b' = a `max` b
    c  = toRational $ sqrt $ fromIntegral $ a^2 + b^2
    c' = fromIntegral $ numerator c
    cd = fromIntegral $ denominator c
Collapse
 
vidit1999 profile image
Vidit Sarkar

C++ solution

// check if three integers a, b, c can form a right triangle
bool pythagoreanTriplet(int a, int b, int c){
    return (a*a + b*b == c*c) || (a*a + c*c == b*b) || (c*c + b*b == a*a);
}

// returns a vector of all the possible third side lengths
// without right triangles in increasing order
vector<int> side_len(int a, int b){
    vector<int> thirdSide;
    int start = abs(b - a) + 1;
    int end = b + a - 1;

    for(int i=start; i<=end; i++){
        if(!pythagoreanTriplet(a, b, i))
            thirdSide.push_back(i);
    }

    return thirdSide;
}