View Full Version : How to get all possible combinations?
p0wered
03-07-2003, 01:08 AM
I want to try something different with php, and by this I want to apply it to a word problem.
How would I go about getting the possible combinations of 1,2,3,4,5 in possible orders, here are some possible orders, and examples of wrong orders.
2,3,4,5,1 = right
5,4,3,2,1 = right
1,3,2,5,4 = wrong
4,5,6,1,2 = right
It has to follow in sequence. How would i go about getting php to output all possible combinations in that way, or if not all possible combinations in total without the right or wrong? I know for a fact there are some 120 possible combinations, of which only 70 some are able to follow in sequence.
Thanks for your help, I want to test this to get me ready for programing in college.
p0wered
03-08-2003, 02:23 PM
*bump*
after few days :pirate:
try php.net because i can't think of anything that'd do this OR if your in school, go to your math teacher and ask them if there's a way to do this then do that in php
p0wered
03-08-2003, 07:45 PM
Its for college TEE Web Programming :p
filburt1
03-08-2003, 08:12 PM
Writing something now.
filburt1
03-08-2003, 08:34 PM
Enjoy my numerous cheap hacks and crazy boolean mixes:
p0wered
03-09-2003, 02:28 AM
Hey thats cool,
Verifying sequence of 5 numbers...
Forwards: passed
Backwards: failed
Sequence passed
Is there something wrong? How would I modify the script to show all possible occurances.
filburt1
03-09-2003, 03:17 PM
I don't know. Right now all it does is verify existing combinations.
Dean C
03-09-2003, 05:25 PM
You have too much time on your hands filburt. That is one nice script :)
- miSt
filburt1
03-09-2003, 09:00 PM
Before I noticed that it had to check forwards and backwards, I had it actually highlighting the point in the sequence where it went wrong. :)
p0wered
03-09-2003, 10:40 PM
This is a great start, I am going to be learning c and vb I would think, this can be easily accomplished with those languages then a web programming language.
Thanks for your help Filb, you got me going in the right direction.
filburt1
03-09-2003, 10:42 PM
Glad to help. My suggestion is just reverse the process of checkforwards() and checkbackwards() to generate sequences instead of validating them. Either way you can see the logic in the if statement of the for loops in both functions.
p0wered
03-09-2003, 10:50 PM
How exactly would I reverse the proccess of testforwards and testbackwards?
I am a bit ne to php I know the basics, but your coding is way to advances :)
Thanks again!
filburt1
03-09-2003, 10:52 PM
(side note: there are technically infinite permutations; you need to say what integers can be used and how many of them)
The core logic behind the whole thing is this line:
if ($sequence[$i] != $smallest and $sequence[$i] != $sequence[$i - 1] + 1) return false;
Translated:
If the current element in the sequence is not the smallest element and it is not equal to the (previous item in the sequence plus one), then the sequence is invalid.
p0wered
03-09-2003, 11:03 PM
Ah I see.
So how do I reverse this to output all sequences?
Sorry, it seems I am a php newbie, just know all basics and mysql basics.
filburt1
03-09-2003, 11:36 PM
SOrry but I don't have to time to rewrite the script.
p0wered
03-10-2003, 12:11 AM
Oh, I wasn't asking to rewrite :) What you have done is so much, and I appreciate the time you took. When anyone has the time, if they can tell me which line to edit, and how to edit it I will do the rest.
Thanks again.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.