Use array subscripts to correspond to prime numbers within a specified range, and take advantage of a Boolean type value flag that is a prime number

Source: Internet
Author: User

1  Packagecom.jdk7.chapter4;2 3  Public classPrimenumber {4      Public voidGetprime (intrange) {5         Boolean[] SourceData = This. IsPrime (range);6         7         if(! (sourcedata==NULL)){8             intSize =sourcedata.length;9integer[] Resultdata =NewInteger[size];Ten             //Number definition to be placed outside of the increment operation loop, otherwise it may not achieve the desired effect One             intNumber = 0; A              for(inti=1;i<size;i++){ -                 if(Sourcedata[i]) { -Resultdata[i] =i; theSystem.out.print ("prime number [" +i+ "]:" +resultdata[i].tostring () + ""); -                     if(++number% 10 = = 0){ - System.out.println (); -                     } +                 } -             } + System.out.println (); ASYSTEM.OUT.PRINTLN ("Total number of prime numbers:" +Number ); at         } -     } -      -     Private Boolean[] IsPrime (intrange) { -         Boolean[] Tag =New Boolean[Range+1]; -         if(range<=0) {return NULL;} in          for(inti=1;i<range+1;i++){ -             if(i==1){ toTag[i] =false; +}Else{ -Tag[i] =true; the                 } *         } $         //assuming Tag[i] is a prime number, the multiples of prime numbers must be non-primePanax Notoginseng         intsqrt = (int) math.sqrt (range); -          for(inti=1;i<=sqrt;i++){ the             if(Tag[i]) { +                  for(intj= (2 * i); j<=range;j+=i) { ATAG[J] =false; the                 } +             } -         } $         returntag; $     } -}
1  Packagecom.jdk7.chapter4;2 3  Public classPrimenumbertest {4      Public Static voidMain (string[] args) {5Primenumber pn =NewPrimenumber ();6         intRange = 100;7 pn.getprime (range);8     }9 }Ten  One Execution Result: APrime number [2]: 2 primes [3]: 3 primes [5]: 5 primes [7]: 7 primes [11]: 11 primes [13]: 13 primes [17]: 17 primes [19]: 19 primes [23]: 23 primes [29]: 29 -Prime number [31]: 31 primes [37]: 37 primes [41]: 41 primes [43]: 43 primes [47]: 47 primes [53]: 53 primes [59]: 59 primes [61]: 61 primes [67]: 67 primes [71]: 71 -Prime number [73]: 73 primes [79]: 79 primes [83]: 83 primes [89]: 89 primes [97]: 97 theTotal number of prime numbers: 25

Use array subscripts to correspond to prime numbers within a specified range, and take advantage of a Boolean type value flag that is a prime number

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.