PTA: + prime number of prime numbers prime to conjecture portion B1007 B1013. c ++

B1007 prime suspect for

Let d n is defined as: d n = p n + 1 -p n, where p i is the i-th prime number. Clearly there is d 1 = 1, and for n> 1 d n there is an even number. "Primes to guess" that "there is an infinite number of adjacent and poor is a prime number 2."
Now given an arbitrary positive integer N (<10 5), does not exceed Calculate number N satisfies the conjecture of primes.

Input formats:
on a single line is given positive integer N.

Output Format:
Output of the number of prime numbers is not more than N satisfies a conjecture in a row.

sample input:
20

sample output:
4

Analyzing a number n is not a prime number is not required from a 2 ~ n-1 over a determined simply determined from the square root of n 2, if there is no divisible these, then this number is a prime number.

#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
bool judge(int n)//判断一个数字是否是素数
{
 if(n<=1) return false;
 int temp=(int)sqrt(n);
 for(int i=2;i<=temp;i++)
 {
  if(n%i==0) return false;
 }
 return true;
} 
int main()
{
 int n;
 cin>>n;
 int top=n-2;
 int count=0;
 for(int i=3;i<=top;i+=2)
 {
  if(judge(i)&&judge(i+2))
  count++;
 }
 cout<<count;
 return 0;
}

B1013 Primes
order P i denotes the i-th prime number. The current to both positive integers M≤N≤10 . 4 , to make all the output P M P N of a prime number.

Input format:
input given M and N in a row, separated by a space therebetween.

Output format:
output from all primes P M P N, each representing a digit line 10, separated by spaces therebetween, the end of the line must not have the extra space

sample input:
5 26

sample output:
11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101

  1. Determining if a number is a prime number program may copying above a problem, since a given range digital, I chose to use all prime numbers within an array to store the range and then in a given range of output, of course, if not given range or if a large range may be dynamically determines one digit is not a prime number
  2. Since the subject is given maximum number of primes output, so when generating the array of prime numbers stored in the memory does not need to last more than n prime numbers is not saved after the
  3. If it is found at the time of submission of the fourth point WA, it should be a prime number exceeds the upper limit; the value of the 10,000 prime numbers more than 100,000, so if the border is not big enough to begin to open, then it will error
  4. Since the output requirements of each figure represents ten separated by blanks between a line number, the end line no extra spaces. Therefore, when the output of the current input to the final judgment in what position, and then outputs a corresponding separator.
#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;

//const int top=10001;
const int top=111111;
int prim[10000];//素数表 
bool judge(int n)//判断一个数字是否是素数
{
 if(n<=1) return false;
 int temp=(int)sqrt(n);
 for(int i=2;i<=temp;i++)
 {
  if(n%i==0) return false;
 }
 return true;
} 
int count=0;
void prime(int n)
{
 for(int i=1;i<top;i++)
 {
  if(judge(i))
  {
   prim[count++]=i;
   if(count>=n)//及时终止 
   break;
  }
 }
}
int main()
{
 int m,n;
 cin>>m>>n;
 prime(n);
 int num=0;
 for(int i=m;i<=n;i++)
 {
  cout<<prim[i-1];
  num++; 
  if(num%10!=0&&i<n) cout<<" ";//每输出十个就一个换行
  else cout<<endl; 
 }
 return 0;
}
Published 29 original articles · won praise 1 · views 951

Guess you like

Origin blog.csdn.net/qq_44654498/article/details/104691924