-2

Is this a 'real' task, that can be written on any language ( C/C++, for example )

So, my task is 'generate' random number with length over 50 digits ( maximum = 200 )?

Then, i must check this number on primality test.

So, is this task 'real' and how many time/resources it will consume?

alternative way is to generate prime Mersenn numbers or other numbers from special class ( which class could be used? )

gaussblurinc
  • 3,642
  • 9
  • 35
  • 64
  • 3
    what do you mean by "real"? Are you asking us if this is homework? you should know... – PlasmaHH Feb 09 '12 at 11:31
  • Proving a number is prime is incredibly difficult (well, trivial but very very slow). GMP (The GNU Multiple Precision Arithmetic Library) has a probable prime function which can run tests (you can tell it how many iirc) to tell you if a number is "probably" prime. http://gmplib.org/manual/Number-Theoretic-Functions.html#Number-Theoretic-Functions It's a little fiddly to use because it was really meant to be C, but you can use it reasonably well from c++. – BoBTFish Feb 09 '12 at 11:35
  • 'real' - task, which need only one computer and time-consuming is not an hour. personal usage, yes. – gaussblurinc Feb 09 '12 at 12:02
  • @BoBTFish: This is not true. Deciding whether given number is prime can be done in polynomial time, (the algorithm)[http://primes.utm.edu/prove/prove4_3.html] was presented in 2002 by Agrawal, Kayal and Saxena. In practice, we use probabilistic Rabin/Miller which is also fine since it is also polynomial provided ERH is true (what most mathematicians believe). – Kris Feb 09 '12 at 12:33
  • @Krystian: where did he say it is not possible to do in polynomial time? AKS is still difficult and slow for ~200 digit numbers. – PlasmaHH Feb 09 '12 at 12:39

2 Answers2

2

There are efficient probabilistic algorithms for primality testing like Miller-Rabin that are usually used for such purposes.

Picking a random number and testing for primality using a randomized algorithm is efficient since the density of primes guarantees you that for n-bit numbers you need to pick around n numbers to test.

Kris
  • 1,388
  • 6
  • 12
1

Use the Miller-Rabin primality test. Also, you'll need a library for big decimals, as numbers of 50 digits do not fit in a native datatype.

Here is a Javascript implementation of Miller-Rabin, which does 50 iterations of Miller-Rabin. It completes in a couple of seconds.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175