5, "sieve" seeking prime number table

5, entitled: "Method" prime number table seek

The so-called "Method" is a method of famous ancient Greek mathematician Erato color out of the Trinity, the principle is: Write on a piece of paper all the integers from 1 to 100, and then one by one to determine whether they are prime, find a non-prime number on dig it, it is the last remaining prime number.
The specific process is:
(1) a first dig
(2) by removing the respective number 2 behind it, to dig divisible by 2, that is a multiple of 2 dig
(3) is removed with 3 behind you number, to dig a multiple of 3
(4) and so on until all the multiples of 10 dug up.

DETAILED digging method:
the latter, it is determined that a number is not prime, put it set to 0, when the output lists of primes, if (a [i] = 0!) { Output a [i]}
Second, it is determined that a number is not after the prime number, the number of all moved forward one behind it, a [j] = a [ j + 1]; j ++; n -; (n is the number of numbers is not gouged)
(missing two methods have inferior)

A method of selection:

#include<stdio.h>
int main(){
	int a[100], i, j;
	
	//将1-100输入数组中 
	for(i=0; i<100; i++){
		a[i]=i+1;
	}
	
	//挖掉1
	a[0]=0;
	
	//挖掉能被2、3...9、10等整除的数
	for(j=2; j<=10; j++){
		for(i=j; i<100; i++){	//i=j 表示以j对数组各元素求余时,从下标为j的数组元素开始求余 
			if(a[i]%j==0){
				a[i]=0;
			} 
		}
	}
	
	int cnt=0;					//cnt记录输出素数的个数 
	for(i=1; i<100; i++){
		if(a[i] != 0){
			printf("%3d ", a[i]);
			cnt++;
		}
		if(cnt%10==0){
			printf("\n");		//每输出10个素数换一行 
		}
	} 
	return 0;
}
Published 16 original articles · won praise 0 · Views 329

Guess you like

Origin blog.csdn.net/NAU_LHT/article/details/104144883