Java program entry 50 questions analysis: 002

Source: Internet
Author: User
 

【Program2] Question: determine the number of prime numbers between-and output all prime numbers.

Program Analysis 2: Shenma is a prime number. I don't know the prime number, I know it. That's all !! Prime number. In a natural number greater than 1, except for 1 and the integer itself, it cannot be divisible by other natural numbers. In other words, only the natural numbers of two positive factors (1 and itself) are prime numbers. When I think about this question, I first think about the profiteering solution. It is only between 101--200 and there are not many prime numbers returned by public int Sushu (int I, then the for loop 101--200 will be ready.

 
 

Package endual; public class qanswer {public int Sushu (int n) {Boolean issushu = false; For (INT I = 2; I <n-1; I ++) {// This number is enough to get the remainder by Division of 2. // if the remainder is 0, it indicates that int res = n % I can be divisible; If (RES = 0) {return 0; // return 0} return N;} if it is not a prime number ;}}

Main class call


 
Package endual; public class main ,.... How many public static void main (string [] ARGs) {// todo auto-generated method stub // int n = 8; qanswer Qu = new qanswer (); For (INT n = 101; n <= 200; n ++) {// obviously the time complexity is very huge, int res = Qu. sushu (n); If (0! = Res) {system. Out. println (RES );}}}}

In this way, you can





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.