I had a problem before with a small lottery program that calucaltes all the combinations possible for 6/49 (in PHP).
This is my current problem. I use conditions so the numbers won't repeat, but in the next loop, when the first number is changed I have a repeating combination. Let me explain by an example:
I have this combination:
1 2 3 4
1 2 3 5
1 2 3 6
...
1 2 4 3
1 2 4 5
1 2 4 6
So my lottery ticket 1 2 3 4 is the same as 1 2 4 3.
Have any ideas how to solve it? I can't think of anything...
Here is the code (I only made it till 4/49 :D )
<?php
for ($i=1 ; $i<50 ; $i++)
{
$a=$i;
for ($j=1 ; $j<50 ; $j++)
{
if ($i!=$j)
{
$b=$j;
for ($k=1 ; $k<50 ; $k++)
{
if ($k!=$j && $k!=$i)
{
$c=$k;
for ($l=1 ; $l<50 ; $l++)
{
if ($l!=$i && $l!=$j && $l!=$k)
{
$d=$l;
echo "$a $b $c $d <br>";
}
}
}
}
}
}
}
?>
<br/><br/>
Thank you !
Update:
the code looks like this:
< ?php
for($a=1; $a<50; $a++)
{
for($b=$a+1; $b<50; $b++)
{
for($c=$b+1; $c<50; $c++)
{
for($d=$c+1; $d<50; $d++)
{
for($e=$d+1; $e<50; $e++)
{
for($f=$e+1; $f<50; $f++)
{
$t=$t+1;
}
}
}
}
}
}
echo "$t";
?>
I do get the right answer but I allso get this error: "Undefined variable: t" Any ideea why?
To get all combinations, you should initialize inner loop variable value to parent loop variable value + 1 Example for 4/49
for($a=1; $a<50-3; $a++)
for($b=$a+1; $b<50-2; $b++)
for($c=$b+1; $c<50-1; $c++)
for($d=$c+1; $d<50; $d++)
echo "$a $b $c $d<br>";
As a bonus, you don't need anymore to test if values are all different
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With