News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

permutations

Started by 0x58, January 30, 2012, 03:13:09 PM

Previous topic - Next topic

0x58

hi all :) i did a lot of googling but didn't find any good answers ::) so i'm wondering if there is a way to get all the possible lexicographic permutations of a given object either a number or a word. for exemple we have 0, 1 and 2, it's possible lexicographic permutations are: 012, 021, 102, 120, 201,210

thank's for your help :)

z941998

Google for Combinatorial Algorithms - For computers and calculators.

You will find a few for Computer Science and Applied Mathmatics - Editor: Werner Rheinboldt from University of Maryland.

Look for the pdfs and you will have all you need.  He also has a book (the pdf).

Welcome on board.

bozo

Here's a rough idea.

comment ~

      000
      001
      002
      010
      011
      012
      020
      021
      022
      100
      101
      102
      110
      111
      112
      120
      121
      122
      200
      201
      202
      210
      211
      212
      220
      221
      222

~

.686
.model flat,stdcall

extern printf :proc
extern exit   :proc

WORD_LEN equ 3

.data

format db 10,"%s",0

elements db "012"
elements_len equ $-str

index db WORD_LEN dup (0)
buf db WORD_LEN+1 dup (0)

.code

start:
      lea esi, [index]
      lea ebx, [elements]
      lea edi, [buf]
load_string:
      mov ecx, WORD_LEN
      pushad
load_index:
      lodsb               ; idx = index[0]
      xlatb               ; c = str[ idx ]
      stosb               ; buf[i] = c
      loop load_index     ; --i

      push offset buf
      push offset format
      call printf
      pop eax
      pop eax
      popad

inc_index:
      inc byte ptr [esi+ecx-1]   ; increment last index
      cmp byte ptr [esi+ecx-1], WORD_LEN   ; exceeded word len?
      jne load_string

      mov byte ptr [esi+ecx-1], 0                 ; reset
      loop inc_index

      push 0
      call exit

      end start

dedndave

long ago, i wrote a permutation routine in 16-bit code
it was recursive, and would handle various group widths

maybe this link will help...
http://www-edlab.cs.umass.edu/cs123/Projects/Permutation/project6.htm

FORTRANS

Hi,

   You can use a set of nested loops.  In a pseudo-BASIC
style..

Cheers,

Steve N.


FOR I = 0 to 2
  FOR J = 0 TO 2
  IF ( I = J ) GOTO 20
    FOR K = 0, 2
    IF ( I = K ) GOTO 10
    IF ( J = K ) GOTO 10
    PRINT I, J, K
10 NEXT K
20 NEXT J
NEXT I
END

0x58

thank you all for your help  :U