Just enter any 6 numbers , How many different combinations can they make?
for example, I input 3numbers "1\3\2",there could be "123 132 213 231 312 321"
How about "6" numbers ,Could you help me ?
A question about permutations and combinations.
Moderator: DosItHelp
Re: A question about permutations and combinations.
penpen wrote:The answer is 6! (=720); for further information see:
https://en.wikipedia.org/wiki/Factorial.
penpen
Sorry, I did't have a clear description about my question,in fact, I want to use batch to list the all combination of the 6 numbers
Re: A question about permutations and combinations.
Thanks, but that's too slow,when I type 123456, it takes about one minute.
I worte some code, but the function is not strong, it needs to enter with space, and can not rule out duplicate items.
The advantage is more faster.I type “1 2 3 4 5 6”,it takes about 8 seconds.
Code: Select all
@echo off
setlocal enabledelayedexpansion
echo;input some numbers (separated by spaces)
set /p str=
echo;&set u=%time%
for %%i in (!str!) do set/a _n+=1&set !_n!=%%i
call:loop
echo;&echo;There are [%mn%] permutations
call :usetime "%u%" "%time%" u_t
echo;%u_t%
pause&exit
:loop
set/a _m+=1
if %_m% leq %_n% for /l %%i in (1 1 %_n%) do (
for %%j in (%~1) do set .%%j=%%j
if not defined .%%i call :loop "%~1 %%i"
for /l %%k in (1 1 %_n%) do set .%%k=
) else (
for /l %%l in (1 1 %_n%) do set .%%l=
for %%m in (%~1) do set/p=!%%m! <nul
echo;
set/a mn+=1
)
set/a _m-=1
goto :eof
:usetime
setlocal&set /a n=0
for /f "tokens=1-8 delims=.: " %%a in ("%~1:%~2") do (
set /a n+=10%%a%%100*360000+10%%b%%100*6000+10%%c%%100*100+10%%d%%100
set /a n-=10%%e%%100*360000+10%%f%%100*6000+10%%g%%100*100+10%%h%%100)
set /a s=n/360000,n=n%%360000,f=n/6000,n=n%%6000,m=n/100,n=n%%100
set "u_t=%s%_Hr %f%_Min %m%_Sec %n%_Ms"
endlocal&set %~3=%u_t:-=%&goto :eof
Re: A question about permutations and combinations.
If you want the program to work as fast as possible, then you may want to create a macro (== command pre built in environment variable);
here is an example ("listPermuation.bat") with no input functionality:
Result:
penpen
here is an example ("listPermuation.bat") with no input functionality:
Code: Select all
@echo off
setlocal enableExtensions enabledelayedexpansion
set "list[0]= a b c d e f"
:: compute N and the list of single variables used
set "N=0"
for %%a in (%list[0]%) do (
set /a "N+=1"
set /a "dec[!N!]=N-1"
set "var[!N!]=%%~a"
)
:: build for loop that outputs all permutations
set "for="
for /l %%a in (1, 1, %N%) do if not %%~a == %N% (
set "for=!for!for %%!var[%%~a]! in (^!list[!dec[%%~a]!]^!) do ( set "list[%%~a]=^^^!list[!dec[%%~a]!]: %%~!var[%%~a]!=^^^!" & "
) else (
set "for=!for!for %%!var[%%~a]! in (^!list[!dec[%%~a]!]^!) do ( "
)
set "for=!for!echo(!list[0]: = %%~!"
for /l %%a in (1, 1, %N%) do set "for=!for!) "
::execute
>con echo(%time%
%for%
>con echo(%time%
endlocal
goto :eof
Result:
Code: Select all
Z:\>listPermuation.bat
18:54:20,80
18:54:20,80
a b c d e f
...
f e d c b a
18:54:21,69
Z:\>listPermuation.bat >"test.txt"
18:54:28,46
18:54:28,46
18:54:28,56
Z:\>findstr /n "^" "test.txt"
1: a b c d e f
...
720: f e d c b a
Z:\>
penpen
Re: A question about permutations and combinations.
penpen wrote:If you want the program to work as fast as possible, then you may want to create a macro (== command pre built in environment variable);
here is an example ("listPermuation.bat") with no input functionality:
Thank you very much,Penpen. You've done me a great favor !