tags:

views:

207

answers:

3

I need to work out if a massive integer (I mean 20 digits...) is prime.

I'm trying to use the brute-force method, but (of course) I need to use doubles to contain the original number. However, the modulo operator (%) is an integer operator - thus it is useless to me!

Any help would be greatly appreciated, tf.

+15  A: 

That's not possible, a double only has 15 significant digits. Look for an implementation of a BigInt class. C specific is discussed here.

Hans Passant
Interesting link on first glance, I'll make sure and read it! Thanks for taking the time.
mr1989foster
+1 This is the correct answer.
ziggystar
A: 

Are you looking for fmod in the C standard library? Or possibly fmodlfor long double?

Lars Wirzenius
It's fmodl for long double. Thanks!
mr1989foster
+2  A: 

Since the double data type is stored as a fractional value scaled to some power of two, and since it only has a precision of 15 decimal digits, a 20-digit number stored as a double will always be divisible by two, and therefore, is not prime.

Jeffrey L Whitledge
That explains one of the bugs I was getting - thanks for the input!
mr1989foster