Seeking a number is prime

------------ ------------ restore content begins

Whether seeking a number is prime there are many solving methods listed a few below for reference

First, the general method for finding

  Ideas: a seek cycle, cycle 2 starts from the root up to n.

       Why only need to determine the root of n?

    Since n = √n * √n, n factor √n addition, others are present in pairs, and must be greater than a less than √n √n,

   Suppose n is not a prime number, there is a factor greater than √n (not n itself), then n must be less than a factor of √n corresponding thereto. This is relatively simple.

 

 

 The time complexity is O (n).

#include<bits/stdc++.h>
using namespace std;
int sushu(int n)
{
  if(n==1) return 0;
  int k;
  k=sqrt(n);
  for(int i=2;i<=k;i++)
  {
    if(n%i==0) return 0;
  }
  return 1;
}
int main ()
{
  int n;
  while(cin>>n)
  {
    int l=sushu(n);
    if(l) printf("ture\n");
    else
    {
      printf("no\n");
    }
  }
  return 0;

}

Second, a prime number is determined sieve. (This may be a bit overkill, but for large data or can)

     Prime number sieve sieve and Egypt can be divided into linear sieve.

  1 Egypt sieve.

  It will be several multiples mesh sieve when prime number times out, so only the screen like prime numbers, the prime numbers only primes screened out. It is to find a prime number, a multiple of all his mark is a composite number. But you will find that some numbers will be marked several times, such as 2, 3 are marked by 12, but this will be a waste of time. (In general, then, if the data is then 1e9, on the line with this, the time complexity is nloglogn).

         Template is given below:

    

const  int N = 1E7 + . 1 ;
 int Prime [N]; // play table, the table inflicted prime, is all prime numbers within the scope of the record
 int B [N]; // equivalent bucket sort, has been originally recorded the prime mark, and then skip on the line in the later screening.
int CNT = 0 , MAX1 = 1E7;
 int the init ()
{
    Memset (B, 1 , the sizeof (B)); // the b [N] array all initialized to 1;
    B [ 0 ] = B [ . 1 ] = 0 ;
     for ( int I = 2 ; I <= MAX1; I ++ ) // start looking primes.
    {
        IF (B [I]) // prime mark will first appears, since this number back to the multiple tag, then encounters a multiple number of skip is again
        {
            Prime [ ++ CNT] = I; // for playing table 
for ( int J = 2 ; J * I <= MAX1; J ++ ) // number of this multiple tag of (i) { b[i*j]=0; } } } return 0; }

Generally with Egypt sieve enough.

 2, linear sieve

 Prime number sieve can be optimized, Common linear sieve method, while greatly reducing the time required prime numbers, but in fact still do many repetitive operations, such as 2 * 3 = 6, the prime number when the screen again, the prime numbers 3 when it screens again. If only the screen is less than equal to the product of prime numbers prime number i and i, both will not cause repeated screening, they will not miss. Time complexity is almost linear. (Linear range can be screened in a sieve prime 2e9) of

  On the code:

const int N=1e7+1;
int prime[N];
int b[N];
int cnt=0,max1=1e7;
int init()
{
    memset(b,1,sizeof(b));
    b[0]=b[1]=0;
    for(int i=2;i<=max1;i++)
    {
        if(b[i])
        prime[++cnt]=i;
        for(int j=1;j<=cnt&&prime[j]*i<=max1;j++)
        {
            b[prime[j]*i]=0;
            if(i%prime[j]==0) break;
        }

    }
    return 0;
}

We want to screen a prime number from 1 to n, and then the first default they are all prime numbers, the outermost layer enumerate
1 to the number n of all,
if it is a prime number, it is added to the list of prime numbers,                                                                                          
for each enumeration of i, gold For table of prime numbers, then they will mark their primes i
times the number is not a prime number, (not a multiple of a prime number of prime numbers)
enumeration prime table when to stop? Enumeration to a minimum quality factor i, the mark can complete stop
, and ensure that each number is only his youngest prime factors screened out.
For example: I = the outer layer 15, the list of prime numbers: 2,3,5,7,11,13
2 * 15 = 30, the 30 screened out; 3 * 15 = 45, the 45 screened out, because 15% 3 == 0, back
out of the inside of the loop;
15 sieve 3 is out, because 3 is the smallest prime factor of 15.

3, know that Egypt and linear sieve screen, the next step is on the main course - how to determine a prime number.

sqrt discriminating O (√N))
if x can be represented as a multiplication factor of two
x = a * b assuming a <= b
then x> A * A =
a <= √x
only enumerate a <= √x on it

int sushu ( Long  Long n-)
{
    int flag=0;
    for(int i=1;prime[i]<sqtr(n);i++)
    {
        if(n%prime[i]==0) {flag=1;break;}
    }
    if(n==1) flag=1;
    return flag;
}

Then be sure to write the main function init (); to call succeeds.

for example:

No knowledge to know whether, should be Lvfeihongshou

Problem:1704

Time Limit:1000ms

Memory Limit:165535K

Description

Song of the woman when married, to investigate the man's satisfaction (satisfaction = wealth value + status value - the value of her mother's violence)
If satisfaction is a prime number, then you can marry, or not consent to the marriage.

Input

The first line is n (1 <= n <= 50), indicates the number of sets of data;
Followed by n lines, each line three positive integers x, y, z (wealth value, the status value, the value of violence); (1 <= x, y, z <= 1e14)

Output

If you can agree to the marriage, output "yes";
Otherwise, output "no";

Sample Input

2
10 2 1
10 2 2

Sample Output

yes
NO 
------------------------------------------------- -----
this is a typical prime number sieve
Prime number sieve on the line, because it is 1e14, so the screen to 1e7 on the line!
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e7+1;
int prime[N];int b[N];
int cnt=0,max1=1e7;
int init(){
    memset(b,1,sizeof(b));
    b[0]=b[1]=0;
    for(int i=2;i<=max1;i++)
    {
        if (b[i])
        prime[++cnt]=i;
         //for(int j=2;j*i<=1000;j++)
          //b[i*j]=0;

         for(int j=1;j<=cnt&&prime[j]*i<=max1;j++)
            {
                b[prime[j]*i]=0;
                if (i%prime[j]==0) break;
            }



    }

    return 0;}
int su(LL n){
    int flag=0;
    for(int i=1;prime[i]<=sqrt(n*1.0);i++)
    if (n%prime[i]==0) {flag=1;break;}
    if (n==1) flag=1;
    return flag;}
int main(){     int n;     LL x,y,z;     init();     cin>>n;     while(n--)     {       cin>>x>>y>>z;       if (su(x+y-z)==0) cout<<"yes"<<endl;       else             cout<<"no"<<endl;     }     return 0;}

 

Guess you like

Origin www.cnblogs.com/zhuyukun/p/12380063.html