4 Replies - 419 Views - Last Post: 19 December 2013 - 10:33 AM Rate Topic: -----

#1 BillKindle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 13

Very large numbers

Posted 19 December 2013 - 09:45 AM

Im working (slowly) through the problems on Project Euler, I am currently on problem 16 and am having some difficulties. The number produced, 2^1000, is way to big to be stored accurately into any standard data types. I attempted to find each digit in the answer using the method below however this did not give me the correct answer, if you have any sugguestion they would be greatly appreciated. Thanks.

For a As Integer = 1 To 1000
            num = num * 2

            If num >= 10 Then
                position = position + 1 'When value reaches > 10 the position is moved on 1 place
                Number(position) = 1 + Carry
                Carry = 1
                num = num - 10
                For b As Integer = 1 To position
                    Number(B)/> = Number(B)/> * 2 + Carry
                    If Number(B)/> >= 10 Then
                        Carry = 1
                        Number(B)/> = Number(B)/> - 10
                    Else
                        Carry = 0
                    End If
                Next
            End If
            Carry = 0
        Next

        For c As Integer = 1 To position
            sum = sum + Number(c) 'Adds all integers found
        Next

        Console.WriteLine(sum) 'Displays sum
        Console.Read()


The value output is : 624 (which is incorrect)

Is This A Good Question/Topic? 0
  • +

Replies To: Very large numbers

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Very large numbers

Posted 19 December 2013 - 09:48 AM

If you are solely using integer arithmetic then you can use the BigInteger class. Otherwise you may have to look into some 3rd party BigDecimal classes.

This post has been edited by Ryano121: 19 December 2013 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 BillKindle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 13

Re: Very large numbers

Posted 19 December 2013 - 09:58 AM

Thanks for the response, I know BigInteger would work in this case but I find it sort of "cheating" as it's not really a data type found in Vb.net originally. I am trying to complete the problem using an alternative method and without the need to store the number in full.
Was This Post Helpful? 0
  • +
  • -

#4 TheCrazy8th  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 14-October 13

Re: Very large numbers

Posted 19 December 2013 - 10:22 AM

I am just curious as to how you see this as "cheating" in any way...Code is all shortcuts really. To say that one shortcut is cheating and the rest aren't is kinda silly. If you wanna REALLY not cheat then you can't really use a language other than machine code lol. That's the way I look at it.
Was This Post Helpful? 0
  • +
  • -

#5 BillKindle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 13

Re: Very large numbers

Posted 19 December 2013 - 10:33 AM

Ok i see what you mean, but I'd just like to find a way of coming up with the answer using only the resources found in the base visual studio program that I have. I guess it's not really just cheating but I'm using the problems on Project Euler to try and learn as well as just simply answer them. I know I could use the Big Integer data type but I'd like to complete the problem without it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1