Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
37
Task/Pythagorean-triples/PHP/pythagorean-triples.php
Normal file
37
Task/Pythagorean-triples/PHP/pythagorean-triples.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
function gcd($a, $b)
|
||||
{
|
||||
if ($a == 0)
|
||||
return $b;
|
||||
if ($b == 0)
|
||||
return $a;
|
||||
if($a == $b)
|
||||
return $a;
|
||||
if($a > $b)
|
||||
return gcd($a-$b, $b);
|
||||
return gcd($a, $b-$a);
|
||||
}
|
||||
|
||||
$pytha = 0;
|
||||
$prim = 0;
|
||||
$max_p = 100;
|
||||
|
||||
for ($a = 1; $a <= $max_p / 3; $a++) {
|
||||
$aa = $a**2;
|
||||
for ($b = $a + 1; $b < $max_p/2; $b++) {
|
||||
$bb = $b**2;
|
||||
for ($c = $b + 1; $c < $max_p/2; $c++) {
|
||||
$cc = $c**2;
|
||||
if ($aa + $bb < $cc) break;
|
||||
if ($a + $b + $c > $max_p) break;
|
||||
|
||||
if ($aa + $bb == $cc) {
|
||||
$pytha++;
|
||||
if (gcd($a, $b) == 1) $prim++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo 'Up to ' . $max_p . ', there are ' . $pytha . ' triples, of which ' . $prim . ' are primitive.';
|
||||
Loading…
Add table
Add a link
Reference in a new issue