• 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

working with huge numbers

 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it unreasonably difficult to work with huge numbers (computing powers with thousands or even millions of digits) in Java? Would you have to implement some sort of custom multiplication, or is this not even feasible really? I'm considering using Java for an academic project involving Marsenne primes. Thanks...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not unreasonably difficult, no. Look at the java.math.BigInteger and BigDecimal classes.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did a little experiment recently in an attempt to implement a BigInteger.pow(BigInteger) method (there's only a BigInteger.pow(int) in java.math). The result can be found in this thread FYI.

What I will say is that, perhaps unsurprisingly, it takes ***ages***!!! Or maybe that's just my crappy code.

Jules
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good work! Well, I just realized that I don't really need to raise my BigIntegers to any powers bigger than a long value since the biggest Mersenne prime known can be expressed with a power that would fit into an int! I need a BigInteger for the result because the number expressed by it is actually millions of digits.

Anyway, does anyone know any decent way to transform a BigInteger into scientific notation or some other shorthand form?

For example, it'd be nicer to see 2^13432589237 than some crazily long string of numbers.
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example, it'd be nicer to see 2^13432589237 than some crazily long string of numbers.

...and you can generate it by using binary shifts.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a school science project or are you looking to push the envelope of number theory?

If it's for school, just try to limit the number of BigInteger operations per trial and time one iteration of your code well enough to set reasonable goals for your project. If you do a good job researching the work that has already been done and integrate that research into your search algorithm, you'll be in good shape.

If you are competing with the big players, you'll want to learn assembler and study the different algorithms for manipulating big numbers and the exact optimization techniques used by the manufacturer of your CPU chip. All this material is on the web. Try google.

I did a search for an odd perfect number in High School and kept my results as the exponents of the prime numbers starting with 3 because the theorems in that area worked with the exponents. You'll want to keep your numbers in a form compatible with the existing theorems on Mersenne primes.

BTW, I found this link: http://www.utm.edu/research/primes/mersenne/index.html

Good luck.
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephen Huey:
Anyway, does anyone know any decent way to transform a BigInteger into scientific notation or some other shorthand form?


I would imagine that BigDecimal does that out of the box. You may be able to convert or perhaps just use BigDecimal instead with no...um...decimal.

Jules
 
reply
    Bookmark Topic Watch Topic
  • New Topic