6

Possible Duplicate:
Generate all combinations of arbitrary alphabet up to arbitrary length

I'm trying to make write all possible words of 10 letters(zzzzzzzzzz) in php. How can I do that? it will look like that : https://i.sstatic.net/xC381.png

I tried some ways to it but they are only making 10 letters randomly not increasing from 1 letter. By the way execution time and how it's big is not problem. i just need to algorithm for it, if somebody show it with code it'll be more helpful of course..

9
  • 12
    26^10 possibilities = 141 167 095 653 376, so it might take a while. Commented Sep 1, 2011 at 12:38
  • @NullUserException it's not problem. Commented Sep 1, 2011 at 12:40
  • 3
    Re Tim's comment that it might take a while: Allowing one millisecond per string, I estimate that the program will take about 4000 years to run. (In reality, it would actually be a lot longer than that). Commented Sep 1, 2011 at 12:46
  • 4
    Also, if you wanted to output the results to disk, allowing 10 bytes per string, you would need 12 terabytes of storage. Again, in reality it would be more because you'd need some kind of structure to the file, but I'm trying to point out the scale of the question you've asked. Commented Sep 1, 2011 at 12:50
  • 1

3 Answers 3

7
function words($length, $prefix='') {
    if ($length == 0) return;
    foreach(range('a', 'z') as $letter) {
        echo $prefix . $letter, "\n";
        words($length-1, $prefix . $letter);
    }
}

Usage:

words(10);

Try it here: http://codepad.org/zdTGLtjY (with words up to 3 letters)

Sign up to request clarification or add additional context in comments.

1 Comment

I like the word in row 16549 :D
6

Version 1:

for($s = 'a'; $s <= 'zzzzzzzzzz'; print $s++.PHP_EOL);

as noted by Paul in comments below, this will only go to zzzzzzzzyz. A bit slower (if anyone cares) but correct version would be:

//modified to include arnaud576875's method of checking exit condition
for($s = 'a'; !isset($s[10]); print $s++.PHP_EOL);

4 Comments

impressive.. that's really performanceful. @arnaud576875's answer is function in function. this is really simple and performanceful answer, thank you Mchl..
According to OP's question, $s = 'aaaaaaaaaa' is probably what he wants. +1 Nonetheless for the actual best answer.
Neither of these do what you might expect. zz gives a list that goes to zyz and aaaaaaaaaa gives only a.
The second one 4 times faster with !isset($s[10]) instead of strlen($s) <= 10 (if anyone cares)
0
 <?php

function makeWord($length, $prefix='')
{
   if ($length <= 0)
   {
      echo $prefix . "\n";
      return;
   }

   foreach(range('a', 'z') as $letter)
   {
      makeWord($length - 1, $prefix . $letter);
   }
}    

// Use the function to write the words.
$minSize = 1;
$maxSize = 3;

for ($i = $minSize; $i <= $maxSize; $i++)
{
   makeWord($i);
}

?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.