Bryan
Bryan

Reputation: 1893

Project Eulers problem 16 in visual basic. Sum of digits in the number 2^1000

Project Euler's problem 16: 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 2^(1000)?

I have been trying to do this problem for a few days now and i just can't figure out how to get vb.net 2008 to recognize anywhere near that large a number. I have seen in other posts that some software like java has the integer type BigNumber or BigInteger but i cant find anything like that in visual basic. I'm running into this problem a lot using Visual Basic. I also can't seem to find any of the standard upper level math features in visual basic such as factorials and a few others that i can't remember but couldn't find under the math feature. any suggestions? (Sorry let me rephrase, any suggestions on how to do this stuff without switching to a different programming language.)

Upvotes: 0

Views: 2704

Answers (4)

shashi.kr
shashi.kr

Reputation: 11

public static void main(String[] args) {
    int m = 2, ci = 1, n = 1000, i;
    int[] arr = new int[n + 1];
    arr[1] = 1;
    for (i = 1; i <= n; i++) {

        int carry = 0;
        for (int j = 1; j <= ci; j++) {
            arr[j] = arr[j] * m + carry;
            carry = arr[j] / 10;
            arr[j] = arr[j] % 10;
        }
        if (carry > 0) {
            while (carry > 0) {
                ci++;
                arr[ci] = carry % 10;
                carry = carry / 10;
            }
        }
    }
    int sum = 0;
    System.out.println(ci + "\n \n ");
    for (int j = ci; j > 0; j--) {
        System.out.print(arr[j]);
        sum = sum + arr[j];
    }
    System.out.println("\n \n " + sum);
}

Answer: Number of digits in 2^1000: 302

2^1000= 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

sum of the digits: 1366

Upvotes: 0

Patrick McDonald
Patrick McDonald

Reputation: 65451

here's the function I wrote, it's not so difficult to do your own implementation of a BigInteger purely for this purpose (very difficult to make it efficient and versatile however, but that's what libraries are for)

Public Shared Function Problem16(ByVal power As Integer) As String

    Dim digits As Integer = CInt(Int(power * Log10(2)))

    Dim number(digits) As Byte
    number(digits) = 1

    For i As Integer = 1 To power
        Dim carry As Byte = 0
        For j As Integer = digits To 0 Step -1
            number(j) <<= 1
            number(j) += carry
            If number(j) > 9 Then
                carry = number(j) \ CByte(10)
                number(j) -= CByte(10)
            Else
                carry = 0
            End If
        Next
    Next

    Dim result As Integer
    For i As Integer = 0 To digits
        result += number(i)
    Next
    Return result.ToString

End Function

Upvotes: 4

Sinan &#220;n&#252;r
Sinan &#220;n&#252;r

Reputation: 118158

I haven't tried it but there seems to be a Big Integer construct for Visual Basic.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755179

There are several BigInteger libraries that are freely available which you can use.

In this case the limitations are not necessarily the language. Visual Basic, outside of basic math operations, largely depends on the BCL for functionality. This is true of most languages which run on the CLR (including C#). In most cases though, there are libraries available which you can use to augment the functionality of the framework.

Upvotes: 4

Related Questions