Seeking primes - prime screening method comprising

In fact, the prime sieve can be considered a relatively very simple way, but I had always been with some of their own understanding and writing code, and decided to learn about the system.
In general, we determine whether a number is a prime number, this number is generally look except 1 and itself is whether there are factors, the core code is as follows

int n,i;
scanf("%d",&n);
for(i=2;i<n;i++){
	if(n%i==0)break;
}
if(i>=n)printf("ture");

Of course, this method of determination is significantly much, not completely from 2 to n-1 is determined, in fact, if non-prime number a = i * j; i <= j, it is clear that since a large number if there is a factor that it must also a small factor, and small factor i <= sqrt (a); so, as long as enumerated from 2 to sqrt (a) on the line. code show as below:

int n,i,x;
scanf("%d",&n);
x=(int)sqrt(n);
for(i=2;i<=x;i++){
	if(n%i==0)break;
}
if(i>=n)printf("ture");

In fact, if you judge more prime numbers, then you actually just hit the table on the line. What is a play table, you first play is a prime table, only after every time you check in the table it is not a prime number on it. While playing table approach is this: In the beginning, all the numbers you shilling is a prime number, and then weed out the non-prime number of prime numbers from the table: methods, see the following code

#include<cstdio>
#include<cstring>
bool Pri[100010];       //一般这么大够了,代表从1筛到100000
void prime(){
	memset(Pri,0,sizeof(Pri));
	int i,j;
	Pri[1]=1;           //1不是素数 
	for(i=2;i*i<=100000;i++){        //2到100000的非素数必然含有2到sqrt(i)的因子 
		if(Pri[i]==0){
			for(j=i*i;j<=100000;j+=i){    //因为如果j=i*p,p<i开始,实际上以p为因子的非素数已经被筛了一次 ,不用再筛了 
				Pri[j]=1;
			}
		}
	}
}
int main()
{
	prime();
	int n;
	while((scanf("%d",&n))>=1){          //不同题目要求结束方式不一样 
		if(Pri[n]==0)printf("ture\n");
		else printf("false\n");
	}
	return 0;
} 

当然,理解本质是最重要的,下面有一道题:
To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1…20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as better than others. In particular, a cow whose serial number has the highest prime factor enjoys the highest social standing among all the other cows.

(Recall that a prime number is just a number that has no divisors except for 1 and itself. The number 7 is prime while the number 6, being divisible by 2 and 3, is not).

Given a set of N (1 <= N <= 5,000) serial numbers in the range 1…20,000, determine the one that has the largest prime factor.
Input

  • Line 1: A single integer, N

  • Lines 2…N+1: The serial numbers to be tested, one per line
    Output

  • . 1 Line:. The Largest of The Integer with the If there are Prime factor Within last More One, One Output The Earliest in that the Appears The INPUT File.
    The Sample the Input
    . 4
    36
    38 is
    40
    42 is
    the Sample the Output
    38 is
    Sponsor
    subject of several top means seeking the number of the maximum number of prime factors, in fact, can be used to play table to do, how to do it:
    first, the original table mean: prime number is 0, a non-prime number is 1.
    and now, primes itself, non-prime number is the greatest prime factor.
    So from 2 to find a prime number, then it has been up, looking for his multiples, and prime factor of his multiples will be reset to this prime number, because we so traverse, prime back must be greater than previous prime number, so you can directly assign . Another point, this table should remember primes set for themselves, and the inner loop can not start from the i * i, and i want to start, because of the greater number of quality behind this.
    code show as below:

#include<cstdio>
#include<cstring> 
int maxpri[20010];
void maxp()
{
	int i,j;
	memset(maxpri,0,sizeof(maxpri));
	maxpri[1]=1;
	for(i=2;i<=20005;i++){
		if(maxpri[i]==0){
			for(j=i;j<=20005;j+=i){
				maxpri[j]=i;            //不断替换,由素数找倍数,后面素数大于前面的 
			}
		}
	}
}
int main()
{
	maxp();
	int n,i,p,tprim;
	while((scanf("%d",&n))>0){            //输入n,代表接下来输入n个数 
	int max=0,how=0;
	for(i=0;i<n;i++){
		scanf("%d",&p);        //输入这n个数 
		tprim=maxpri[p];       //取最大素数 
		if(tprim>max){
			max=tprim;         //保存最大素数 
			how=p;             //保存含最大素因子的数 
		}
	}
	printf("%d\n",how);
}
	return 0;
}
Published 14 original articles · won praise 16 · views 1285

Guess you like

Origin blog.csdn.net/weixin_45981189/article/details/104978324