【Mathematical Knowledge】|Prime Numbers, Prime Numbers|Screening Prime Numbers, Decomposing Prime Factors|Simple Mathematical Conclusions

One, a prime number

1, a prime number

Prime numbers are also called prime numbers. First, let's look at the definition of prime numbers. Prime numbers refer to positive integers that can only be divisible by 1 and itself.

In other words, a number is prime if it has no factors other than 1 and itself. For example, 2, 3, 5, 7, 11, 13, etc. are all prime numbers. 4, 6, 8, 9, 10, etc. are not prime numbers.

Frequently Asked Questions about Prime Numbers

When the number less than or equal to 1 is not a prime number,
and the number less than 0 is not a prime number
2 is a prime number,
the values ​​of these negative semi-axis are the most prone to boundary judgment errors.

2. Prime factor

The prime factors are all composed of prime numbers. Use the method of decomposition to decompose it into n = prime number 1 * degree 1 * prime number 2 * degree 2 * ...The
decomposed prime factor of elementary school is equivalent to..., see a picture
insert image description here

Second, whether it is a prime number

The most important thing about is or not is prime is what it means by definition, only 1 and itself can be divided by its Mod 0, which is simple

#include <iostream>
using namespace std ;
int n ;
bool is_prime () 
{
    
     
	if( n <= 1) return false ;
	for ( int i = 2 ; i < n ; i ++ )
		if(n % i == 0)
			return false ;
	return true ;
}
int main () 
{
    
    
	
	cin >> n ;
	if(is_prime())
		cout << "YES , is prime" ;
	else 
		cout << "NO , is not prime" ;
	cout << endl ;
	return 0 ;
}

Simplification, first of all, this algorithm must have a time complexity of O(N) . We know that factors always appear in pairs, and the largest factor is i * i = n. We only need to traverse to this value.

//仅仅修改上文第六行
for(int i = 2 ; i  <= n / i ; i ++ ) 

First of all, the best way to deal with this i is i <= n / i, other methods may report errors, so I won’t go into details

Third, decompose the prime factor

Two points, one is that the overall traversal from two to the maximum can only be n / i , and the value subtracted in the recursive operation, traversal **O(N)** is the largest time complexity

#include <iostream>
using namespace std ;

int n ;

void prime_son()
{
    
    
	for(int i = 2 ; i <= n / i ; i ++ )
	{
    
    
		if(n % i == 0)
		{
    
    
			int ans = 0;
			while ( n % i == 0)
			{
    
    
				n /= i ;
				ans ++ ;
			}
			cout << i << " " << ans << endl ;
		}
	}
	if(n != 1)
		cout << n << " " << 1 << endl ;
	return ;
}

int main ()
{
    
    
	cin >> n ;
	prime_son() ;
	return 0 ;
}

Fourth, how many prime numbers are there in the first n numbers?

We know that there is essentially only one way to search for prime numbers among the first n numbers, which is to assign the known prime numbers to the front. The simplest operation is to assign a value every time.

#include <iostream>
using namespace std ;

const int N = 1e6 + 10;
bool st[N] ;
int get_prime(int n ) 
{
    
    
    int ans = 0 ;
    for( int i = 2 ; i <= n ; i ++ )
    {
    
    
        if(!st[i])
        ans ++ ;
        for(int j = i ; j <= n ; j += i )
            st[j] = true ;
    }
    return ans ;
}

int main () 
{
    
    
    int n ;
    cin >> n ;
    cout << get_prime(n) << endl ;
    return 0 ;
}

But we can also do this every time with the factor of his prime number

#include <iostream>
using namespace std ;

int n ;
const int N = 1e6 + 10 ;

int prime[N] ;
int cnt = 0 ;
bool st[N] ;
int get_prime() 
{
    
    
    for(int i = 2 ; i <= n  ; i ++ )
    {
    
    
        if(!st[i])
        prime[++ cnt]  = i ;
        for(int j = 1; prime[j] <= n / i ; j ++ )
        {
    
    
            st[prime[j] * i ] = true ;
            if(i % prime[j] == 0 ) break ;
        }
    }
    return cnt ;
}

int main ()
{
    
    
    cin >> n ;
    cout << get_prime() << endl ;

    return 0 ;
}

This time complexity is O(N) , so it is called the linear sieve method. Let's explain the principle. First, we first take the prime number operation every time, and then perform the assignment operation on the following prime number set to this point, and then pop up at the point.

Guess you like

Origin blog.csdn.net/wen030803/article/details/132004849