求100以内的素数.c

Algorithm to find prime numbers :
(1)digging to 1 ;
(2)have not been digging behind the removal of P to P The number of multiples of P cut;
(3) has been dug to the integer portion of the n;
(4) not being dug to number is prime.
code as follows:

#include <stdio.h>
#include <math.h>            //调用sqtr()求平方根 
int main(void)
{
	int i,j,n,a[101];         //定义a数组有101个元素 
	for(i=1;i<=100;i++)       //为符合人们习惯a[0]不用 
	a[i]=i;                   //使a[1]到a[100]的值与数字对应(存储数据) 
    a[1]=0;                   //把a[1]挖掉 
	for(i=2;i<sqrt(100);i++)   //除到√100即可 
	for(j=i+1;j<=100;j++)     //用后一个数除前一个数 
	{
		if(a[i]!=0&&a[j]!=0)
		if(a[j]%a[i]==0)
		a[j]=0;               //把非素数挖掉 
	}
	printf("\n");
	for(i=1,n=0;i<=100;i++)
	{
		if(a[i]!=0)               //选出值不为0的数组元素,即素数 
		{
			printf("%5d",a[i]);  //宽度为5列 
			n++;                 //为素数a[i]计数 
		}
		if(n==10)                //输出10个数后换行 
		{
			printf("\n");
			n=0;                //重置n 
		}
	}	
	
	return 0;
}
发布了30 篇原创文章 · 获赞 10 · 访问量 304

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/104631047