0

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!

BЈовић
  • 62,405
  • 41
  • 173
  • 273

3 Answers3

18

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

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

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
  • 58,241
  • 9
  • 71
  • 99
1

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