hi,
i want permutation for all alphabets with word length is 8.but i get only permutation for 4.pls help me to debug this issue

code:

<?PHP

FUNCTION permutations($letters,$num){ 
$last = STR_REPEAT($letters{0},$num); 
$result = ARRAY(); 
WHILE($last != STR_REPEAT(lastchar($letters),$num)){ 
echo "$last";
$result[] = $last; 
$last = char_add($letters,$last,$num-1); 
} 
$result[] = $last; 
RETURN $result; 
} 
FUNCTION char_add($digits,$string,$char){ 
IF($string{$char} <> lastchar($digits)){ 
$string{$char} = $digits{STRPOS($digits,$string{$char})+1}; 
RETURN $string; 
}ELSE{ 
$string = changeall($string,$digits{0},$char); 
RETURN char_add($digits,$string,$char-1); 
} 
} 
FUNCTION lastchar($string){ 
RETURN $string{STRLEN($string)-1}; 
} 
FUNCTION changeall($string,$char,$start = 0,$end = 0){ 
IF($end == 0) $end = STRLEN($string)-1; 
FOR($i=$start;$i<=$end;$i++){ 
$string{$i} = $char; 
} 
RETURN $string; 
} 
?> 




<?php
for($j=1;$j<9;$j++){
$Array=permutations("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$j); 
FOR($i=0 ; $i < COUNT($Array) ; $i++) { 
ECHO "$i." . $Array[$i] . "<BR>"; 
} 
}
?> 

output:

i want a output like this
a
b
c
d
e
f
.
.
.
.
.
z
aa
ab
ac
ad
.
.
.
.
.
.
az
.
.
.
.
.
.
.
.
.
.
zzzzzzzz

But the above code shown like this
a
b
c
d
.
.
.
.
.
aa
ab
ac
.
.
.
.
az
.
.
.
zzzz

yes

Dani AI

Generated

— quick diagnosis: trying to generate and store every string of length up to 8 over a 26-letter alphabet is infeasible. 26^8 = 208,827,064,576 and the total for lengths 1..8 is 217,180,147,158 strings ((26^(8+1)-26)/25). That will exhaust memory, hit PHP’s max execution time, or fill any disk you write to. The reason you see results only to length 4 is almost certainly a resource limit (memory or execution time) or the script being killed; check your error log for “Allowed memory size exhausted” or “Maximum execution time exceeded.”

Main fixes and practical options:

  • Don’t collect everything into an array. Stream output (echo/write) or use a generator so you never keep all strings in memory.
  • Re-check whether you really need every string. If you only need counts, ranks, or to map a single index to its string, use base-26 math instead of enumeration.
  • For testing, run with very small lengths (1–3) and measure time/memory before scaling.

Example (safe pattern): a PHP generator that emits each k-length string without storing them all (requires PHP 5.5+):

<?php
function gen_strings($alphabet, $len) {
    $n = strlen($alphabet);
    $idx = array_fill(0, $len, 0);
    while (true) {
        $s = '';
        for ($i = 0; $i < $len; $i++) $s .= $alphabet[$idx[$i]];
        yield $s;
        $pos = $len - 1;
        while ($pos >= 0) {
            if (++$idx[$pos] < $n) break;
            $idx[$pos--] = 0;
        }
        if ($pos < 0) break;
    }
}
foreach (gen_strings('abcdefghijklmnopqrstuvwxyz', 3) as $str) echo $str, PHP_EOL;

Python (concise, but same caveat: don’t run for large lengths):

import itertools
for p in itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=3):
    print(''.join(p))

As hinted, look at streaming/iterator approaches rather than building giant arrays. If you need help for a specific, smaller task (e.g., generate a subset, map index->string, or resume generation), state that and an example can be provided.

Have a look at this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.