0

I am trying to sum up the digits in a very large number. I have gotten the length of the number with
l = answer.bitLength() but I can't figure out how to increament through each digit using a For loop. Any ideas?

I'm using the java.math.biginteger.

Visual Studio 2005 Version 2.0

I should also add that I can't seem to use <> or any of the simple math options with the biginteger I'm using. If anyone could tell me how to use a different biginteger I would be more than willing to swap.

Dim answer As java.math.BigInteger
Dim sum As Integer = 0
Dim x As Integer
Dim i As Integer
'Sets value of answer equal to 1
answer = java.math.BigInteger.valueOf(1)

'gets 100!
For i = 1 To 100
answer = answer.multiply(java.math.BigInteger.valueOf(i))
Next

'gets length of answer
Dim l As Integer
l = answer.bitLength()

'Sums up digits in 100!
For x = 0 To l - 1
'Need to pull each character here to add them all up
Next

Final Solution for summing up the digits. Thanks to wageoghe.

Dim r As Integer
Dim s As Integer
s = 0
While (answer.compareTo(java.math.BigInteger.valueOf(0)) > 0)

r = answer.mod(java.math.BigInteger.valueOf(10)).ToString()
s = s + r
answer = answer.divide(java.math.BigInteger.valueOf(10))

End While
Bryan
  • 1,851
  • 11
  • 33
  • 56
  • What have you tried? Can you post a pseudo code example of what you are trying to do? – Oded Jan 25 '11 at 20:56
  • There is no `bitLength` (or even `BitLength`) member of `System.Numerics.BigInteger`. Are you sure you're not thinking about Java? – LukeH Jan 25 '11 at 21:01
  • I'm using java.math.biginteger in VB.Net – Bryan Jan 25 '11 at 21:08
  • Why aren't you using System.Numerics.BigInteger? – wageoghe Jan 25 '11 at 21:09
  • If you are using .Net 4, you should use the `System.Numerics.BigInteger` type. – Gabe Jan 25 '11 at 21:13
  • I'm not using System.Numerics.BigInteger because I can't seem to get it. I'm trying to add the reference but I'm not seeing the suggested System.Numerics. – Bryan Jan 25 '11 at 21:17
  • Does the Add Reference dialog say "Filtered to: .NET Framework 4" at the top (of the .NET tab)? If so, System.Numerics should be in there. Have you tried clicking on the Component Name header to sort the components alphabetically? – wageoghe Jan 25 '11 at 21:20
  • No, there is no filtered to text or any filtering options in the add reference window I have, just the .Net,COM,Projects,Browse,and Recent tabs. And under .Net it just has the Component Name and other columns, nothing else. – Bryan Jan 25 '11 at 21:32

3 Answers3

0

If you think of the number as a list of binary characters, then you could get the least significant hex digit by ANDing the number with 0xF. If you then shifted the number right by 4 bits (>> 4), then you could get the next hex digit.

After you get all of the hex digits, you could sum them up and then convert them to decimal.

David Weiser
  • 5,190
  • 4
  • 28
  • 35
0

Something like this should work:

    Dim bi As New System.Numerics.BigInteger(12345)
    Dim c As Char
    Dim s As Long

    s = 0

    For Each c In bi.ToString()
        s = s + Integer.Parse(c.ToString())
    Next

Or this more conventional way using Mod and / (integer division)

    Dim bi As New System.Numerics.BigInteger(12345)
    Dim s As Long
    Dim r As Integer

    s = 0

    While bi <> 0
        r = bi Mod 10
        s = s + r
        bi = bi / 10
    End While
wageoghe
  • 27,390
  • 13
  • 88
  • 116
  • I don't seem to have access to a System.Numerics. – Bryan Jan 25 '11 at 21:10
  • You can add a reference to the assembly (System.Numerics) by going to your Project->Properties dialog, go to Reference tab, click on Add..., find System.Numerics, and add it. I guess that I should add that System.Numerics is available in .Net 4.0. – wageoghe Jan 25 '11 at 21:12
  • Even with java.math.biginteger, you should be able to implement something my second example using the divide and mod functions. – wageoghe Jan 25 '11 at 21:18
  • Glad it worked. BTW if you are using Visual Studio 2010 (or some flavor of 2010 like Visual Basic Express 2010 or Visual C# Express 2010), you should be able to find System.Numerics in there somewhere. If you clicked on the Component Name column header to sort the Component Name column alphabetically and still can't find System.Numerics, then I guess you don't have it. – wageoghe Jan 25 '11 at 21:54
0

Another approach, is to do the following (this assumes that answer is positive):

int sum = 0;
while(answer > 0){
    sum += answer % 10;
    answer /= 10;
}
David Weiser
  • 5,190
  • 4
  • 28
  • 35