Project Euler Solutions by Ross Marks

<?php
/*****************************
 * ProjectEuler - Problem 33
 * By Ross Marks
 *****************************
 * The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
 * 
 * We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
 * 
 * There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
 * 
 * If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
 ****************************/

$awns 0;
$products = array();

$maina 1;
$mainb 1;

//if(simplify(49,98) == simplify(4, 8))
//    echo "here";
            
for($i 11$i <= 99$i++){
    for(
$j 11$j <= 99$j++){

        
$check 0// check if any no's match
        
if(strval($i)[0] == strval($j)[1]){
            
$check 1;
            
$i2 strval($i)[1];
            
$j2 strval($j)[0];
        } 

        if(
strval($j)[0] == strval($i)[1]){
            
$check 1;
            
$i2 strval($j)[1];
            
$j2 strval($j)[0];
        }

        if(
strval($j)[0] == strval($i)[0]) // remove no's like 11, 22, 33 etc.
            
$check 0;

        if(
$check == 1){ // should be checked
            
if((int)$i %10 == || (int)$j %10 == 0){ // neither no's mod 10
                
$check 0
            }
        }

        if(
$check == 1){
            if(
simplify($j,$i) == simplify($j2$i2)){
                echo 
"$j / $i - $j2 / $i2 \n";
                
$maina bcmul($maina,$j);
                
$mainb bcmul($mainb,$i);
            }
        }         
        
    }
}

$main_simp simplify($maina$mainb);
echo 
"$maina / $mainb = $main_simp[0] / $main_simp[1]\n";
echo 
"Answer: $main_simp[1]";

// simplify a fraction
function simplify($num,$den) {
    
$g gcd($num,$den);
    
//echo "denominator: $g - ";
    
return Array($num/$g,$den/$g);
}

// get common denominator of a fraction
function gcd($a,$b) {
    
$a abs($a); $b abs($b);
    if( 
$a $b) list($b,$a) = Array($a,$b);
    if( 
$b == 0) return $a;
    
$r $a $b;
    while(
$r 0) {
        
$a $b;
        
$b $r;
        
$r $a $b;
    }
    return 
$b;
}

?>