0

I have a 2500 digit integer which I need to determine the primality of. There are many methods in R for testing primality of 'small' numbers but the language doesn't seem to be suited for storing massive numbers. There are packages designed to store such numbers but they all seem to revolve around saving it in a string which makes me uncertain of how I could then perform a primality test on it. Any clarification on what the capabilities of the language are with regards to this topic would be appreciated.

Tim Hargreaves
  • 292
  • 3
  • 11
  • 3
    Is there any particular reason your are drawn to R to do this task? I personally love R but it wouldn't be my first choice for something like this. – MrFlick Sep 08 '17 at 19:37
  • Just because I know the basics of R and I'm trying to further my understanding of it. If there's a better language or tool I could use for this then that would be fine too. – Tim Hargreaves Sep 08 '17 at 19:45
  • 1
    "they all seem to revolve around saving it in a string" That's not the case. You just usually need to input numbers as strings. – Roland Sep 08 '17 at 19:49
  • Okay that must be where I misunderstood. So I can still manipulate as I would a normal integer? – Tim Hargreaves Sep 08 '17 at 22:43

1 Answers1

1

Look up the Lucas-Lehmer Test for checking primality of huge numbers... it is already in the numbers library as the mersenne function and you can view it with the getAnywhere function.

library(numbers); getAnywhere(mersenne)

I recommend Haskell or Cython to squeeze some extra speed out of this if you actually plan to run it on a number that large - you will want it running in C/C++ and NOT in R, hopefully this lets you read some interesting R code though.

https://cran.r-project.org/web/packages/numbers/numbers.pdf

  • 1
    His number isn't necessarily a Mersenne, so that doesn't really help. – thc Sep 08 '17 at 22:02
  • The algorithm can be applied to any number - it requires some recursion though, another reason Haskell would be a much better fit than R. http://mathworld.wolfram.com/PrattCertificate.html – Kahlan Maki Sep 08 '17 at 22:45
  • There seems to be some confusion about the Lucas and Lucas-Lehmer test, which are different things. Lucas was a busy guy, and also has things like Lucas numbers, Lucas sequences, and the Lucas probable primality test named after him. The Lucas-Lehmer test is for Mersenne numbers. The Lucas test you're thinking of is for doing proofs using n-1. See the 1975 Brillhart-Lehmer-Selfridge paper for full details including many significant extensions. 2500 digits isn't huge, but we would use APR-CL or ECPP at this size unless the number is particularly suited to n +/- 1. – DanaJ Sep 09 '17 at 04:02