Java algorithm for solving prime numbers

Source: Internet
Author: User

Number of primes in 100:
   public void A () {for              (int i = 2; I <=; i++) {                  int temp = (int) math.sqrt (i);                  I put that aqrt alone, so the speed is a little bit faster, although the change in 100 is not small, but if it is a prime number within 10000000?                  if (I <= 3) {                      System.out.println (i + "is a Prime");                  } else {for                      (int j = 2; J <= Temp; j + +) {//Put math. sqrt (i) converted to int class                          if (i% j = 0) {break                              ;                          }                          if (J >= temp) {                              System.out.println (i + "is a Prime");}}}}            

(ii) The number of primes between n-m
  public void Sushu (int n, int. m) {for              (int i = n; i < m; i++) {                  int temp = (int) math.sqrt (i);                  if (i<=3) {                      System.out.println (i);                  }                  for (int j = 2; J <= Temp; j + +) {                      if (i%j==0) {break                          ;                      }                      if (j>=temp) {                          System.out.println (i);}}              }          


Java algorithm for solving prime numbers

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.