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

Instant Grammar Checker - Correct all grammar errors and enhance your writing

What's The REAL Secret To First Date Success With a Woman? Click Here To Find Out

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

29,372 questions

38,322 answers

573 users

How to find all pythagorean triples (a^2 + b^2 = c^2) from an array in PHP

1 Answer

0 votes
function PrintPythagoreanTriples($arr) {
    $size = count($arr);

    for ($i = 0; $i < $size - 2; $i++) {
        for ($j = $i + 1; $j < $size - 1; $j++) {
            for ($k = $i + 2; $k < $size; $k++) {
                $a = $arr[$i];
                $b = $arr[$j];
                $c = $arr[$k];
                if ($a * $a + $b * $b == $c * $c) {
                    echo $a . " " . $b . " " . $c . "\n";
                }
            }
        }
    }
}

$arr = array(2, 3, 4, 5, 6, 7, 8, 9, 10);
        
PrintPythagoreanTriples($arr);




/*
run:

3 4 5
6 8 10

*/

 


Protect Your Privacy - Download VPN


answered Sep 21, 2022 by avibootz
...