5 Replies - 2418 Views - Last Post: 28 September 2011 - 06:38 PM Rate Topic: -----

#1 xanthaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 09

Handling math with very large numbers

Posted 28 September 2011 - 06:24 AM

I have written a program that is designed to test if an input value is or is not a prime number. This is accomplished by dividing the test value t by all values x such that 1 < x < t by means of a for loop. Attached is the complete source code for this program. It is in text format since DIC will not allow .java uploads (an understandable precaution). It is simplistic and small. It was written to proof a concept in my discrete mathematics class. The program is not part of my homework; rather it is an undertaking of my own.

I am having the following problem. The program functions perfectly for smaller values. I have successfully tested the program against primes and composites of 10 digits with no errors. However, when I start testing larger primes, I find the following as a barrier:

Numbers start being reformatted. x becomes xEy (example: 1.8946456813E24). This causes loss of precision in the floor comparison. This loss of precision results in a false factorization of the number, and labels a proven prime as a composite.

I am currently using the double type for the value. This obviously comes with limitations. I am not aware of any primitive type that will allow the rather large values I am planning to test to work (numbers of 20+ digits, both integer and decimal).

Is there a different numeric type, be it a reference or class method type, that will allow working with very large numbers without the xEy reformatting? This would be most helpful for this program and the next program; a prime number generator... (it's a part of my next chapter on indexing and a revisit on prime theorems.)

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Handling math with very large numbers

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Handling math with very large numbers

Posted 28 September 2011 - 06:24 AM

Check out BigInteger and BigDecimal, located in the java.math package.
Was This Post Helpful? 2
  • +
  • -

#3 xanthaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-April 09

Re: Handling math with very large numbers

Posted 28 September 2011 - 09:56 AM

I have pulled the javadocs for these classes in the package. Thanks for the tips. I'll check back if I have any questions after reading the javadocs and examples I can find...

I wish I knew how to give kudos. If I did I would do so...

Many thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Handling math with very large numbers

Posted 28 September 2011 - 09:58 AM

There is a + and - post for each post not your own. You are welcome to click the + button for posts that helped you, and the - button for factually wrong information. I'm just glad I was able to help, though. No need for kudos. :)
Was This Post Helpful? 0
  • +
  • -

#5 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Handling math with very large numbers

Posted 28 September 2011 - 01:46 PM

Just a remark:

using double for prime numbers, which are integer by definition, is really not a good idea

for (x = 2; x < t; x++) //loops to divide by all valid values of composite

also, your loop x++: start with x = 3 and then x += 2; no need to check all the even number, you will just cut your loop time by half

There are a lot of better algorithms then simple iteration, especially if you want to go with Big Numbers
Was This Post Helpful? 2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Handling math with very large numbers

Posted 28 September 2011 - 06:38 PM

Better to just check with previously found prime numbers
That will bring you up to Long.MAX_VALUE

http://www.dreaminco...snippet4665.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1