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,995 questions

51,940 answers

573 users

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

3 Answers

0 votes
function containsPythagoreanTripletNumbers($arr) { 
    $len = count($arr);
    
    for ($i = 0; $i < $len; $i++) { 
        for ($j = $i + 1; $j < $len; $j++) { 
            for ($k = $j + 1; $k < $len; $k++) { 
                $a = $arr[$i] * $arr[$i]; 
                $b = $arr[$j] * $arr[$j]; 
                $c = $arr[$k] * $arr[$k]; 
      
                if ($a == $b + $c or
                    $b == $a + $c or 
                    $c == $a + $b) 
                    return true; 
            } 
        } 
    } 

    return false; 
} 

$arr = array(4, 7, 3, 1, 5); // 3*3 + 4*4 = 5*5 // 9 + 16 = 25

if (containsPythagoreanTripletNumbers($arr)) 
    echo "Yes"; 
else
    echo "No"; 
    
    

/*
run:

Yes

*/

 



answered Nov 11, 2020 by avibootz
0 votes
function containsPythagoreanTriplets($arr) {
    sort($arr);

    // Iterate through the array to find triplets
    $n = count($arr);
    for ($i = 0; $i < $n - 5; $i += 2) {
        for ($j = $i + 2; $j < $n - 3; $j += 2) {
            for ($k = $j + 2; $k < $n - 1; $k += 2) {
                $a = $arr[$i];
                $b = $arr[$j];
                $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;
}

$array = [3, 3, 4, 4, 5, 5]; // 3*3 + 4*4 = 5*5 // 9 + 16 = 25

if (containsPythagoreanTriplets($array)) {
    echo "Yes"; 
}
else {
    echo "No"; 
}

  
/*
run:
  
yes
   
*/

 



answered Mar 12, 2025 by avibootz
edited Mar 12, 2025 by avibootz
0 votes
function containsPythagoreanTripletNumbers($arr) {
    $len = count($arr);
    // Square all elements and sort the array
    for ($i = 0; $i < $len; $i++) {
        $arr[$i] = $arr[$i] * $arr[$i];
    }
    sort($arr);

    // Check for the triplet
    for ($i = $len - 1; $i >= 2; $i--) {
        $a = 0;
        $b = $i - 1;

        while ($a < $b) {
            if ($arr[$a] + $arr[$b] == $arr[$i]) {
                return true; // Triplet found
            } elseif ($arr[$a] + $arr[$b] < $arr[$i]) {
                $a++;
            } else {
                $b--;
            }
        }
    }

    return false; // No triplet found
}

$array = [4, 7, 3, 1, 5]; // 3*3 + 4*4 = 5*5 // 9 + 16 = 25

if (containsPythagoreanTripletNumbers($array)) {
    echo "The array contains a Pythagorean triplet.";
} else {
    echo "The array does not contain a Pythagorean triplet.";
}



/*
run:

The array contains a Pythagorean triplet.

*/

 



answered Jul 25, 2025 by avibootz
...