Today, learning the method of prime numbers, it feels great, share it. First of all, compare the two methods: the common method of calculating prime numbers and the method of selecting prime numbers based on the screening method.
- normal method to calculate prime number
The
Prime Test
Time limit:6000 ms
Memory limit:65536 K
Total submissions:29046
Accepted:7342
Case time limit:4000 Ms
DescriptionGiven a big integer number, you are required to find out whether it's a prime number.
InputThe first line contains the
[Algorithm] calculates the prime number by the common method and the limit method, and the algorithm returns the prime number by the limit method.
Prime
, traverse to sqrt (n ), because if the sqrt (n) is not found on the left, then the right side must not be able to find the approximate. The C + + code is as follows:BOOL isprime_2 (int num) { int tmp =sqrt (num); for (int i= 2;i 3) Another methodMethod (2) should be the most common judgment algorithm, the time Complexity O (sqrt (n)), the speed is much faster than the method (1) O (n). Recently on the Internet by chance to see another more ef
2.1 theoretical basis of Miller-RABIN
Fermat TheoremNIs a odd prime number,AIs any positive integer (1 ≤A≤N? 1), thenAN? 1 pmodN. [2]
Theoretical Basis of Miller-Rabin AlgorithmNIs an odd prime numberN? 1 indicates 2S*RForm,RIs odd,AAndNIs any integer of the vegetarian, thenARLimit 1 pmodNOrJ(0 ≤J≤S? 1,JεZ) EquationA2JRWhy? 1 pmodNYes. [2]
This theory is derived
#include #includestring.h>#include#include#include#includeusing namespacestd;//****************************************************************//Miller_rabin Algorithm for prime number testing//fast, and can judge the number of //****************************************************************Const ints= -;//the
There are often beginners to solve the problem of all prime numbers (prime) in N, for this, there are many answers on the internet, but many are either not professional, or only the program does not parse the algorithm, so Sanzang building to do a summary of this problem, discuss the common algorithm of solving
On the internet to see a lot of algorithms to find prime numbers, are checking from 2 to n-1 the number can be divisible by n, can be not prime, the reverse is prime, this is certainly correct, but wasted some unnecessary checks, in fact, as long as the check from 2 to sqrt (n) The
Preface when I was doing ACM today, I encountered a prime number check. When I checked a prime number in a range, if I used the simplest method, the timeout would be serious, so I learned a new algorithm for prime
1. Determining the number of primes
The problem of determining prime number is a very common problem, this paper introduces several common methods of judging.
2. Original algorithm
A prime is defined as a number that, apart fro
random number (8 bits per byte), return 16 binary to 10 binary integer return" "Randomdata=urandom (n)returnInt (binascii.hexlify (randomdata), 16) #===========================================defprimality_testing_1 (n):" "test One, a small prime test, with a small prime number within 100 to detect random numbers x,
Calculate the number of prime numbers in N (N) = 3) using an algorithm.
First, let's talk about the definition of prime numbers. What is a prime number? Except 1 and itself, it cannot be divisible by other natural numbers (except
multiplied by the prime number in the prime number table, such as 9 is a multiple of 3, so after 9*3 can no longer be used to calculate 9*5. So add a line of judgment to the code:
1 //by Morewindows (http://blog.csdn.net/MoreWindows ) 2 Const intMAXN = -; 3 BOOLFLAG[MAXN]; 4 intPRIMES[MAXN/3], pi; 5 voidgetprime_2
Reprinted: http://blog.csdn.net/liukehua123/article/details/5482854
Determine whether a number is a prime number. As we all know, if a number N is a combination, all its factors cannot exceed the square of SQRT (n) -- N, so we can use this property to use the most intuitive method.
To obtain all
callbacks"Sorting problems, only the need to sort the global order when the database will be sorted, if the problem of local sorting, or PHP to read after the sortingSorting algorithm "Focus: Learning with data structures and algorithms"6. To determine whether a number is a prime, the prime is that there is no
1 Const intS = -;//the number of random algorithms, the larger the s, the smaller the probability of the wrong judgment2ll Pow_mod (ll A, ll B, ll MoD) {//A^b%mod3LL ans =1;4A = a%MoD;5 while(b) {6 if(B 1) {7Ans = (ans * a)%MoD;8 }9A = (A * a)%MoD;Tenb >>=1; One } A returnans; - } - BOOLCheck (ll A, ll N, ll X, ll t) { theLL ret =Pow_mod (A, x, n); -LL last =ret; - for(inti =1; I ) { -RET = (ret * ret)%N; + if
number of M, all the number greater than the square root of the product is greater than that number, and then remove it is redundant.
Why the second layer of the filter loop loops only to max/i.Because j*max/i equals Max at this point, the number that needs to be marked as wrong is already up to the scale of the probl
Out of boredom, intends to implement the RSA algorithm againThe first step, large prime number generationIn Java's BigInteger, there's a ready-made method.public static BigInteger probableprime (int bitlength, Random rnd) {Bitlength is the number of bits that is expected to be generated, and Rnd is the random
Entry-level sieve primes-trial Division, Complexity O (n^2)BOOL Long Long N) { for (longlong2; I ) if 0 return false ; return true ;}After learning a period of time algorithm, you should understand the Sieve method to calculate the prime number, the complexity is slightly higher than O (n)#include #includeusing namespacestd;Const intMAXN =500000;BOOLISP
1 # Include 2 3 Bool If_prime ( Int A, Int B) 4 { 5 Int C; 6 While (B> 0 ) 7 { 8 C = A % B; 9 A = B; 10 B = C; 11 } 12 If (A = 1 ) Return True ; 13 Else Return False ; 14 } 15 Int Main () 16 { 17 Int Num, I; 18 While ( True ) 19 { 20 Printf ( " Input the 32-bit int number you want to check: \ n " ); 21 Scanf ( " % D " , Num ); 22 For (I = 2 ; I * I )
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.