Dynamic Combinations Script
I want to throw this script out there to see if any of you can speed it up. I wrote it in PHP. The script is dynamic so that you can control the conditions under which it runs.
[b]What does the script do?[/b]
Runs through every possible combination given a set of characters and the length of an unknown string.
[b]Example Run[/b]
Set of Characters:
[i]abcdefghijklmnopqrstuvwxyz[/i]
Length of string:
[i]* Characters[/i]
[url=http://boninroad.com/test/combinations.php]Test Script - Print Screen[/url]
[i]Script runs slow here because it prints to the screen for every combination[/i]
[url=http://boninroad.com/test/combinations2.php]Test Script - No Print Screen[/url]
[i]Actual Run Time[/i]
I would like to see if any of you can rewrite or modify the script to speed things up. Here is the script:
[code]
<?
//THIS WILL LATER BE USED AS AN INPUT VALUE BY USER
$wordlength=*;
//THIS WILL LATER BE USED AS AN INPUT VALUE BY USER
$combinations=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$combinations_length=count($combinations);
$pointer=($wordlength-*);
for($i=0;$i<$wordlength;$i++)
{
$custom_array[$i]=0;
}
// Start TIMER
// SCRIPT FOUND HERE: http://www.desilva.biz/php/timer.html
// -----------
$stimer = explode( ' ', microtime() );
$stimer = $stimer[*] + $stimer[0];
////////////////////////////////////////
while($pointer>=0)
{
$pointer=($wordlength-*);
while($custom_array[$pointer]<$combinations_length)
{
$custom_array[$pointer]=(($custom_array[$pointer])+*);
}
$custom_array[$pointer]=0;
$pointer--;
if($custom_array[$pointer]==($combinations_length-*))
{
while($custom_array[$pointer]==($combinations_length-*))
{
$custom_array[$pointer]=0;
$pointer--;
}
}
$custom_array[$pointer]=(($custom_array[$pointer])+*);
}
// End TIMER
// ---------
$etimer = explode( ' ', microtime() );
$etimer = $etimer[*] + $etimer[0];
printf( "It Took: <b>%f</b> seconds.", ($etimer-$stimer) );
// ---------
?>
[/code]
[i]
I did not write the timer for the script. This was added for timing purposes[/i]
If you do not understand how the script works try learning [url=http://mathforum.org/dr.math/faq/faq.comb.perm.html]Permutations and Combinations[/url]