Jul 11, 2009
Re: Write a C program
Solution in PHP, (I am loving this language 😀)
<?phprequire_once 'common.php';$sol = array();$final_sol = array();$n = 4; Factorize($n);foreach($sol as $str){ $arr = explode('+',$str); sort($arr); $final_sol[] = implode('+',$arr);}$final_sol = array_unique($final_sol);$i=0;foreach($final_sol as $str){ $i++; echo "$i)$str <br>\n";}function Factorize($n){ global $sol; if($n == 0){ $sol[] = "0"; return; } $sol[] = "0+$n"; if($n == 1){ return; } GetNaturalFactors($n);}function GetNaturalFactors($n,$prepend=""){ global $sol; if($n == 1){ $sol[] = $prepend."1"; return; } if($n == 2 ){ $sol[] = $prepend."1+1"; return; } for($i = 1; $i <= $n/2 ; $i++ ){ $sol[] = "$prepend$i+" . ($n-$i); if($i < $n-$i) GetNaturalFactors($n-$i,"$prepend$i+"); } return;}?>