dimkasmir Posted December 14, 2007 Share Posted December 14, 2007 Hello, I have multiple integer variables and several conditions that they have to meet. How would I make a loop that would go through all the possible values for the variables until they meet my conditions? Thanks! Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 14, 2007 Share Posted December 14, 2007 are you validating a variable or do you just need to repeat an event until you reach a certain point; if you trying to do the later - use a "for loop". Quote Link to comment Share on other sites More sharing options...
dimkasmir Posted December 14, 2007 Author Share Posted December 14, 2007 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? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 14, 2007 Share Posted December 14, 2007 what condition are you trying to find? <?php $x="5"; $y="9"; $z="11"; $addem = $x + $y + $z; if ($addem == "25") { // do something here } else { // do something else } ?> is this what your looking for? Quote Link to comment Share on other sites More sharing options...
dimkasmir Posted December 14, 2007 Author Share Posted December 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 14, 2007 Share Posted December 14, 2007 <?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"; } ?> Quote Link to comment Share on other sites More sharing options...
dimkasmir Posted December 14, 2007 Author Share Posted December 14, 2007 What I want is for the program to actually find the numbers that make the conditions true, not check if provided values are true. Thanks. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 14, 2007 Share Posted December 14, 2007 use a while loop or a do while loop. Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 14, 2007 Share Posted December 14, 2007 can you explain it better? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 14, 2007 Share Posted December 14, 2007 can you explain it better? yeah - really - I am not understanding either Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 14, 2007 Share Posted December 14, 2007 maybe for($x = 0;$x>10;$x++){ if (condition == false){ continue; }else{ //stuff here break; } } you don't understand but you keep on answering lol Quote Link to comment Share on other sites More sharing options...
dimkasmir Posted December 14, 2007 Author Share Posted December 14, 2007 I would like the formula to substite numbers between 0 and 9 for all 6 variables until they match all of hte provided conditions. For example it would first check 0 0 0 0 0 1 then 0 0 0 0 0 2 ... 0 1 1 2 0 0 etc. Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 14, 2007 Share Posted December 14, 2007 serious about that do you know how many combination you can create out of that 6 digit? Quote Link to comment Share on other sites More sharing options...
dimkasmir Posted December 14, 2007 Author Share Posted December 14, 2007 I know I know but theoretically how would you make such loop; let's say there were only 2 variables. Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 14, 2007 Share Posted December 14, 2007 $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? Quote Link to comment Share on other sites More sharing options...
dimkasmir Posted December 14, 2007 Author Share Posted December 14, 2007 But that would only be with one var, how would I have 2 or more? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 14, 2007 Share Posted December 14, 2007 you don't understand but you keep on answering lol yeah - I was trying the figure out what they was saying. Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 14, 2007 Share Posted December 14, 2007 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 ... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.