Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
SOLVED
Thx SAKryukov[^]

Hey there! I need to make some BIG calculations using binomial coefficients[^]

Ok, since vb.net doesn't have factorial nor binomial coefficient functions I've already done my own:
VB
Function fact(ByVal Number As Byte) As Decimal
        Dim i As Integer = 1
        Dim Calculated As Decimal = 0

        Calculated = Number

        Do While i < Number
            Calculated *= (Number - i)
            i += 1
        Loop

        Return Calculated
    End Function

    Function C(ByVal n As Byte, ByVal p As Byte) As Decimal
        Return ((fact(n)) / (fact(p) * fact(n - p)))
    End Function


But here is my problem, I need to make calculations with numbers bigger then the biggest binomial using a byte as n and as p and that is:
(ps: I know that a byte is only up to 255, but I'm shifting it up one unit, so 0 is 1.....and 255 would be 256)
C(256, 128) = 5768658823449206338089748357862286887740211701975162032608436567264518750790

Or have a look Here
And that has 75 digits, the BIGGEST data type vb.net has is decimal with 28 digits.

Does anyone know how I can work around this?
Posted
Updated 3-Nov-11 14:33pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Nov-11 20:10pm    
What is your target .NET Framework version?
--SA
Sergey Alexandrovich Kryukov 3-Nov-11 20:57pm    
Solved? Great. You are very welcome. (I also voted 5 for the question. Not knowing all the libraries is not a great sin, but correct recognition of a problem is a good trait. :-))
Good luck, call again.
--SA
WhiteKnightBRASIL 3-Nov-11 21:18pm    
Yes, thanks a lot!
I had a look at the vb.net data types @ MSDN but I didnt find anything that linked to BigInteger! Or I have a bad eye or they should really link it up! :P
Thx once again! ;)
Sergey Alexandrovich Kryukov 3-Nov-11 23:16pm    
I don't know... :-) I have a habit to look through the articles ("What's new in version {0} of {1}", ...) every time such publication appears. It always helped me.

Best,
--SA

1 solution

You need to use the structure BigInteger, see http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx[^]. It provides unlimited number if digits.

This unit (the whole name space System.Numerics) was introduced in .NET Framework v.4.0. What if you need to use earlier version of .NET? Try source code of Mono: http://en.wikipedia.org/wiki/Mono_%28software%29[^], http://www.mono-project.com/Main_Page[^].

Download the latest Mono source code and search for this structure — it's currently implemented.

—SA
 
Share this answer
 
Comments
RaisKazi 4-Nov-11 3:14am    
Framework Independent Solution. 5ed.
Sergey Alexandrovich Kryukov 4-Nov-11 13:24pm    
Thank you, Rais.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900