Asheeown Posted June 1, 2007 Share Posted June 1, 2007 I have 5 categories of numbers, I need one number from each category and the sum of those 5 numbers has to be 484, what function(s) would I use for this? It has 19,600 possibilities. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/ Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 Can you post some examples of the input? Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266368 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 67 88 109 61 147 134 165 100 118 106 46 50 70 55 37 82 94 103 97 155 39 79 54 112 43 190 52 138 115 146 150 85 130 81 124 73 127 Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266374 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 Do you have any code started, as you will need to create your own algorithm... Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266384 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 No, I have an idea, to run 1 number in the first through every single one, but that would be sloppy and certainly would take a bunch of statements, I was just looking for functions to help with the process of process and elimination. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266389 Share on other sites More sharing options...
taith Posted June 1, 2007 Share Posted June 1, 2007 i can garantee your parser engine wont like you very much... but how bout somin like this? $array1=array("67","134","46","82","39"); $array2=array("88","165","50","79","190","146"); $array3=array("109","100","70","103","54","52","150","81"); $array4=array("61","118","55","97","112","138","85","124","73","127"); $array5=array("147","106","37","155","43","115","130"); $numbers=array(); while($all!=484 && count($numbers)!=5){ if(count($numbers)==0){ $numbers[]=$array1[array_rand($array1)]; $all+=$array1[array_rand($array1)]; }elseif(count($numbers)==1){ $numbers[]=$array2[array_rand($array2)]; $all+=$array2[array_rand($array2)]; }elseif(count($numbers)==2){ $numbers[]=$array3[array_rand($array3)]; $all+=$array3[array_rand($array3)]; }elseif(count($numbers)==4){ $numbers[]=$array4[array_rand($array4)]; $all+=$array4[array_rand($array4)]; }elseif(count($numbers)==5){ $numbers[]=$array5[array_rand($array5)]; $all+=$array5[array_rand($array5)]; } if(count($numbers)==5 && $all!=484){ $numbers=array(); $all=0; } } not tested... Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266392 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 I'm looking over it, it is what I'm looking for but their is an error, I got the white screen but my error reporting doesn't work (don't ask) Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266397 Share on other sites More sharing options...
taith Posted June 1, 2007 Share Posted June 1, 2007 did you print_r($numbers);? whats the error? Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266400 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 Its a tough one man. Definitely a brain strain. Because you would first need to start with the first number in the first column, match that up against all possibilities for the other 5 categories and check the sums. Unfortunately I do not think there is anything that will help you here really. I am not sure if recursion would be right or not. How I envision it is you have a master array, which houses the 5 categories, the first category is the main category to do the checks against. You would have to loop through the first category checking each possibility of the other 4 categories against it. I am not sure if recursion would help out, or if you want the foreach or the for statement. I try and do some code but for now it is not clicking if I come up with it I will be sure to post it but yea, this is a pretty intense code you want (at least with the knowledge I have). Taith's example seems like it would work, but wow that could take a long ass time, but yea if I think of a better way I will sure post it. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266401 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 It only took about 20-30 seconds, it's not bad, I can kick this server's ass it's okay, and taith yes I did try and print the array and no error comes up but thats just my stupid setup, but I know it hit an error cause I put some html writing below and it would have shown that even if the php failed. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266406 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 Yea, it only took that long for the results you had, but the time will also vary every time you retry the code with the random array part, potentially there is a small line that this could run for 10 minutes given how many results etc. as it is all randomized. Anyhow it works, I am still going to try and rack my brain over something like that, but more efficient for future use, it will be an interesting challenge. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266409 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 It's only 19,600 possible chances, and the random rate would probably only put it over 100,000 or so, and thats not that much, 30 seconds sounds about right, however I have no returned results. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266413 Share on other sites More sharing options...
taith Posted June 1, 2007 Share Posted June 1, 2007 that is of course assuming it doesnt try the same combination twice... lol Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266415 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 Thats what I was thinking and one solution I was thinking is, when it finds a combo and it doesn't equal the number then just add it to a blacklist, and have the array check double check with the blacklist, this would confine it to the right number of possibilities. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266417 Share on other sites More sharing options...
taith Posted June 1, 2007 Share Posted June 1, 2007 yes... however, since it grabs them sequentially, it'd be next to impossible to blacklist em... Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266419 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 $array1=array("67","134","46","82","39"); $array2=array("88","165","50","79","190","146"); $array3=array("109","100","70","103","54","52","150","81"); $array4=array("61","118","55","97","112","138","85","124","73","127"); $array5=array("147","106","37","155","43","115","130"); $numbers=array(); while($all!=484 && count($numbers)!=5){ if(count($numbers)==0){ $numbers[]=$array1[array_rand($array1)]; $all+=$array1[array_rand($array1)]; }elseif(count($numbers)==1){ $numbers[]=$array2[array_rand($array2)]; $all+=$array2[array_rand($array2)]; }elseif(count($numbers)==2){ $numbers[]=$array3[array_rand($array3)]; $all+=$array3[array_rand($array3)]; }elseif(count($numbers)==3){ $numbers[]=$array4[array_rand($array4)]; $all+=$array4[array_rand($array4)]; }elseif(count($numbers)==4){ $numbers[]=$array5[array_rand($array5)]; $all+=$array5[array_rand($array5)]; } if(count($numbers)==5 && $all!=484){ $numbers=array(); $all=0; } } Fixed, changed the count($numbers)==4,5 supposed to be 3,4 Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266421 Share on other sites More sharing options...
taith Posted June 1, 2007 Share Posted June 1, 2007 unless... if you grab all 5 variables at the same time <?php $array1=array("67","134","46","82","39"); $array2=array("88","165","50","79","190","146"); $array3=array("109","100","70","103","54","52","150","81"); $array4=array("61","118","55","97","112","138","85","124","73","127"); $array5=array("147","106","37","155","43","115","130"); while($all!=484){ $numbers[1]=$array1[array_rand($array1)]; $numbers[2]=$array2[array_rand($array2)]; $numbers[3]=$array3[array_rand($array3)]; $numbers[4]=$array4[array_rand($array4)]; $numbers[5]=$array5[array_rand($array5)]; $all=array_sum($numbers); } print_r($numbers); ?> that'll prolly speed it up a bit...? Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266426 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 I have it on 900 second time-out, hasn't found it yet. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266428 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 Done, I had one number missing and it screwed it all up, it gets a different answer every time, which brings me to this, can it put all those answers in a variable for the end result, just so we know all the possibilities? Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266431 Share on other sites More sharing options...
taith Posted June 1, 2007 Share Posted June 1, 2007 as stated before... since it is on a random based principal... it may time out before it finds it... i uploaded the same on my server... finds in FAR less then a second... (if you want to see http://www.divinedesigns.ca/test/) Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266433 Share on other sites More sharing options...
Barand Posted June 1, 2007 Share Posted June 1, 2007 0.013 seconds <?php $time1 = microtime(true); $a1=array("67","134","46","82","39"); $a2=array("88","165","50","79","190","146"); $a3=array("109","100","70","103","54","52","150","81"); $a4=array("61","118","55","97","112","138","85","124","73","127"); $a5=array("147","106","37","155","43","115","130"); $t1=count($a1); $t2=count($a2); $t3=count($a3); $t4=count($a4); for ($k1=0 ; $k1<$t1; $k1++) { for ($k2=0 ; $k2<$t2; $k2++) { for ($k3=0; $k3<$t3; $k3++) { for ($k4=0; $k4<$t4; $k4++) { $tot = $a4[$k4]+$a3[$k3]+$a2[$k2]+$a1[$k1]; $rem = 484 - $tot; if (in_array($rem, $a5)) echo "$a1[$k1], $a2[$k2], $a3[$k3], $a4[$k4], $rem <br>"; } } } } $time2 = microtime(true); echo '<br>Finished', ' in ', $time2-$time1, 'sec'; ?> EDIT: Can save a further 3 millisec by looping through a5 instead of a4 and testing if rem value is in a4, as a4 gives more iterations than a5. But who cares? Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266459 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 Damn, Barand beat me to it. Very nicely done, my brain is having a huge fart today. Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266461 Share on other sites More sharing options...
taith Posted June 1, 2007 Share Posted June 1, 2007 you'd need to shuffle the arrays... as is... it only grabs the first matching pair... Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266463 Share on other sites More sharing options...
Asheeown Posted June 1, 2007 Author Share Posted June 1, 2007 You guys have been an amazing help and it's going fast as anything, thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/53877-solved-probablity/#findComment-266469 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.