Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 What am i doing wrong? it should work but it dosnt.
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BigFat




PostPosted: Fri Dec 25, 2009 4:50 am   Post subject: What am i doing wrong? it should work but it dosnt.

i'm trying to do number 16 on Project Euler and my code theoretically should work, but it dosnt. Can anybody tell me why it dosnt work. I already looked up the answer and im about 2 thousand off the correct answer and my program never even hits the right answer. could some please help, heres my code:

'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)?

Dim x As Double = 1
Dim y As Double
Dim k As Integer = 1
Dim answer, counter As Int16
Dim numbers(10000) As Integer
Dim cup3 As String

x= 2^1000

Do While counter < 310

y = x Mod 10 ^ (k)

cup3 = CStr(y)

numbers(counter) = Strings.Mid(cup3, 1, 1)

counter += 1
k += 1
Loop

For i = 0 To 301

answer += CInt(numbers(i))
If answer = 3366 Then
'3366 is the answer
MsgBox("", "")
End If
Next

end sub

It should work but it dosent:(
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Fri Dec 25, 2009 1:31 pm   Post subject: RE:What am i doing wrong? it should work but it dosnt.

I'm going to guess it's just an accuracy problem, doubles can be accurate or big, and 2**1000 is really big.

A 64 bit integer can hold a number up to 2**64-1, and that's the space a double takes, so you can't accurately store 2**1000 in a double.

If it helps, I put 2**1000 into Python (which has arbitrary integer size), and got

10715086071862673209484250490600018
10561404811705533607443750388370351
05112493612249319837881569585812759
46729175531468251871452856923140435
98457757469857480393456777482423098
54210746050623711418779541821530464
74983581941267398767559165543946077
06291457119647768654216766042983165
2624386837205668069376

If that helps. (I'm guessing that this wouldn't be cheating since 2**1000 can easily be found with most high level languages).

[edit] Broke up number into lines
BigFat




PostPosted: Fri Dec 25, 2009 2:02 pm   Post subject: Re: What am i doing wrong? it should work but it dosnt.

I though VB was a high level programming language? They say it has similar power and performance to c#.

I also though Python was was a low level language, is this true?

and thanks for the number and info i might just have to learn python.
TheGuardian001




PostPosted: Fri Dec 25, 2009 2:27 pm   Post subject: Re: What am i doing wrong? it should work but it dosnt.

Both python and visual basic are high level languages. Your problem is not the language level, it is simply the amount of information capable of being stored in a specific type of variable. Language level is (mostly) superficial.

Visual basic uses different types of integers to hold different amounts of data. Visual basic, like most other languages, allocates a certain amount of memory based on the type of variable. This is, in general, more than enough to hold the values most people will be using. However since 2 ** 1000 is an astronomically large number, it can't be stored in a standard type. You could technically store this number, however you would have to create your own type with enough memory allocated to store the result.

I believe Python takes a different approach to allocating memory, instead allocating whatever is needed to accommodate the value stored within the variable, should that value exceed the initial 32 bits of memory. This means that it has no trouble storing 2 ** 1000, as it will dynamically allocate more memory to hold all the digits in the number.
BigFat




PostPosted: Fri Dec 25, 2009 2:34 pm   Post subject: RE:What am i doing wrong? it should work but it dosnt.

wow, that sounds like a big crutch on vb's part, im surprised MS hasn't fixed it or addressed it.
TheGuardian001




PostPosted: Fri Dec 25, 2009 2:56 pm   Post subject: Re: What am i doing wrong? it should work but it dosnt.

It's not a problem with the language, VB was intentionally made this way. And it's not just VB, C, C++, Java, and most other compiled languages use generically sized types for integers. Very few languages use arbitrary integer sizes, as they are not, in general, very memory efficient, although they do have their applications.

Some languages have specific types for arbitrary sized integers (Java has BigInteger, Perl has BigInt). You could try looking for a similar type in VB.
DtY




PostPosted: Fri Dec 25, 2009 3:15 pm   Post subject: RE:What am i doing wrong? it should work but it dosnt.

Yeah, arbitrarily sized integers will use more memory and cycles to work with, and their only advantage is that they can work with numbers bigger than 18446744073709551615.

Python also has normal integers that automatically become long integers when their value grows too big.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: