-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdouble_squares.php
38 lines (37 loc) · 899 Bytes
/
double_squares.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?
// still a little messed up...
function find_squares($num) {
$ct=0;
$max=sqrt($num)+1;
$i=0;
$squares = array();
while ($i <= $max) {
$remainder=$num-($i*$i);
if (sqrt($remainder) == (int)sqrt($remainder)) {
// echo "for $num - $i " . sqrt($remainder) . "\n";
$found = false;
for ($j = 0; $j < count($squares); $j++) {
if ($i == $squares[$j] || sqrt($remainder) == $squares[$j]) {
$found = true;
break;
}
}
if (!$found) {
$ct++;
array_push($squares, $i);
array_push($squares, sqrt($remainder));
}
}
$i++;
}
return $ct;
}
$fh = fopen($argv[1], "r");
while (!feof($fh)) {
$test = trim(fgets($fh));
if ($test != "") {
echo find_squares($test) . "\n";
}
}
fclose($fh);
?>