0

I have a program that calculates large numbers by storing them as strings, so that I can have very large digits that extend beyond long long.

I can add the strings using a function I've written that models how we humans do addition by hand on paper, and it works. I'm able to add large "string integers" together accurately, even when they have hundreds of digits in them.

I now want to use this to enumerate large numbers and test for primality. The problem is, I don't know how I would do this on a string-int that is very large, because I can't convert it to a long long and then perform the test.

Are there techniques for testing for prime numbers that would work on the digits of the number or something? How would I try to factor large numbers that are represented as strings, and test if numbers are factors of it, etc? How do I approach this problem?

moreQthanAs
  • 1
  • 1
  • 4
  • 1
    This references a large integer library with a test for prime https://stackoverflow.com/questions/52767319/big-primes-loop-with-gmp-library-c There are several choices out there. – Kenny Ostrom Dec 11 '19 at 16:18
  • 4
    You won't like me for saying this, but ditch the whole string way of modelling a number, and use a large number library. The one in Boost is excellent, and even gives you cute `typedef`s like `uint1024_t`. Then it's a matter of doing the mathematics. Google *probable prime* as a starting point. – Bathsheba Dec 11 '19 at 16:18
  • Although it's fun writing prime number algorithms, mathematicans dedicate their professional lives to the field, so it's a good idea to leverage their work! – Bathsheba Dec 11 '19 at 16:24
  • 1
    if you want to do it just for the fun of it I would suggest to implement the missing basic operations (subtraction, modulo,..) and try the "normal" prime test. Dont expect it to be fast, but it will be a starting point for refinement – 463035818_is_not_an_ai Dec 11 '19 at 16:27
  • How would I implement the missing basic operation of modulo? – moreQthanAs Dec 11 '19 at 19:15
  • Divide two numbers with pen and paper ... this gives you divisor and remainder. Implement that in code; won't be fast but gives you a starting point – Daniel Jour Dec 11 '19 at 21:13

0 Answers0