flat assembler
Message board for the users of flat assembler.

Index > Main > Combinations

Author
Thread Post new topic Reply to topic
bitRAKE



Joined: 21 Jul 2003
Posts: 2914
Location: [RSP+8*5]
bitRAKE
Number of possible combinations from a set.
Code:
Choose:
   ; RDX in range [0,RCX]
      mov r9,rcx
  sub r9,rdx
  jc .err

 push 0 1
    pop rax r8

      ; reduce, R9 = MIN(RDX,R9)
  cmp rdx,r9
  cmovc r9,rdx
        
    jmp .1

.0:   inc r8
      mul rcx
     dec rcx

 ; RDX in range [0,R8)
       cmp rdx,r8
  jnc .err

        div r8

.1:   cmp r8,r9
   jc .0
       ; RAX = RCX choose RDX
      retn

.err:   xor eax,eax
 stc
 retn    
...only good for a small range of integers: all the values up to 68 choose 30 are within 64-bit range. Larger values work, for example, 1973 choose 6 doesn't overflow.

This uses a method to generate individual rows of Pascal's triangle.
Code:
  push 52 5
   pop rdx rcx
 call Choose
 jc .error    
...an example of use: there are 2598960 possible 5 card hands from a deck of 52 unique cards.

_________________
¯\(°_o)/¯ unlicense.org
Post 08 Jun 2009, 04:10
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.