【数据结构与算法】学习笔记-《算法笔记》-18【素数】

将解决两个问题:

  • 如何判断给定的正整数n是否是质数;
  • 如何在较短的时间内得到1~n内的素数表
  1. 如何判断给定的正整数n是否是质数;
    结论:只需要判定n能否被2,3……sqrt(n)(向下取整)中的一个整除,即可判定n是否为素数。 具体证明过程如下:
    在这里插入图片描述
    代码如下:
//判断是否为素数
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;
}

如果n没有接近int型变量的范围上界
可以写成更简单的写法

//判断是否为素数
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. 素数表的获取
    示例一:获得100以内的素数(穷举法)在这里插入图片描述
#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;
}

示例二:“Eratosthenes筛法”
在这里插入图片描述
在这里插入图片描述

#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++)
	{

练习

数素数


在这里插入图片描述

#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;
}

注意点:

  1. 用筛法或者非筛法都可以解决该题,在算法中只需要添加一句控制素数个数的语句:if (count >= n) break;
  2. Find_Prime()函数中要记得==i<maxn而不是i<=maxn,否则程序一运行就会崩溃;
  3. 在main函数中要记得调用Find_Prime()函数,否则不会出结果;
  4. 1不是素数;
  5. 素数表长至少要比n大1。

素数

题目描述
输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数,如果没有则输出-1。

输入
输入有多组数据。
每组一行,输入n。

输出
输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数(素数之间用空格隔开,最后一个素数后面没有空格),如果没有则输出-1。

样例输入
70
样例输出
11 31 41 61

#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

题目描述
Output the k-th prime number.
输入
k≤10000
输出
The k-th prime number.
样例输入
10
50
样例输出
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;
}

注意:maxn要远大于10000而不是设置为10001!

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;
}

这题用到了two pointers中的算法思想。

发布了43 篇原创文章 · 获赞 4 · 访问量 1205

猜你喜欢

转载自blog.csdn.net/weixin_42176221/article/details/101932676