Java to find prime number algorithm __java

Source: Internet
Author: User

Package Com.test_java;

Import Java.util.Arrays;
	public class Prime {* * * * * * * * To find the prime number within n/int n;
	
	int i,j; Prime (int inputn) {this.
	N = inputn;
		//Normal Trial Division private void Comprime1_1 () {System.out.println ("less than" + N + "prime number has:");
				for (i=n;i>=0;i--) {to (j=i-1;j>=2;j--) {if (i%j==0) {break;
			} if (j<=2) System.out.println (i);
	} System.out.println (2); }//Loop to sqrt (N), simply explain: the factors are in pairs. For example, 100 of the factors are: 1 and 100,//2 and 50,4 and 25,5 and 20,10 and 10. See No.
	A pair of factors, one of which must be less than or equal to 100 of the open//squared, and another greater than or equal to 100 of the open squared.
		private void Comprime1_2 () {System.out.println ("less than" + N + "prime number has:");
				for (int i=n;i>=0;i--) {for (int j=2;j<= (int) math.sqrt (i); j + +) {if (i%j==0) break;
			if (j = = (int) math.sqrt (i)) System.out.println (i);
			Filter Private boolean[] filternumber (int num) {if (num<=0) {System.out.println ("scope must be greater than 0");
		return null;
		} boolean[] IsPrime = new Boolean[num + 1];
		ISPRIME[1] = false;
		Arrays.fill (isprime,2,num,true); Int n = (int) math.sqrt (num);
				for (int i=1;i<n;i++) {if (Isprime[i]) {for (int j=2*i;j<=num;j+=i) {isprime[j]= false;
	}} return isprime;
		private void ComPrime2 () {boolean[] primes= filternumber (N);
		int num=0;
					if (Primes!=null) {for (int i=1;i<primes.length;i++) {if (primes[i)) {System.out.println (i+ "");
				if (++num%10==0) System.out.println ();
		} System.out.println ();
		
	} System.out.println ("A total" +num+ "a prime number");
		public static void Main (string[] argc) {Prime Prime = new Prime (100);
	Prime.comprime2 ();
 }
}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.