Screening of prime numbers (herlike sieve and Euler's sieve) and Euler's Sieve
There are many screening methods for prime numbers
Three common methods are provided here.
All the code given below has passed the test here
Heratosney screening method
Long Name: joy: but the code is short.
The idea is very simple. For every prime number, to enumerate its multiples,
Tags: poj number theory Prime sieve Goldbach ' s conjecture
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 40944
Accepted: 15664
Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the foll Owing conjecture:
Every even number greater than 4 can beWritten as the sum of the odd prime numbers.
Let's take a look at the most classic Eratsteni sieve method. Time Complexity of O (n loglog N)
int ANS[MAXN];
void Prime (int n)
{
int cnt=0;
memset (prime,1,sizeof (Prime));
prime[0]=prime[1]=0;
for (int i=2;i
Obviously, when a number is a prime, then his multiples must be composite numbers, the filter tag can be. From the i*i and not from the i*2, because the i*3,i*2 has already been screened by 2, 3.
As a result, we can also find that some
1181 prime numbers in prime numbers (prime number Sieve method)Title Source: SguBase time limit: 1 seconds space limit: 131072 KB score: 0 Difficulty: Basic problemIf a prime number is a prime in the mass list, it is called prime in prime. For example: 3 5 are prime numbers for the 2nd and 3rd respectively, so they are prime numbers in prime numbers. Now give a number n, and find out how many prime numbers are in the smallest prime numbers of >=n (whi
Sieve method, sieve method
2016.1.25
We all know that determining whether a number is a prime number can be solved within the time complexity of O (√ n. However, if the number of prime numbers in [1, n] is required, it is obviously a little slow to judge one by one.
But we know an obvious property: all the prime factors of a given number are smaller than this sum, and there is no prime factor smaller than
The Erato method (Sieve of Eratosthenes) is a method for finding all primes less than n.Start by creating an integer 2~n table, looking for I? Integer, which is programmed to implement this algorithm, and discusses the computational time.Since they are implemented by deletion, and 1 and 0 are not prime numbers, they are removed from 2,3,5 and their multiples.Use data[] to store all the numbers, replacing the good number in the presence of data[]Instea
Toss a night very water of number theory, the whole people are Meng Meng daThe algorithm of O (n) of Euler sieve and prime sieve is mainly seen.The advantage of this algorithm, which is longer than that of a long list of English names, is that a number is not counted more than once, which means that a number is sieved once, mainly after%==0, and the specific explanation of the confusion, especially the solu
A Eratosthenes sieve method (the idea is very good is the complexity is a bit high) (O (Nlognlogn))The multiple of the principle prime number is not a number of prime numbers, the number of which is not more than the multiples of his small (1) of the numbers is prime.2 is the prime number 4,6,8,10 .... Not prime (marking)3 are prime numbers (not marked), 6,9,.... Not prime (same marking)4 are not prime numbers (marked)5 are prime numbers (not marked)
The principle is to first 2-n all the numbers within an array, initialize all the numbers are prime, and then starting from 2 to look for, as long as the mark is a prime number of his all multiples of the mark is changed to composite, and so on. The time complexity is O (NLOGLOGN).Code implementation 1 void Prime_table () 2 { 3 for (int i=2 ;(LL) i1 ; 4 for ( int i=2 ;(LL) i*i) Span style= "color: #008080;" >5 if (Prime[i]) for (LL j=i*i; j0 6 } Prime SievePrime
[Luogu 3383] [TEMPLATE] linear sieve prime number, luogu3383 sieve Prime NumberDescription
For example, given a range of N, You need to query whether M is a prime number (each number is within the range of 1-N)Input/Output Format
Input Format:
The first line contains two positive integers N and M, indicating the query range and the number of queries respectively.
Each row in the next M row contains an integ
Returns the prime number and Euler's prime number by linear sieve (Euler's sieve ).
Time complexity O (n) WHEN n is large, the time used by the Euler's screening method is more and more obvious than the time used by the O (nloglogn) algorithm.
Why?
In the Euler's screening method, each sum is accessed only and the value of f [] is modified once.
For (I = 2; I
CSS3D animation creates a 3d Rotation sieve, and css3d animation creates a sieve
I hope this demo will help you understand the 3d space animation of CSS3 (actually a fake 3D)
First, give a 3d line chart, x/y/z axis line has been marked
The animation rotation for adding special effects is attached below.
You can refer to the above screenshot for understanding based on the demo.
Label: style OS SP C on log R time efI have been writing $ \ Text {o} \ left (n \ log {n }}\ right) $ for a long time, but I have never thought about its optimization. when I saw the linear screening method by chance, I thought it was a good optimization, so I learned it.First, $ \ Text {o} \ left (n \ log {n }}\ right) the $ screening method must be slower than $ \ Text {o} \ left (n \ right) $, although it is not obvious on the current machine. do not set $ \ Text {o} \ left (n \ log {n }}\ ri
Title: Given an n, find all prime numbers of 1~n.
Here are two methods for selecting prime numbers:
The first type: ordinary Sieve method.
Time complexity is O (Nloglogn), and the disadvantage is that a composite number may be filtered multiple times.
Code:
void Prime ()
{
memset (tag,0,sizeof (tag));
Tag[0]=tag[1]=1;
for (int i=2; i*i
The second type: linear sieve.
The time complexity is O (n
The Sieve method to calculate the prime must first establish the sieve, here uses the array as the sieve. The subscript corresponds to the number, the corresponding subscript variable's value flag is in the sieve: for 1 in the sieve, for. The expression has been sifted out,
To get all the primes within the natural number n, it must be less thanThe multiples of all primes are removed, and the remainder is the prime number. Give the range N of the value to be sifted to find the prime number within.1 is neither prime nor composite, remove, first with 2 to sift, that is, 2 left, 2 multiples of the elimination, and then the next prime, that is, 3 sieve, 3 left, the multiples of 3 were removed; then the next prime number 5
Euler sieve (for prime numbers)
Linear sieve with an O (n) degree of complexity. Compared to the composite, it is more efficient to repeat the labeling of the labeled markers. The Euler sieve decomposes the composite into the form of (min factorization * a composite) and determines whether the current composite has been marked by a minimum of factorization.
algorithm increases the number of prime-seeking
Time limit: 1.0s memory limit: 256.0MB problem Description
For a given interval [L, R], calculate the number of primes in the interval. Input Format
Two numbers L and R. output Format
Row, the number of primes in the interval. Sample Input
2 Sample Output
5 data size and conventions
2
Test instructions: Slightly
Analysis: Know the general prime sieve method should all know the principle, here is actual
Hey, Rank 7?
The square root algorithm is also not called the square root algorithm.Make h (n) H (n) the standard for us to classify. (H (n) The expression seems to be in 2015 or 2014 of the national Team paper)If P≥h (n) p\geq H (n), the Sieve method finds the prime number smaller than p in [1,109p] [1,\frac{10^9}{p}], and then selects nth digits.P≤h (n) p\leq H (n), first find the prime number of [1,p] [1,p], and the second-division.As for the value
We know that the Sieve method is generally open up a large array, and then the cycle can be 2,3,5, such as multiple culling, through yesterday's ACM training, I learned several variants, very interesting.
1): General Screening Method:
BOOL Prime[max];
memset (prime,true,sizeof (Prime));
for (k=4;k
J Starting from the I*i, it is clear that the number of smaller than i*i has been removed, but this method note that the size of I may lead to 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.