Quickly find prime numbers in C/C++ [full of details]

Personal homepage:There are still unknowns waiting to be explored_C language difficulties, data structures, small projects-CSDN blog

Special column:Number theory_There are still unknown blogs waiting to be explored-CSDN blog

Table of contents

 

1. Violent solution

1. Find the prime number between 1-n (O(n^2))

1. Ideas

2.Code 

 2. Determine whether a certain number x is a prime number

1. Ideas

2.Code

2. Sieve of Eratosthenes (Erthosthenes sieve)

1. Find the prime number between 1-n (O(n^logn))

1. Ideas

2.Code

3. Euler sieve method (Euler sieve method)

1. Find the prime number between 1-n (O(n))

1. Ideas

2.Code


 

1. Violent solution

In previous studies, we wrote about how to find prime numbers within 1-n and how to determine whether a number is a prime number. We learned that a prime number (also called a prime number) is only divisible by 1 and itself.

Based on this, we can start writing.

1. Find the prime number between 1-n (O(n^2))

1. Ideas

First, we need to have numbers between 1-n, and then determine whether they are prime numbers one by one, and then print them.

According to the following code or this idea, we can know that the time complexity of this code is O(n^2). In some games, you may be stuck in time.

Since we know that 1 is not a prime number, the numbers provided start with 2.

2.Code 

#define _CRT_SECURE_NO_WARNINGS  1
#include<stdio.h>
#include<math.h>
void _prime(int n);
int main()
{
	int n;
	scanf("%d", &n);
	_prime(n);
	return 0;
}
void _prime(int n)
{
	//因为我们知道1不是素数,所以直接从2开始进行判断
	for (int i = 2; i <= n; i++)
	{
		//判断是否是素数
		int flag = 0;
		for (int j = 2; j <= sqrt(i); j++)
		{
			if (i % j == 0)
			{
				flag = 1;
				break;
			}
		}
		if (flag == 0)
		{
			printf("%d ", i);
		}
	}
}

 2. Determine whether a certain number x is a prime number

1. Ideas

Loop 2-sqrt(x), and then take the remainder of x. If the remainder result is 0, it is not a prime number. Otherwise, it is a prime number.

2.Code

#include<stdio.h>
#include<math.h>
int isprime(int x);
int main()
{
	int x;
	scanf("%d", &x);
	if (isprime(x) == 1)
	{
		printf("YES\n");
	}
	else
	{
		printf("NO\n");
	}
	return 0;
}
int isprime(int x)
{
	if (x == 1)
		return 0;
	for (int i = 2; i <= sqrt(x); i++)
	{
		if (x % i == 0)
			return 0;
	}
	return 1;
}

2. Sieve of Eratosthenes (Erthosthenes sieve)

1. Find the prime number between 1-n (O(n^logn))

1. Ideas

The idea of ​​​​this sieving method is to sift out all integer multiples of prime numbers.

Similarly, we first provide a number between 1-n. Then judge whether the number is a prime number.If it is a prime number, mark all integer multiples of the prime number as not prime numbers. If it is not a prime number, no processing is performed. In this case, its time complexity will be reduced, but the definition will still be repeated.

Its time complexity is O(n*logn). Although it has been reduced a bit, it is still a bit long.

To determine whether it is a prime number, do not write a judgment function, and perform a loop remainder judgment from 2-sqrt(n).

We first set up an array to label prime numbers. Then we mark everything in the array as prime numbers by default.

Then we start from the number 2 (we know that the number 2 is a prime number) and label its integer multiples. The same is true for 3. In this way, prime numbers can be marked.

Since we know that 1 is not a prime number, the numbers provided start with 2.

2.Code

#include<stdio.h>
#define MAX 100
int prime[MAX];//记录素数,默认2-n之间全是素数,然后再进行筛选
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 2; i <= n; i++)
	{
		//因为我们知道1不是素数,所以直接从2开始进行判断
		if (prime[i] == 0)
		{
			for (int j = 2 * i; j <= n;j+=i)
			{
				//0代表素数,1代表非素数
				prime[j] = 1;
			}
		}
	}
	for (int i = 2; i <= n; i++)
	{
		if (prime[i] == 0)
			printf("%d ", i);
	}
	return 0;
}

三、 Euler 法(Euler law)

1. Find the prime number between 1-n (O(n))

1. Ideas

Euler sieve is the only sieve method that can filter out a number. There is no repeated calculation, so the time complexity is O(n).

Core code: i % prime[j] == 0 (i is a multiple of prime number).

2.Code

#include<stdio.h>
#define MAX 100
int prime[MAX];//用来记录素数,0为素数
int record[MAX];
int cnt;//记录素数的大小
void Euler(int n);
int main()
{
	int n;
	scanf("%d", &n);
	Euler(n);
	for (int j = 0; j < cnt; j++)
	{
		printf("%d ", prime[j]);
	}
	return 0;
}
void Euler(int n)
{
	for (int i = 2; i <= n; i++)
	{
		if (!record[i])//如果当前是素数,记录下来
		{
			prime[cnt++] = i;
		}
		for (int j = 0; j < cnt && i * prime[j] <= n; j++)
		{
			record[i * prime[j]] = 1;
			if (i % prime[j] == 0)
				break;
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_73435980/article/details/134210427