• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

First 10 digits of the sum of 100 50 digit numbers (Project Euler # 13)

 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I stored the numbers in a String array and I'm trying to parse them one at a time and store them into a long variable. Is 50 digits too large for a long data type?? If so should I be using BigInteger or some other data type in java???

Here's what I have so far:
Whenever I run this code, It throws a NumberFormatException every iteration.



Thanks,
Hunter M.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hunter McMillen wrote:Is 50 digits too large for a long data type?? If so should I be using BigInteger or some other data type in java???


Yes to both questions. A long can only hold up to 2^63-1, which is 9,223,372,036,854,775,807. For anything larger, you will have to use float / double (but you'll precision) or BigInteger.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one becomes pretty trivial since the standard Java library has an arbitrary precision integer class (BigInteger). For (hypothetical) extra credit, how would you roll your own BigInt class (with an add() method)?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so here's what I have with the BigInteger changes, Sorry Garret I'm not really sure what you mean by making my own add() method for BigInteger.




This gave me the correct answer however by using the BigInteger's built in add() method.
Thanks for your help guys.

Hunter M.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was half joking that since Java has a BigInteger class in the standard library, this question is pretty trivial. What might you do in a language that only supported 32 bit ints or 64 bit longs but didn't already have a class designed for arbitrary length integers. I was calling the class that you might make BigInt:


How would you internally represent arbitrary precision integers? How would you implement the add() and parse() methods. This to me seems a more interesting question since the problem from Project Euler was relatively easy.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those are interesting questions, unfortunately I have no idea. Im not sure I'm good enough yet to tackle that question.

Hunter.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One representation suggested by the Wikipedia article would be to use an array of ints with each int representing a digit in base 1000.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you remember how you were taught to add when you were little in school? Add the units, carry 1 if over 9, then add the 10s. That is the basis for the add() method.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pretty cool article, thanks for linking it Garret; I'm going to work on that over the next few weeks or so and ill post what I have by then.

Hunter.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Can you remember how you were taught to add when you were little in school? Add the units, carry 1 if over 9, then add the 10s. That is the basis for the add() method.


I had to implement something like this once in my algorithms class. I basically wrote a linked list, putting each digit into a node. it was quite a pain, but I got it to work, and was pretty proud of it.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:I had to implement something like this once in my algorithms class. I basically wrote a linked list, putting each digit into a node. it was quite a pain, but I got it to work, and was pretty proud of it.


The idea is good, but this is ofcourse super-inefficient...

Note that the source code for BigInteger is available. Look in your JDK installation directory for the file src.zip, you can find BigInteger.java in there.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've done a number of the Project Euler puzzles, but have always thought that using BigInteger was a bit like cheating. There's much more to be learnt by avoiding it, IMO. Maybe you can rewrite the code without using it when you're a bit farther down the road.
reply
    Bookmark Topic Watch Topic
  • New Topic