1, period judgment factor
First, we need to define a function that takes the integer n as input and returns a Boolean value indicating whether n is a prime number. Inside the function, we need to make a judgment. If n is less than or equal to 1, it is not a prime number and returns false.
If n is greater than 1, then we need to make a loop. From 2 to the square root of n, judge whether n is divisible by these numbers in turn. If we find a number divisible by n in the loop, then n is not a prime number and returns false. If we check all possible factors and find no number divisible by n, then n is a prime number and returns true.
Step 2 use mathematical formulas
We need to define a function that takes the integer n as input and returns a Boolean value indicating whether n is a prime number. Inside the function, we need to make a judgment. If n is less than or equal to 1, it is not a prime number and returns false. If n is greater than 1, we need to calculate all positive integer factors of n, which can be found from 2 cycles to the square root of n.
Count the number of all factors of n, if the number of factors is equal to 2, then n is a prime number and returns true;; Otherwise return false. When counting the number of factors, we need to pay attention to the situation of repeated calculation. For example, if n is an even number, its factors must include 1 and itself, and only the number of other factors needs to be calculated. If the number of factors is greater than 2, then n can be determined as a composite number by exclusion.
Properties of prime numbers
1, the prime factor is only 1 and itself.
2. The prime number has no other positive factors except 1 and itself.
3. Prime numbers have a unique decomposition theorem, that is, any natural number can be decomposed into the product of several prime numbers.
4. There are infinitely many prime numbers, and their distribution in the natural number system is more and more sparse.
5. The average distribution of prime numbers follows a mathematical theorem, that is, among all natural numbers greater than 1, the number of prime numbers is directly proportional to the square root of natural numbers.
6. The prime factor decomposition of prime numbers is unique, that is, any composite number can be decomposed into the product of several prime numbers, and this decomposition is unique.
7. Prime numbers have important applications in cryptography because they have high security. For example, RSA public key cryptography algorithm is designed based on the difficulty of prime numbers.
8. The calculation of prime numbers is relatively difficult, because there are no other factors, so some simple algorithms can be used to solve them.
The above contents refer to Baidu Encyclopedia-Prime Numbers.