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Јовић
  • 59,719
  • 40
  • 167
  • 261

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
  • 897,808
  • 140
  • 1,634
  • 2,455
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
  • 55,844
  • 9
  • 67
  • 97
1

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