Questions frequently asked during the interview -- Determination of prime numbers

Source: Internet
Author: User

I will start looking for a job next year, so I am preparing for some basic data structure knowledge and interview questions. Therefore, I will sort out some questions from time to time here. I found that prime number judgment is a very commonAlgorithm, Basically, many of the questions I have seen involve the determination of prime numbers, such as the sum of all prime numbers from 1024 to 687432, or the number of prime numbers in the Npower of 720, the core issue that involves prime numbers is how to determine prime numbers.

How can we determine the prime number? Prime Number refers to the natural number that can only be divisible by itself and 1. The common and easy-to-implement algorithm is Try Division: set this number N Use <= N All prime number division. If division fails N Is a prime number.

next we will start to write the Code :

  Boolean  isprime ( int   Number) {  Boolean  isprime =  true  ;   for  ( int  I = 2; I <= math. SQRT (number); I ++ ) {  If  (Number % I = 0 ) {isprime  =  false  ; 
}} return isprime ;}

test again:

  Public   class   test {  Public   static   void   main (string [] ARGs) {  for  ( int  I = 0; I <100; I ++ ) {  If   (isprime (I) {system. out. println (I  + "is a prime number"  );}}} 
}

the output result is:

2 is a prime number
3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is prime Number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number
53 is a prime number
59 is a prime number
61 is a prime number
71 is a prime number
BR> 73 is a prime number
79 is a prime number
83 is a prime number
89 is a prime number
97 is a prime number

The Code does not need to be backed up, as long as you know the algorithm,ProgramYou can compile the desired code.

So here I will repeat the algorithm again: as long as there is a number of N, in the range of [2, SQRT (n)], no number can be divisible, it is a prime number. Why is it 2? Because 0 cannot be used as the divisor, any number can be divided by 1.

The determination of prime numbers is still a small problem. As mentioned above, it is necessary to combine specific questions. In addition, these questions often have traps. For example, the following questions:

In the Npower of 720, how many are prime numbers?

Such a question seems very simple. We can make it at once:

Check the Code:

  Public   class   test {  int  Number = 1 ;  int  counter = 0 ;  for  ( int  I = 0; I 
  
   ) {number  * = 720 
   ;  
    If  
    (isprime (number) {counter  ++ 
   ;}} system. out. println (Counter) ;} 
  

here, I believe you have seen the problem: how can we determine the n? Here we use the maximum value of a positive integer, but we know that errors will occur, because it is definitely beyond the maximum range of the integer, and determining n is the key to this question:

  int  getrange ( int  Number,  int   divisor) {  int  range = 0 ;   while  (number/divisor! = 0 ) {range  ++ ; number  = Number/ divisor ;}  return   range ;} 

test:

  Public   static   void   main (string [] ARGs) {  int  Number = 1 ;  int  divisor = 720 ;  int  range =  getrange (integer. max_value, divisor);   for  ( int  I = 0; I 
  
   ) {number  * = 
    divisor;  
    If  
    (isprime (number) {system. out. println (number  + "is a prime number" 
   ) ;}} 
  

Make sure that the number of judgments is always within the Java Integer Range.

Here we will show you the Integer Range of Java: The 31 Power of-2 to the 31 power of 2 minus 1.

 

 

 

 

 

 

 

 

 

 

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.