views:

851

answers:

3

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.)

+4  A: 

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.

JaredPar
This seems like it would work but im having problems implementing it. i cant seem to really get in to reference the downloaded stuff, or even find the download when it says it was downloaded. i've been able to get the Microsoft.JScript option to appear but that doesn't seem to be helping since i cant find BigInteger or the like anywhere in it. anyone know where im going wrong?p.s. im not really sure how to include other libraries and whatnot into my project
Bryan
There is also supposedly supposed to be a systems.numeric namespace that has bigint in there too but i dont have systems.numeric as far as i can tell in vb.net 2008
Bryan
Try the following. Open Solution Explorer. Right Click on your porject and select "Add Referenc". Choose the Browse tab and navigate to the place you downloaded the files to. Select all of the .dll files and hit OK.
JaredPar
I would love to, but as i said i can't find where it downloaded to. a screen pops up saying its downloading but doesn't give me any storing options and another thing comes up saying its done downloading but even if i look in my recent downloads nothing is there. and if i try to search my computer for any file with what should be the name of what i downloaded, i get an error and the "my computer" screen closes with error text basicly saying that my search was treated as a potential virus.
Bryan
A: 

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

Sinan Ünür
In version 4 of the .NET framework however
Robin M
That's pointing to the documentation for .Net 4.0 which is not yet released.
JaredPar
Thanks for noticing. I thought I was searching under 3.5. I'll blame it on that silly collapsible menu on the left. Apologies.
Sinan Ünür
+4  A: 

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
Patrick McDonald
I was thinking about my solution, and found you implemented it already ;)
devio
Glad you did this. I thought it was cheap to use a library instead of an array or string solution.
Nosredna
Well this works, i just don't know how....... i've never even seen or used += or -= much less <<=, only =, >=, <=, and <>. and the whole number(digits) as byte confuses me too. does result += number(i) mean the same as result = result + number (i)?
Bryan
@Bryan, a+=1 is the same as a=a+1. a<<=1 means shift the bits to the left by 1 (double the number).
Nosredna
good to know, what about the number(digits) type thing. is that considered a brand new variable or does the variable digits interact with it somehow?
Bryan