# Handling math with very large numbers

Page 1 of 1

## 5 Replies - 5313 Views - Last Post: 28 September 2011 - 06:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=248943&amp;s=79258061ebab98c0ed7836f7264ae6bc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 xanthaos

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 12226
• Posts: 45,301
• 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.

### #3 xanthaos

Reputation: 0
• 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

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12226
• Posts: 45,301
• 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.

### #5 cmpshr

Reputation: 42
• 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

### #6 pbl

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

Reputation: 8378
• Posts: 31,956
• 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