Jump to content

How to make this loop?


dimkasmir

Recommended Posts

I don't think a for loop would work because I need the program to go through all possible combinations in multiple variables. For example, I have variables $X $Y and $Z. I know that all of them are less than 9 and $X > $Y $Y > $Z and $X + $Y + $Z = 25. How would I make a loop that tried all the possible combinations of x y and z until they match the conditions?

Link to comment
https://forums.phpfreaks.com/topic/81611-how-to-make-this-loop/#findComment-414548
Share on other sites

There are quite a few conditions:

 

(($A + $B + $C) * ($A + $B + $C) == 100 * $X + 10 * $Y + $Z) &&

(($X + $Y + $Z) * ($X + $Y + $Z) != 100 * $A + 10 * $B + $C) &&

((100 * $A + 10 * $B + $C) - ($A + $B + $C) = ($X + $Y + $Z) * ($X + $Y + $Z)) &&

((100 * $X + 10 * $Y + $Z) >= (100 * $A + 10 * $B + $C)) &&

((100 * $X + 10 * $Y + $Z) <= 2 * (100 * $X + 10 * $Y + $Z))

&& ($A => 0) && ($A =< 9

&& ($B => 0) && ($B =< 9

&& ($C => 0) && ($C =< 9

&& ($X => 0) && ($X =< 9

&& ($Y => 0) && ($Y =< 9

&& ($Z => 0) && ($Z =< 9)

 

I want the program to find all 6 variables so that they match these.

Link to comment
https://forums.phpfreaks.com/topic/81611-how-to-make-this-loop/#findComment-414553
Share on other sites

<?php

if (($A => 0) && ($A =< 9))
{

}
else if (($B => 0) && ($B =< 9))
{

}
else if (($C => 0) && ($C =< 9)) 
{

}
else if (($X => 0) && ($X =< 9))
{

}
else if (($Y => 0) && ($Y =< 9))
{

}
else if (($Z => 0) && ($Z =< 9))
{

}
else
{
echo "No Conditions Have Been Meet\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/81611-how-to-make-this-loop/#findComment-414565
Share on other sites

$test = true;
while($test == true){
$x = str_split(rand(5, 999999));
//create formula here note you note $x will contain an array that has rondom number on given lenght 
if (result form formaula == true){
	$test = false
	$result= yoursesult;
}else{
       $test = true;
     }
}

 

maybe...

 

where are those var coming from?

Link to comment
https://forums.phpfreaks.com/topic/81611-how-to-make-this-loop/#findComment-414586
Share on other sites

while($test == true){
$numbers = array($va1,$var2);
array_rand($numbers);
//create formula here note you note $numbers will contain an array that has rondom number on given lenght 
if (result form formaula == true){
	$test = false
	$result= yoursesult;
}
}

 

maybe ...

Link to comment
https://forums.phpfreaks.com/topic/81611-how-to-make-this-loop/#findComment-414597
Share on other sites

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.