Jump to content

Help with listing combinations using for loop


shamuraq

Recommended Posts

Hi all,

I'm trying to list the possible combinations in fraction from 1/1 + 1/1 until 9/9 + 9/9. So i came up with this

<?php
//numbers of combination for w/x + y/z
$num1=1;
$den1=1;
$num2=1;
$den2=1;
$row=1;
for($w=0; $w<9; $w++){
echo $row.') ';
$row=$row+1;
echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
$num1=$num1+1;

for($x=0; $x<9; $x++){
	echo $row.') ';
	$row=$row+1;
	echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
	$den1=$den1+1;

	for($y=0; $y<9; $y++){
		echo $row.') ';
		$row=$row+1;
		echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
		$num2=$num2+1;

		for($z=0; $z<9; $z++){
			echo $row.') ';
			$row=$row+1;
			echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
			$den2=$den2+1;
		}
	}
}
}
?>

The problem is it parse all the way to

7380) 10/82 + 730/6561.

Where did it go wrong? Thanx in advance...

just do

<?php
//numbers of combination for w/x + y/z
$num1=1;
$den1=1;
$num2=1;
$den2=1;
$row=1;
for($num1=1; $num1<=9; $num1++){
for($den1=1; $den1<=9; $den1++){
	for($num2=1; $num2<=9; $num2++){
		for($den2=1; $den2<=9; $den2++){
			echo $row.'. ) ';
			$row=$row+1;
			echo $num1.'/'.$den1.' + '.$num2.'/'.$den2.'<br>';
		}
	}
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.