Home > Article > Web Front-end > JS Use 6N±1 Method to Find Prime Numbers Example Tutorial_Javascript Skills
Use the 6N±1 method to find prime numbers
Any natural number can always be expressed in one of the following forms:
6N, 6N 1, 6N 2, 6N 3, 6N 4, 6N 5 (N=0, 1, 2,...)
Obviously, when N≥1, 6N, 6N 2, 6N 3, 6N 4 are not prime numbers. Only natural numbers in the form of 6N 1 and 6N 5 may be prime numbers. Therefore, except for 2 and 3, all prime numbers can be expressed in the form of 6N±1 (N is a natural number).
Based on the above analysis, we can construct another sieve to screen only natural numbers in the shape of 6 N±1. This can greatly reduce the number of screenings and further improve the operating efficiency and speed of the program.
The following code requires a natural number greater than 10