Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to check if an array contains the Pythagorean triplet numbers (a*a+b*b=c*c) in Node.js

2 Answers

0 votes
function containsPythagoreanTripletNumbers(arr) { 
    const size = arr.length;
      
    for (let i = 0; i < size; i++) { 
        for (let j = i + 1; j < size; j++) { 
            for (let k = j + 1; k < size; k++) { 
                let a = arr[i] * arr[i];
                let b = arr[j] * arr[j];
                let c = arr[k] * arr[k]; 
                  
                if (a == b + c || b == a + c || c == a + b) {
                    return true; 
                }
            } 
        } 
    } 
     
    return false; 
}  
     
  
const arr = [4, 9, 3, 6, 5]; // 3*3 + 4*4 = 5*5 // 9 + 16 = 25
  
let r = containsPythagoreanTripletNumbers(arr) ? "Yes" : "No";
  
console.log(r);
   
   
   
/*
run:
   
Yes
   
*/

 



answered Mar 12, 2025 by avibootz
0 votes
function containsPythagoreanTriplets(arr) {
    arr.sort((a, b) => a - b); // Sort the array in ascending order

    let n = arr.length;

    // Iterate through the array to find triplets
    for (let i = 0; i < n - 5; i += 2) {
        for (let j = i + 2; j < n - 3; j += 2) {
            for (let k = j + 2; k < n - 1; k += 2) {
                let a = arr[i];
                let b = arr[j];
                let c = arr[k];
                if (a === arr[i + 1] && 
                    b === arr[j + 1] && 
                    c === arr[k + 1] && 
                    (a * a + b * b === c * c)) {
                    return true;
                }
            }
        }
    }
    
    return false;
}

let array = [6, 6, 8, 8, 10, 10]; // 6*6 + 8*8 = 10*10 // 36 + 64 = 100

if (containsPythagoreanTriplets(array)) {
    console.log("Yes");
} else {
    console.log("No");
}



/*
Run:

Yes

*/

 



answered Mar 12, 2025 by avibootz
...