Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Knowledge Base > Code Library > Combinatorical Function - VB.NET style.

Thread Tools Display Modes
Old 12-21-2003, 02:47 PM
Iceplug's Avatar
Iceplug Iceplug is offline

Retired Moderator
* Guru *
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default Combinatorical Function - VB.NET style.

See this thread for MathImagics' details on the Combinational Formula.

It is quite fast.
Public Class Combinational_FormulaOpt '... Public Shared Function ChooseFromTwo(ByVal N As Long, ByVal K As Long) As Decimal 'The decimal can hold up to 79 octillion Dim FactorCount As Long = System.Math.Min(K, N - K) 'This is the number of entries to factor[ (5 2) = 2; (5 3) = 2; (5 4) = 1; ] K = N - FactorCount 'The lowest distance that K is from N and 0. Dim UpperSet(FactorCount), LowerSet(FactorCount) As Long 'The lowerset will hold the numbers from 1 to FactorCount, which is N-K 'The upperset will hold the numbers from K+1 to K+N-K = N. Dim LV, IV, GCD As Integer, Verified As Boolean = True 'LV and IV are loop values, 'GCD will hold our greatest common divisor 'Verified makes sure that our values in the denominator are eliminated 'Set up our factor array. UpperSet(1) = K + 1 For LV = 2 To FactorCount LowerSet(LV) = LV UpperSet(LV) = K + LV Next For LV = FactorCount To 2 Step -1 'No need to check 1. If LowerSet(LV) > 1 Then For IV = 1 To FactorCount GCD = GreatestCommonDivisor(LowerSet(LV), UpperSet(IV)) If GCD > 1 Then UpperSet(IV) /= GCD LowerSet(LV) /= GCD End If Next End If Verified = Verified AndAlso (LowerSet(LV) <= 1) Next If Not Verified Then MessageBox.Show("Failed reduction of factors.", "Choosing Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Else Try ChooseFromTwo = Convert.ToDecimal(UpperSet(1)) For LV = 2 To FactorCount If UpperSet(LV) > 1 Then ChooseFromTwo *= Convert.ToDecimal(UpperSet(LV)) End If Next Catch ex As System.OverflowException If Verified Then MessageBox.Show("Answer overflows!", "Choose Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Else MessageBox.Show("Answer unobtainable! ", "Choose Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End Try End If End Function Public Shared Function GreatestCommonDivisor(ByVal Num1 As Decimal, ByVal Num2 As Decimal) As Decimal Dim X As Decimal = System.Math.Max(Num1, Num2) 'X is the maximum of the two numbers. Dim Y As Decimal = System.Math.Min(Num1, Num2) 'Y is the minimum of the two numbers. Dim Remainder As Decimal = X Mod Y 'Remainder always holds the remainder. 'Method: 'Ra Rb Rc 'X = Q0 * Y + R0; Q# is quotient of X/Y, R# = remainder of X/Y 'Y = Q1 * R0 + R1; Since we have Mod, there's no need to calculate Q# 'R0 =Q2 * R1 + R2 'etc... 'Ra =Qc * Rb + Rc; a + 2 = b + 1 = c 'where Rc = 0, Rb is the GCD Do Until Remainder = 0 X = Y 'Pass Rb into Ra 'Y is smaller than X at the beginning Y = Remainder 'Pass Rc into Rb 'Remainder cannot be larger than the dividend or the divisor (than X or Y) Remainder = X Mod Y Loop 'Extract our GCD Return Y End Function End Class

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!

Last edited by Iceplug; 12-21-2003 at 03:19 PM.
Reply With Quote

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Registry: queue list of keys? robplatt File I/O and Registry 3 07-02-2003 01:00 PM
need help! i'm a beginner geekool General 7 04-26-2003 12:44 AM
Shareware Registry Protection Technigue karachi999 General 2 01-21-2002 02:40 PM
MS Winsocket gibson General 3 07-20-2001 10:03 AM
link function to textboxes golash General 2 05-03-2001 11:57 AM


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..