Cod sursa(job #145723)

Utilizator AndreyPAndrei Poenaru AndreyP Data 29 februarie 2008 11:33:56
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include<stdio.h>
char c[2000001];
int v[1001];
int main()
{
	freopen("ciur.in","r",stdin);
	freopen("ciur.out","w",stdout);
	c[0]=c[1]=1;
	int n,i,j,k=0;
	scanf("%d",&n);
	for(i=4; i<=n; i+=2)
		c[i]=1;
	for(i=3; i*i<=n; i+=2)
	{
		if(!c[i])
		{
			for(j=i+i+i; j<=n; j+=i+i)
				c[j]=1;
		}
	}
	for(i=n; i>1; i--)
	{
		if(!c[i])
		{
			k++;
			if(k<1001)
				v[k]=i;
		}
	}
	printf("%d\n",k);
	if(k>=1000)
	{
		for(i=1000; i>0; i--)
			printf("%d ",v[i]);
	}
	else
	{
		for(i=k; i>0; i--)
			printf("%d ",v[i]);
	}
	printf("\n");
	return 0;
}