Determining if a number is prime

后端 未结 20 1359
悲哀的现实
悲哀的现实 2020-11-30 03:05

I have perused a lot of code on this topic, but most of them produce the numbers that are prime all the way up to the input number. However, I need code which only checks w

20条回答
  •  星月不相逢
    2020-11-30 03:21

    There are several different approches to this problem.
    The "Naive" Method: Try all (odd) numbers up to (the root of) the number.
    Improved "Naive" Method: Only try every 6n ± 1.
    Probabilistic tests: Miller-Rabin, Solovay-Strasse, etc.

    Which approach suits you depends and what you are doing with the prime.
    You should atleast read up on Primality Testing.

提交回复
热议问题