Basic knowledge of number theory

Source: Internet
Author: User

1) Prime number

There are infinitely many theorem primes

Proof. If there is a maximum prime number P_max, set x = (P_1 * p_2 ... * p_max) + 1, at this time if X is a prime number, then x > P_max, contradiction, if X is composite, then there must be one of its prime factor p_x and p_x > P_max, contradiction.

Theorem

It is obvious that the more sparse the distribution is in the larger range.

Determination of Prime number

The brute force method is to enumerate each digit that is less than N, and the simple optimization is to enumerate all prime numbers of 2~SQRT (n).

Because some pseudo-prime order Fermat theorem Converse is not established, and the back of a pseudo-prime table is not very graceful, miller-rabin algorithm with the resulting.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////

Strong pseudo Prime number
Set n is an odd integer greater than 4, s and T are positive integers that make (n-1) =2^s*t, where T is odd, and set B (n) is a set of integers defined as follows:
A belongs to set B (n) when and only if 2≤a≤n-2 and

1:a^tmodn=1
2: Existence of integer i,0<=i<s satisfies a^ ((2^i) *t) mod n=n-1
When n is a prime number, any A in 2 and n-1, a belongs to set B (n)
When n is composite, if a belongs to set B (n), then n is a strong pseudo-prime number based on a (base), and a strong pseudo-evidence of N primality is called.
N is a prime number, indicating that it is a strong pseudo prime for all the bottom

Btest (a,n) {
n is an odd number and returns True. That is, the return true indicates that N is a strong pseudo prime
s←0; t←n-1;//t begins to be even
Repeat
s++;t←t÷2;
Until t mod 2 = 1;//n-1=2st T is odd
X←at mod n;
If X=1 or x=n-1 then return true;//satisfies ①or②.
For i←1 to S-1 do{
X←X2 mod n;
If X=n-1 then return true;//satisfies ②,
}
return false;}

By this definition, it is found that in odd composite less than 1000, the probability of randomly selecting a strong pseudo-evidence is less than 1%.
More importantly, to any odd composite, the proportion of strong perjury is very small
Therefore, we can run the following algorithm multiple times, we can reduce the error probability we can control the range

Millrab (n) {//Odd n>4, returns true-time representation of prime number, false representation composite
A←uniform (2..n-2);
Return Btest (A,n); Tests whether n is a strong pseudo prime number
}//the algorithm is 3/4-correct, partial false.

Repeatmillrob (n,k) {
For I←1 to K do
If Millrob (n) =false Then
return false; Must be composite.
return true;
}

Although Miller-rabin is a random algorithm, the first 9 primes are guaranteed to be correct within the 10^18 range.

The code is simple.

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <  Cmath> #define LL Long longusing namespace Std;int t, prim[15] = {0, 2, 3, 5, 7, one,,, 23};ll U;ll (ll X, ll K, ll MoD) {ll ret = 1;while (k) {if (K & 1) (ret *= x)%= mod; (x *= x)%= mod;k >>= 1;} return ret;} BOOL MR (ll A, ll N) {if (a >= N) return 1;ll w = Mypow (A, u, N), if (w = = 1) return 1;for (int i = 0; i < T; i + +) {if (w = = n-1) return 1; (w *= W)%= N;} return 0;} BOOL PD (ll N) {if (n = = 2) return 1;if (N < 2 | | (! (N & 1))) return 0;t = 0; U = N-1;while ((u & 1) = = 0) u >>= 1, t ++;for (int i = 1; I <= 9; i + +) if (!MR (Prim[i], n)) return 0;return 1;} int main () {int N; scanf ("%d", &n)! = EOF) {int cnt = 0;for (int i = 1; I <= N; i + +) {ll x; scanf ("%i64d", & x); if (PD (x)) CNT + +;} printf ("%d\n", CNT);} return 0;}

  

Basics of Number theory

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.