Permutations: Difference between revisions
Content added Content deleted
Line 3,616: | Line 3,616: | ||
479001600 //= 12! |
479001600 //= 12! |
||
00:00:01.328</pre> |
00:00:01.328</pre> |
||
=={{header|PHP}}== |
|||
Full non-recursive algorithm generating permutation in reversed lexicographical order. |
|||
Taken from here: [https://habrahabr.ru/post/275331/]. It's a little bit modified algorythm of Narayana Pandit [https://en.wikipedia.org/wiki/Narayana_Pandit]. It reveres a string and works until a and b are not equal. It searches |
|||
a new permutation directly (first internal cycle) as we do it on a sheet of paper. You can use English alphabet instead of digits. Author: I. Gavryushin aka dcc0. |
|||
<lang php> |
|||
<?php |
|||
$b="0123"; |
|||
$a=strrev($b); |
|||
while ($a !=$b) { |
|||
$i=1; |
|||
while($a[$i] > $a[$i-1]) { |
|||
$i++; |
|||
} |
|||
$j=0; |
|||
while($a[$j] < $a[$i]) { |
|||
$j++; |
|||
} |
|||
$c=$a[$j]; |
|||
$a[$j]=$a[$i]; |
|||
$a[$i]=$c; |
|||
$a=strrev(substr($a, 0, $i)).substr($a, $i); |
|||
print $a. "\n"; |
|||
} |
|||
?> |
|||
</lang> |
|||
'''Output''' |
|||
<pre> |
|||
3210 |
|||
2310 |
|||
3120 |
|||
1320 |
|||
2130 |
|||
1230 |
|||
3201 |
|||
2301 |
|||
3021 |
|||
0321 |
|||
2031 |
|||
0231 |
|||
3102 |
|||
1302 |
|||
3012 |
|||
0312 |
|||
1032 |
|||
0132 |
|||
2103 |
|||
1203 |
|||
2013 |
|||
0213 |
|||
1023 |
|||
0123 |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |