[] Data structure and algorithm study notes - "algorithm notes" -18 [prime]

We will address two questions:

  • How to determine whether a given positive integer n is prime;
  • How to get primes in Table 1 ~ n in a short time
  1. How to determine whether a given positive integer n is prime;
    Conclusion:N can be determined only 2,3 ...... sqrt (n) (rounded down) of a divisible, can be determined whether or not a prime number n.DETAILED demonstrated as follows:
    Here Insert Picture Description
    Code as follows:
//判断是否为素数
bool isPrime(int n)
{
	//特判
	if (n <= 1)	return false;
	//遍历2~根号n
	for (int i = 0; i < (int)sqrt(1.0*n); i++)
	{
		if (n%i == 0)	return false;
	}
	return true;
}

If n is not near the upper boundary of the range of the int type variables
can be written in a simpler wording

//判断是否为素数
bool isPrime(int n)
{
	//特判
	if (n <= 1)	return false;
	//遍历2~根号n
	for (int i = 0; i*i <= n; i++)
	{
		if (n%i == 0)	return false;
	}	
	return true;
}
  1. Prime number table acquisition
    Example 1: less than 100 to obtain prime (exhaustive)Here Insert Picture Description
#include "stdafx.h"
#include <cstdio>
#include <string>
#include<iostream>
typedef long long LL;
const int maxn = 101;
bool p[maxn] = { false };//标签是否为素数
int prime[maxn], count = 0;//存放所有素数并计数

//判断是否为素数
bool isPrime(int n)
{
	//特判
	if (n <= 1)	return true;
	//遍历2~根号n
	for (int i = 2; i < (int)sqrt(1.0*n); i++)
	{
		if (n%i == 0)	return true;
	}
	return false;
}


//生成素数表
void Fine_prime()
{
	for (int i = 2; i < maxn; i++)//注意不能写成<=!
	{
		p[i] = isPrime(i);
		if (!p[i])
		{
			prime[count++] = i;
		}
	}
}

int main()
{
	Fine_prime();
	for (int i = 0; i < count; i++)
	{
		printf("%d\n", prime[i]);
	}
	return 0;
}

Example Two:“Eratosthenes筛法”
Here Insert Picture Description
Here Insert Picture Description

#include <cstdio>
#include <string>
#include<iostream>

const int maxn = 101;
bool p[maxn] = { false };//标签是否为素数
int prime[maxn], count = 0;//存放所有素数并计数

//生成素数表
void Fine_prime()
{
	for (int i = 2; i < maxn; i++)//注意不能写成<=!
	{
		if (p[i] == false)//如果i是素数
		{
			prime[count++] = i;	
			//筛去所有i的倍数
			for (int j = i * 2; j < maxn; j += i)//注意不能写成<=!
			{
				p[j] = true;//不是素数
			}
		}
		

	}
}

int main()
{
	Fine_prime();
	for (int i = 0; i < count; i++)
	{

Exercise

Number prime


Here Insert Picture Description

#include <cstdio>
#include <string>
#include<iostream>
// #include <time.h>
// #include<stdlib.h>
//#include<math.h>
//#include <vector>
//#include <iostream>
//#include <string>
//#include <algorithm>

//using namespace std;
typedef long long LL;
const int maxn = 10000;
bool p[maxn] = { false };//标签是否为素数
int prime[maxn], count = 0;//存放所有素数并计数
int m, n;

//生成素数表
void Find_prime()
{
	for (int i = 2; i < maxn; i++)//注意不能写成<=!
	{
		if (count >= n)
			break;
		if (p[i] == false)//如果i是素数
		{
			prime[count++] = i;	
			//筛去所有i的倍数
			for (int j = i * 2; j < maxn; j += i)//注意不能写成<=!
			{
				p[j] = true;//不是素数
			}
		}
	}

}

int main()
{
	scanf("%d%d", &m, &n);
	Find_prime();
	for (int i = m; i < n; i++)
	{
		printf("%d", prime[i]);
		if (i != n)
			printf(" ");
	}
	printf("\n");
	return 0;
}

important point:

  1. Sieve or a non-sieve method can solve this problem, requiring only the addition of a prime number of control algorithm in the statement: if (count >= n) break;;
  2. Find_Prime () function to remember == i <maxn rather than i <= maxn, otherwise it will run a program crashes;
  3. In the main function to remember to call Find_Prime () function, otherwise it will not be the result;
  4. 1 is not a prime number;
  5. Prime number n is greater than the length of the table at least 1.

Prime numbers

Description Title
enter an integer n (2 <= n <= 10000), the output of all requires this integer from 1 to (and does not include the integer 1) th bit is a prime number, and if not then outputs -1.

Enter
input multiple sets of data.
Each row, enter n.

Output
the output of all the integers from 1 to (and does not include the integer 1) th bit is 1 primes (prime numbers separated by spaces, and finally no space behind a prime number), if not then outputs -1.

Input Sample
70
Sample Output
11314161

#include <cstdio>
#include <string>
#include<iostream>
 
const int maxn = 10001;
bool p[maxn] = { false };//标签是否为素数
int prime[maxn], count = 0;//存放所有素数并计数
int n;
 
//生成的素数表
void Find_prime()
{
    count = 0;
    for (int i = 2; i < n; i++)
    {
        if (p[i] == false)
        {
            if(i%10==1)
                prime[count++] = i;
            for (int j = i + i; j < n; j += i)
            {
                p[j] = true;
            }
        }
    }
}
 
int main()
{
    while (scanf("%d", &n) != EOF)
    {
        Find_prime();
        if (!count)
            printf("-1\n");
        else
        {
            for (int i = 0; i < count; i++)
            {
                printf("%d", prime[i]);
                if (i != count - 1)
                    printf(" ");
            }
            printf("\n");
        }
    }
    return 0;
}

Prime Number

Title Description
Output the k-th prime number.
Input
k≤10000
Output
The k-th prime number.
Sample Input
10
50
Sample Output
29
229

#include <cstdio>
#include <string>
#include<iostream>

typedef long long LL;
const LL maxn = 1000001;
bool p[maxn] = { false };//标签是否为素数
int prime[maxn], count = 0;//存放所有素数并计数
int n;

//生成的素数表
void Find_prime()
{
	count = 0;
	for (int i = 2; ; i++)
	{
		if (count == n)	break;
		if (p[i] == false)
		{
			prime[count++] = i;
			for (int j = i + i; j < maxn; j += i)
			{
				p[j] = true;
			}
		}
	}
}

int main()
{
	while (scanf("%d", &n) != EOF)
	{
		Find_prime();
		printf("%d\n", prime[n - 1]);
	}
	return 0;
}

Note: maxn much larger than 10000 to 10001 instead!

Goldbach’s Conjecture

题目描述
Goldbach’s Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
输入
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
输出
Each output line should contain an integer number. No other characters should appear in the output.
样例输入
4
10
16
0
样例输出
1
2
2

#include <cstdio>
#include <string>
#include<iostream>

typedef long long LL;
const LL maxn = 1000001;
bool p[maxn] = { false };//标签是否为素数
int prime[maxn], count = 0;//存放所有素数并计数
int n;

//生成的素数表
void Find_prime()
{
	count = 0;
	for (int i = 2; ; i++)
	{
		if (count == n)	break;
		if (p[i] == false)
		{
			prime[count++] = i;
			for (int j = i + i; j < maxn; j += i)
			{
				p[j] = true;
			}
		}
	}
}

int main()
{
	while (scanf("%d", &n), n)
	{
		Find_prime();
		int num = 0;
		int i = 0, j = count - 1;//要不要-1?
		while (i <= j)//有没有等号?
		{
			if (prime[i] + prime[j] == n)
			{
				i++;
				j--;
				num++;
			}
			if (prime[i] + prime[j] < n)
				i++;
			if (prime[i] + prime[j] > n)
				j--;
		}
		printf("%d\n", num);
	}
	return 0;
}

This theme uses two pointers thinking algorithm.

Published 43 original articles · won praise 4 · Views 1205

Guess you like

Origin blog.csdn.net/weixin_42176221/article/details/101932676