/
cof.php
70 lines (61 loc) · 1.29 KB
/
cof.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* check if GMP extension is installed
*/
if( !function_exists ( "gmp_gcd" )){
echo "GMP function missing! Please enable php gmp extension first!\n";
exit();
}
/**
* include the library
*/
require_once('src/CommonOddFactor.php');
/**
* Start the script
*/
echo "Please enter two odd numbers to find the commond factories, separated by space. For example 12345 8873.\n";
/**
* Get the command line input
*/
$handle = fopen ("php://stdin","r");
$line = trim(fgets($handle));
$numbers = explode( " ", $line);
/**
* Check if all the numbers are odd
*/
checkIsOdd( $numbers );
/**
* initial main object and call the method
*/
$obj = new CommonOddFactor( $numbers[0], $numbers[1] );
$ary = $obj->getAllCommonFactors();
/**
* Echo out the result
*/
echo "The common factories are: \n";
foreach ($ary as $n){
echo $n."\n";
}
/**
* utility functions: check both numbers
*/
function checkIsOdd( $ary ){
foreach ( $ary as $n ){
if( !isOdd( $n )){
echo "One of the number you entered is not an valid odd number!\n";
echo "Please restart and try again.\n\n";
exit();
}
}
}
/**
* check if the number is an odd number
*/
function isOdd( $n )
{
if ($n & 1 ){
return true;
}else{
return false;
}
}