How would I go about testing the primality of a large integer in R?

152 views Asked by At

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.

1

There are 1 answers

3
Kahlan Maki On

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