Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

...

Posted by dawei007 at 2010-04-23 13:39:41 on Problem 3518
#include<iostream>
#include<cmath>
using namespace std;
#define MAX 1299709

int main()
{	
	int i,j,n;
	char prime[MAX+1];

	memset(prime,1,(MAX+1));
	prime[1]=0;
	for(i=2;i<=(int)sqrt((double)MAX);i++)
	{
		if(prime[i])
		{
			j=i*2;
			while(j<=MAX)
			{
				prime[j]=0;
				j+=i;
			}
		}
	}
	while(cin>>n)
	{
		if(n==0)	break;
		i=n;
		while(!prime[i])	--i;
		j=n;
		while(!prime[j])	++j;
		cout<<j-i<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator