CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2001
    Posts
    25

    How to Add 90 digits No

    IN c++ i added a 90 digits no around 5 lakh times .It took around 6 sec .
    and in visual Basic around 2 mins.
    can anyone send me the Vb code for adding 90 digits NO taking less time



  2. #2
    Join Date
    May 2001
    Posts
    46

    Re: How to Add 90 digits No

    Post your code to see if it can be improved...


  3. #3
    Join Date
    Aug 2001
    Posts
    25

    Re: How to Add 90 digits No


    Hi
    Here is the code

    Dim strFirstNo as string
    Dim strSecondNo as string
    Dim strResult as string
    Dim nCtr as Long
    Dim nCarryForwrd as Long
    Dim nAddition as Long
    Dim nFirstNoLen as Long
    Dim nSecondNoLen as Long
    Dim nCarryForward as Long

    strFirstNo = "1212345678902345678903456789456789043567890876764536576786564453543467"
    strSecondNo = "1212345678902345678903456789456789043567890876764536576786564453543467"

    nFirstNoLen = len(strFirstNo)
    nSecondNoLen = len(strSecondNo)

    nCtr = 0
    nCarryForwrd = 0
    Dim ncount as Long

    Dim strStarttime as string
    Dim strEndtime as string

    strStarttime = Now
    for ncount = 1 to 50000
    strResult = ""

    nFirstNoLen = len(strFirstNo)
    nSecondNoLen = len(strSecondNo)
    While nFirstNoLen > 0
    nAddition = Val(mid(strFirstNo, nFirstNoLen, 1)) + Val(mid(strSecondNo, nSecondNoLen, 1)) + nCarryForward
    strResult = Right(CStr(nAddition), 1) & strResult
    If nAddition > 9 then
    nCarryForward = Val(Left(CStr(nAddition), 1))
    End If
    nFirstNoLen = nFirstNoLen - 1
    nSecondNoLen = nSecondNoLen - 1
    Wend
    next

    strEndtime = Now
    MsgBox strStarttime + "End time = " & strEndtime




    Waiting for response



  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to Add 90 digits No

    to work (a little) faster with strings, substitute
    Right with Right$
    Left with Left$
    mid with mid$
    However, I do not think this will enanche it a lot...

    Cesare Imperiali


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to Add 90 digits No


    'I did some tests and found out it is mid$ function who slow this down.
    'Moreover, your code is not all right...(you did not reset the nCarryForward)
    'I tried with array, and the following seems a little faster (1 min instead
    ' of 2 min, even if there are a lot of operations)
    'Have a try

    Dim arr1() as Integer
    Dim arr2() as Integer
    Dim arrResult() as Integer
    Dim tmpval as Integer
    Dim strStarttime as string, strEndtime as string
    Dim lngCount as Long
    Dim intUbArray as Integer
    Dim tmpArr as Variant
    Dim strResultString as string
    Screen.MousePointer = vbHourglass
    'fill with values
    tmpArr = Array(1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 3, 4, 5, 6, 7, 8, 9, 0, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9, 0, 4, 3, 5, 6, 7, 8, 9, 0, 8, 7, 6, 7, 6, 4, 5, 3, 6, 5, 7, 6, 7, 8, 6, 5, 6, 4, 4, 5, 3, 5, 4, 3, 4, 6, 7)
    ReDim arr1(UBound(tmpArr))

    for lngCount = 0 to UBound(tmpArr)
    arr1(lngCount) = tmpArr(lngCount)
    next

    Erase tmpArr
    tmpArr = Array(1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 3, 4, 5, 6, 7, 8, 9, 0, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9, 0, 4, 3, 5, 6, 7, 8, 9, 0, 8, 7, 6, 7, 6, 4, 5, 3, 6, 5, 7, 6, 7, 8, 6, 5, 6, 4, 4, 5, 3, 5, 4, 3, 4, 6, 7)
    ReDim arr2(UBound(tmpArr))
    for lngCount = 0 to UBound(tmpArr)
    arr2(lngCount) = tmpArr(lngCount)
    next
    Erase tmpArr
    strStarttime = Now
    for lngCount = 1 to 50000
    'perform sum

    If UBound(arr1) > UBound(arr2) then
    ReDim arrResult(UBound(arr1) + 1) 'dimension it and fill with 0
    else
    ReDim arrResult(UBound(arr2) + 1)
    End If
    for intUbArray = UBound(arr1) to 0 step -1
    tmpval = arr1(intUbArray) + arr2(intUbArray)
    If tmpval > 9 then
    arrResult(intUbArray + 1) = CInt(Right$(CStr(tmpval), 1)) + arrResult(intUbArray + 1)
    arrResult(intUbArray) = CInt(Left$(CStr(tmpval), 1)) + arrResult(intUbArray)
    else
    arrResult(intUbArray + 1) = tmpval + arrResult(intUbArray + 1)
    End If
    next intUbArray
    next lngCount

    for intUbArray = 0 to UBound(arrResult)
    strResultString = strResultString & CStr(arrResult(intUbArray))
    next intUbArray
    If Left$(strResultString, 1) = "0" then
    strResultString = Right$(strResultString, len(strResultString) - 1)
    End If
    strEndtime = Now
    Screen.MousePointer = vbDefault
    MsgBox strStarttime & vbCrLf & "End time = " & strEndtime & vbCrLf & strResultString






    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: How to Add 90 digits No

    Assigning a string directly to a byte array is much faster than meddling with variant arrays.

    private Sub Command1_Click()
    Dim ByteArr() as Byte
    Dim i as Integer
    Dim strng as string
    strng = "1212345678902345678903456789456789043567890876764536576786564453543467"
    ByteArr = strng
    for i = 0 to UBound(ByteArr) step 2
    print ByteArr(i);
    next
    End Sub





  7. #7
    Join Date
    Jun 2001
    Location
    Israel
    Posts
    228

    Re: How to Add 90 digits No

    hi, i found your task interesting and thought i'd give it a try, i don't understand what's all the fuzz about 2 min, it works in less than a sec for me. give it a try, maybe i didn't understand something... this is it:

    Dim tmpArr as Variant
    Dim n1() as Long
    Dim n2() as Long
    Dim sum() as Long
    Dim s1 as Integer
    Dim s2 as Integer
    Dim ltemp as Long
    Dim lLoop as Integer
    Dim x as Long
    Dim t as Long
    '!!! you have to enter the number this way!:
    tmpArr = Array(111111, 222222, 333333, 444444, 555555, 666666, 777777, 888888, 999999, 0)
    '!!! it is also backwards, (111111 are the least segn. digits)
    s1 = UBound(tmpArr) '!!! i used them in the same size 4 comfort, i don't use s2 (size2)
    ReDim n1(s1)
    for lLoop = 0 to s1
    n1(lLoop) = tmpArr(lLoop)
    next lLoop
    Erase tmpArr
    tmpArr = Array(111111, 333333, 555555, 222222, 444444, 666666, 0, 777777, 888888, 999999)
    ReDim n2(s1)
    for lLoop = 0 to s1
    n2(lLoop) = tmpArr(lLoop)
    next lLoop
    Erase tmpArr

    ReDim sum(s1 + 1)
    t = Now
    for lLoop = 0 to s1
    x = sum(lLoop)
    sum(lLoop) = (x + n1(lLoop) + n2(lLoop)) Mod 1000000
    sum(lLoop + 1) = (x + n1(lLoop) + n2(lLoop)) \ 1000000
    next lLoop
    t = Now - t



    the sum array gets the number in the same way as the input (in XXXXXX mask)
    it's really easy to print it! just print the sum array cells 1 by 1...*

    ----------
    The @host is everywhere!
    ----------

  8. #8
    Join Date
    Aug 2001
    Posts
    25

    Re: How to Add 90 digits No


    HI Deghost

    I tried it this way .
    repeat the whole addition
    in loop (5 lakh times )
    then see what happens .it should take
    time more than 2 mins .
    thanx for response







  9. #9
    Join Date
    Aug 2000
    Posts
    27

    Re: How to Add 90 digits No

    This looks suspiciously like one of those "Why can't VB do something as well as C++", but I tried playing around with it anyway. I was able to get the time down to about 30 seconds (on my machine) using the following code:

    public Sub StringAdd()

    Dim strFirstNo as string
    Dim strSecondNo as string
    Dim strResult as string
    Dim nAddition as Long
    Dim nCarryForward as Integer
    Dim ncount as Long
    Dim strStarttime as string
    Dim strEndtime as string
    Dim First() as Integer
    Dim Second() as Integer
    Dim i as Integer

    strFirstNo = "1212345678902345678903456789456789043567890876764536576786564453543467"
    strSecondNo = "1212345678902345678903456789456789043567890876764536576786564453543467"
    ReDim First(len(strFirstNo) - 1)
    ReDim Second(len(strSecondNo) - 1)

    for i = 0 to len(strFirstNo) - 1
    First(i) = mid(strFirstNo, i + 1, 1)
    next i

    for i = 0 to len(strSecondNo) - 1
    Second(i) = mid(strSecondNo, i + 1, 1)
    next i

    strStarttime = Now

    for ncount = 1 to 50000
    strResult = ""
    for i = 0 to UBound(First)
    nAddition = First(i) + Second(i) + nCarryForward
    strResult = Right(nAddition, 1) & strResult
    If nAddition > 9 then
    nCarryForward = Left(nAddition, 1)
    else
    nCarryForward = 0
    End If
    next
    next

    strEndtime = Now
    MsgBox "Start time = " & strStarttime & vbCrLf & "End time = " & strEndtime

    End Sub





  10. #10
    Join Date
    Jan 2001
    Posts
    165

    Re: How to Add 90 digits No

    With your code it took me 159 seconds to process, with the following code it took me 10 seconds to process.

    NOTE: The - 96 and the + 48 are ascii adjustments so that the math is done using the integers and stored as the ascii values for the string representation of the numbers.

    Dim strFirstNo as string
    Dim strSecondNo as string
    Dim strFiller as string
    Dim strResult as string
    Dim nAddition as Long
    Dim nCarryForward as Long
    Dim ByteArr1() as Byte
    Dim ByteArr2() as Byte
    Dim ByteArrResult() as Byte
    Dim i as Integer

    strFirstNo = "1212345678902345678903456789456789043567890876764536576786564453543467"
    strSecondNo = "1212345678902345678903456789456789043567890876764536576786564453543467"

    If len(strFirstNo) > len(strSecondNo) then
    strFiller = Space(len(strFirstNo) - len(strSecondNo))
    strFiller = Replace(strFiller, " ", "0")
    strSecondNo = strFiller & strSecondNo
    ElseIf len(strSecondNo) > len(strFirstNo) then
    strFiller = Space(len(strSecondNo) - len(strFirstNo))
    strFiller = Replace(strFiller, " ", "0")
    strFirstNo = strFiller & strFirstNo
    End If

    ByteArr1 = strFirstNo
    ByteArr2 = strSecondNo
    ReDim ByteArrResult(UBound(ByteArr1))

    nCarryForward = 0

    Dim ncount as Long
    Dim strStarttime as string
    Dim strEndtime as string

    strStarttime = Now
    for ncount = 1 to 50000
    ReDim ByteArrResult(UBound(ByteArr1))

    for i = UBound(ByteArr1) - 1 to 0 step -2
    nAddition = ByteArr1(i) + ByteArr2(i) - 96 + nCarryForward
    ByteArrResult(i) = 48 + (nAddition Mod 10)
    nCarryForward = (nAddition \ 10)
    next i

    strResult = ByteArrResult
    If nCarryForward > 0 then strResult = Str(nCarryForward) & strResult
    next ncount
    strEndtime = Now

    MsgBox "Start time = " & strStarttime & vbCrLf & "End time = " & strEndtime & _
    vbCrLf & "Duration: " & 86400 * (CDate(strEndtime) - CDate(strStarttime)) & vbCrLf & _
    "Result: " & strResult



    -K


  11. #11
    Join Date
    Jan 2001
    Posts
    165

    Re: How to Add 90 digits No

    You can shave off 1 more second of this code if you take out the Redim statement that is inside of the For loop. This statement is unneccessary since on subsequent iterations you overwrite every element anyway.

    -K


  12. #12
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: How to Add 90 digits No

    Did you run your program through the environment or after making an exe

    A program that contains many loops runs much faster in an exe, the speed(in the case of VB6) being comparable with C, other things remaining same.

    If you don't already know,
    For improvement in speed, go to the compile tab in Project|Properties and click Advanced Optimizations. Check all options. But do make sure that you are not exceeding array bounds, or such things, if you don't want to see the blue screen too often.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured