Number of bonded continuous number icodelab

description

Xiaoming sometimes found that the number of all the numbers between two quality numbers are combined, for example: the presence of three consecutive between 7 and 11 together there are 3 consecutive engagement between number 8,9,10.19 number 20 to 23, 21, 22. Now given a number, please comprising determining the number of consecutive number of the number, including the engagement.

Entry

A plurality of sets of data (group not more than 50,000), one per line a positive integer a, 0 represents the last line of the input end of input.

Export

For each line of input data, comprising a given number of consecutive number of the number, including the engagement.

Sample input 1

6
100
7
0

Sample Output 1

1
3
0

prompt

a< =1000,000。

Ideas: first screening in addition to composite number

Each read a number is not prime time has been increased, until j is a prime number

Skip composite number has been found

See Notes

Code:

#include <the iostream> 
#include <cstdio> 
#include <CString> 

the using namespace STD; 
#define NUM 1,299,709 
BOOL U [NUM +. 1]; 
int ANS [NUM +. 1]; 
void PREPARE () {// Å sieves 
	for ( int i = 2; i <= NUM; i ++) u [i] = 1; // first that all numbers are prime numbers 
	for (int I = 2; I <= NUM; I ++) { 
		IF (U [I] ) { 
			for (int J = I; J <= NUM / I; J ++) U [I * J] = 0; // screening Composite number 
		}		 
	} 
	for (int I = 2; I <= NUM; I ++) {		 
		if {// number bonded to (U [I]!) 
			int j = I; 
			the while (j <= NUM && U [j]!) j ++; // been increased when j is not a prime number, until j is a prime number 
			j -; // j is decremented back to make a position j, the location is a composite number 
			for (int k = i; k <= j; k ++) ans [k] = j-i + 1; // k from to All consecutive number j together are equal numbers corresponding to 
			i = j; // skip composite number that has been found 
		} the else { 
			ANS [I] = 0; 
		} 
	}
} 

int main(){
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
	int k;
	prepare();
	while(scanf("%d",&k)>0&&k>0){
		printf("%d\n",ans[k]>0?ans[k]:0);
	}
}

 

 

 

Guess you like

Origin www.cnblogs.com/mysh/p/11291567.html