[the summarization of algorithm]How to list the prime number
2013-12-31 20:48
656 查看
In order to get the list of prime number, I find some ways to do that.
素数判定法:
1、朴素的方法:
#include<stdio.h>
bool isprime(int n);//判断是否是质数的函数
int main()
{
int n;
scanf("%d",&n);
if(isprime(n))
printf("%d is a prime.\n",n);
else
printf("%d isn't a prime.\n",n);
return 0;
}
bool isprime(int n)
{
int i;
for(i = 2;i < n; i++)//原理:利用质数的定义:质数是只能被1和其本身整除的数
{
if(n % i == 0)
break;
}
if(i == n)
return true;
else
return false;
}
复杂度:o(n)
2、对朴素的方法的一阶优化:
复杂度:o(n/2)
3、对朴素的方法的二阶优化:
bool isprime(int n)
{
int i;
for(i = 2;i <= (int)sqrt((double)n); i++)//改进原理:只要2至根号n之间的数不能被n整除,n即为质数。
{
if(n % i == 0)
break;
}
if(i == (int)sqrt((double)n)+1)
return true;
else
return false;
}原理:若n(n∈N,n>1)不能被小于根号n的所有质数整除,则n为质数。
复杂度:O(sqrt(n))
解释:非质数都可以分解为更小的质数乘积的形式,至于为什么是根号n,因为假如x=ab的话,那么ab一定一个小于等于根号另一个大于等于,所以a>根号n的时候b一定<根号n,所以到根号就相当于把所有可能的a都测试过一遍了。
埃拉托色尼选筛法(the Sieve of Eratosthenes)简称埃氏筛法,是古希腊数学家埃拉托色尼(Eratosthenes 274B.C.~194B.C.)提出的一种筛选法。
是针对自然数列中的自然数而实施的,用于求一定范围内的质数。
步骤:1)先把1删除(现今数学界1既不是质数也不是合数)
2)读取2,并把2的倍数删除
3)读取3,并把3的倍数删除
4)读取5,并把5的倍数删除(4是2的倍数,在第2步已经被删除)
5)如上所述,直到所需范围你所有数字被读取过
#include<stdio.h>
#include<stdlib.h>
#define N 1000 //N以内的素数表
int main()
{
bool chart[N+1];
for(int i = 2;i<N;i++)
{
if(chart[i])
for(int j = 2*i;j<N;j+=i)
chart[j] = false;
}
for(int i = 2;i<N;i++)
{
if(chart[i])
printf("%d\n",i);
}
return 0;
}
素数判定法:
1、朴素的方法:
#include<stdio.h>
bool isprime(int n);//判断是否是质数的函数
int main()
{
int n;
scanf("%d",&n);
if(isprime(n))
printf("%d is a prime.\n",n);
else
printf("%d isn't a prime.\n",n);
return 0;
}
bool isprime(int n)
{
int i;
for(i = 2;i < n; i++)//原理:利用质数的定义:质数是只能被1和其本身整除的数
{
if(n % i == 0)
break;
}
if(i == n)
return true;
else
return false;
}
复杂度:o(n)
2、对朴素的方法的一阶优化:
bool isprime(int n) { int i; for(i = 2;i < n/2; i++)//一阶改进原理:只要2至n/2之间的数都不能被n整除,那么n就是质数(因为n/(n/2) = 2,n/((n/2)+k)<2)(k>0) { if(n % i == 0) break; } if(i == n/2) return true; else return false; }
复杂度:o(n/2)
3、对朴素的方法的二阶优化:
bool isprime(int n)
{
int i;
for(i = 2;i <= (int)sqrt((double)n); i++)//改进原理:只要2至根号n之间的数不能被n整除,n即为质数。
{
if(n % i == 0)
break;
}
if(i == (int)sqrt((double)n)+1)
return true;
else
return false;
}原理:若n(n∈N,n>1)不能被小于根号n的所有质数整除,则n为质数。
复杂度:O(sqrt(n))
解释:非质数都可以分解为更小的质数乘积的形式,至于为什么是根号n,因为假如x=ab的话,那么ab一定一个小于等于根号另一个大于等于,所以a>根号n的时候b一定<根号n,所以到根号就相当于把所有可能的a都测试过一遍了。
4、埃拉托色尼筛选法(The
sieve of Eratosthenes)
埃拉托色尼选筛法(the Sieve of Eratosthenes)简称埃氏筛法,是古希腊数学家埃拉托色尼(Eratosthenes 274B.C.~194B.C.)提出的一种筛选法。是针对自然数列中的自然数而实施的,用于求一定范围内的质数。
步骤:1)先把1删除(现今数学界1既不是质数也不是合数)
2)读取2,并把2的倍数删除
3)读取3,并把3的倍数删除
4)读取5,并把5的倍数删除(4是2的倍数,在第2步已经被删除)
5)如上所述,直到所需范围你所有数字被读取过
#include<stdio.h>
#include<stdlib.h>
#define N 1000 //N以内的素数表
int main()
{
bool chart[N+1];
for(int i = 2;i<N;i++)
{
if(chart[i])
for(int j = 2*i;j<N;j+=i)
chart[j] = false;
}
for(int i = 2;i<N;i++)
{
if(chart[i])
printf("%d\n",i);
}
return 0;
}
相关文章推荐
- How to get the number of pages in a Word Document
- 转:How to submit the rows of a repeating table in InfoPath to a SharePoint list
- How to Fix “HTTP Error 403.14 – Forbidden The Web server is configured to not list the contents of this directory”
- How to Calculate the Number of IOPS and Throughput of a Database (文档 ID 2206831.1)
- How to arrange 10 digits so that the product of the some of them is equal to a number represented by the remaining digits
- R12: How to add Microsoft Excel as Type to the Create Template List of Values in BI Publisher (Doc ID 1343225.1)
- How to arrange 10 digits so that the product of the some of them is equal to a number represented by the remaining digits
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- How to delete a large number of data in SharePoint for List when refreshing data?
- How to choose the number of topics/partitions in a Kafka cluster?
- How to count the occurrences of a number?
- How to delete a large number of data in SharePoint for List when refreshing data?
- 1TB is equal to the number of how many GB? 1PB equal to is equal to the number of TB? 1EB PB? | PC-fault.com
- How to choose the number of topics/partitions in a Kafka cluster?
- How to count the number of threads in a process on Linux
- How to choose the number oftopics/partitions in a Kafka cluster?
- How to calculate the number of parameters in CNNs?
- How to calculate the number of parameters of Convolutional Neural Networks(CNNs) correctly?
- How to decide on the correct number of clusters?
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array