Al Pascual



Geo RSS
Geo Twitter Timeline

Blogs I read

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Factorial computation in ASP.NET

image

Zichuan Ye, a coworker wrote for fun a factorial calculator. It does both, multiplication and factorials providing a postback and callback implementation in ASP.NET and VB.NET. A great tool to download and learn from it. He was kind enough to provide the source code to share this great tool with all of us. Something that took him an hour to create would take me a few days to figure it out.

Just take a look at the multiply function below.

Private Function DoMultiply(ByVal A() As Short, ByVal B() As Short) As Short()
    Dim iCnt As Integer
    Dim jCnt As Integer
    Dim c() As Short
    iCnt = A.Length
    jCnt = B.Length
    Dim i As Integer
    Dim j As Integer
    Dim kCnt As Integer
    kCnt = iCnt + jCnt - 1
    ReDim c(kCnt)
    For i = 0 To kCnt
      c(i) = 0
    Next
    For i = iCnt - 1 To 0 Step -1
      For j = jCnt - 1 To 0 Step -1
        DigitMultiply(i + j + 1, A(i), B(j), c)
      Next
    Next
    Return c
  End Function
  Public Function DigitMultiply(ByVal kIndex As Integer, _
  ByVal iValue As Short, ByVal jValue As Short, ByVal c() As Short) As Integer
    Dim c1 As Short
    Dim c2 As Short
    Dim cm As Short
    cm = iValue * jValue
    If (cm < 10) Then
      DigitalAdd(cm, kIndex, c)
    Else
      c1 = (cm \ 10)
      c2 = (cm Mod 10)
      DigitalAdd(c2, kIndex, c)
      DigitalAdd(c1, kIndex - 1, c)
    End If
  End Function
  Public Sub DigitalAdd(ByVal iValue As Short, ByVal kIndex As Integer, ByRef c() As Short)
    Dim cm As Short
    Dim c10 As Short
    Dim c0 As Short
    cm = c(kIndex) + iValue
    If cm < 10 Then
      c(kIndex) = cm
    Else
      c10 = cm \ 10
      c0 = (cm Mod 10)
      c(kIndex) = c0
      DigitalAdd(c10, kIndex - 1, c)
    End If
  End Sub

Download the code from here

Cheers

Al

Posted from http://weblogs.asp.net/albertpascual

HiddenNetwork.com Banner

Posted: Sep 17 2008, 11:10 PM by albert | with no comments
Filed under: , ,