Java implementation Euler Contrast on the screen with the bells and whistles prime senior Dafa

I do not know what it is advanced algorithms, Euler sieve yesterday there was a big brother, between midnight inadvertently tell me
Euler screen:
The main implication of this is that I put all the multiples of the number of Dounong out, and then the next cycle when you can skip directly to the

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

public class 求指数 {

public static	ArrayList<Integer> list = new ArrayList<Integer>();
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		int [] num =getPrime(10000);
//		for (int i :num) {
//			System.out.print(i+" ");
//		}
		long end = System.currentTimeMillis();
		System.out.println();
		System.out.println(end-start);
		

		long start1 = System.currentTimeMillis();
		zhishu(10000);
//		for (int i :list) {
//			System.out.print(i+" ");
//		}
		long end1 = System.currentTimeMillis();
		System.out.println();
		System.out.println(end1-start1);
	}
	//神秘求质数,我也不清楚叫什么名字
	public static void zhishu(int n){
		A:	for (int i = 2; i <n; i++) { 
				int sqrt=(int) Math.sqrt(i);
				for(int num:list){
					if(i%num==0){
						continue A;
					}
					else if(num>sqrt)
						break;
				}
				list.add(i);
			}
		}
	//欧拉筛
	public static int [] getPrime(int n) {
		int [] list=new int[n+1];
		int [] prime =new int[n+1];
		int count=0;
		for(int i =2;i<=n;i++) {
			if(list[i]==0)prime[count++]=i;
			for(int j=0;j<count&&i*prime[j]<=n;j++) {
				list[prime[j]*i]=1;
			}
			
		}
		
		return Arrays.copyOf(prime, count);
		
	}

}

The results are shown below:
Here Insert Picture Description
apparent, the top of the screen or Euler

(ง •_•)ง

Released 1449 original articles · won praise 10000 + · views 1.72 million +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104736513