flat assembler
Message board for the users of flat assembler.

Index > Main > Combinations

Thread Post new topic Reply to topic

Joined: 21 Jul 2003
Posts: 3977
Location: vpcmipstrm
bitRAKE 08 Jun 2009, 04:10
Number of possible combinations from a set.
   ; 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

.err:   xor eax,eax
...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.
  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)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.