Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Code Samples & Tips
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Code Samples & Tips Share your recent discoveries and ideas with other users.


Combinations Sub: choose(n,k)


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-21-2008, 05:19 PM
Senior Member
 
Join Date: Apr 2007
Posts: 192
Default Combinations Sub: choose(n,k)

Hi,

I developed a sub for computinf the amout of combinations, given a population N, choose subset K (notation: choose(n,k).
Example: how many combinations are possible for 7 cards from 52?
choose(52,7). To compute n!/((n-k)!*k!)
Because this creates an overflow for relatively small numbers I created an alternative and really fast routine.

Code:
Sub Choose(nn,kk)
	' effficient Choose method implementation

	If (nn<0) OR (kk<0) Then Msgbox("Invalid negative parameter in Choose()..")
	If (nn<kk) Then Return 0
	If (nn=kk) Then Return 1

	delta = 0
	imax = 0

	If (kk < (nn-kk)) Then 'choose(100,3)
		delta = nn-kk
		imax = kk
	Else 'choose(100,97)
		delta = kk
		imax = nn-kk
	End If

	ans = delta+1
	For i = 2 To imax
		ans = (ans * (delta + i))/i
	Next
	Return ans
End Sub
I am building an application for displaying and storing all possible combinations. When ready I will publish.

Have fun

Marc
Reply With Quote
  #2 (permalink)  
Old 02-21-2008, 06:04 PM
Senior Member
 
Join Date: May 2007
Posts: 150
Awards Showcase
Competition Winner 
Total Awards: 1
Default combinations

Marc
Have you seen my answer to your previous thread regarding combinations ?
It might help you...
Combinations function
__________________
David Erez
Ramat Hasharon, Israel
Reply With Quote
  #3 (permalink)  
Old 02-21-2008, 06:51 PM
Senior Member
 
Join Date: Apr 2007
Posts: 192
Default

Hi David,

Yes I have and I find it very useful.
Thank you for that, by the way...
However I needed an application for my main application to find a next combination without using recursive programming.
I found it at:
http://msdn.microsoft.com/msdnmag/is...n/default.aspx
I translated the code a bit and added some for my own code. Now I can use any combination and find its immediate successor or predecessor.
Marc
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Choose Folder - Optimized Compilation orhan Questions & Help Needed 9 05-15-2008 02:36 PM
Combinations application Stellaferox Share Your Creations 0 02-21-2008 08:24 PM
Combinations function derez Share Your Creations 0 02-04-2008 11:40 AM
code to write out combinations e.g. (10 choose 3) Stellaferox Questions & Help Needed 4 02-04-2008 05:51 AM
List of possible combinations for x numbers. EdQas Questions & Help Needed 3 10-07-2007 09:18 AM


All times are GMT. The time now is 05:53 PM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0