792. K-th prime number

792. K-th prime number

Chinese English

Given prime number n, which is the output of the first prime number.

Sample

Sample 1

输入: n = 3
输出: 2
解释:
[2,3,5],3是第2个质数。

Sample 2

输入: n = 11
输出: 5
解释:
[2,3,5,7,11],11是第五个质数。

Precautions

  • n <= 100000
  • Defined prime number greater than 1a natural number, in addition to 1other than its own, and there are no other factors.
class Solution:
     "" "
     @param n-: The Number 
    @return: The Number of The Rank 
    " ""
     "" "
     general idea:
     1 . First, a given initial value, if it is prime has been assigned, then do not need to assign a. has been assigned is noted that it has been determined current this number is not a prime number, divisible
     2 according to the parameters to iterate to the desired position n of assignment is not a prime number (assigned a label is not a prime number can).
     3 . in order cycle, if it is 0 then there is no assigned, to be described count number is prime.
     "" "
     DEF kthPrime (Self, n-): 
        ## initializes all 0 
        Prime = [ 0 ] * 100000 
        
        for I in Range ( 2 , n-): 
            ## 0 if the description is not yet assigned (in fact, add this determination, the assignment may be less, to avoid duplicate assignments) 
            IF Prime [I] == 0 :
                If it is not a prime number ##, then, can be 2, 3 , 4 , 5 ... and so divisible, it is assigned 1 (opposite from 2, 3 , 4 , 5 ... multiplied value)
                 for J in Range ( 2 * I, n-, I): 
                    Prime [J] = 1 
            
        ## then determines the first number, to be described is not a prime number 1, to be described is a prime number (from 2 start determination) 0 
        COUNT = 1  
        for J in Range ( 2 , n-):
             IF Prime [J] == 0 : 
                COUNT + = . 1 
        return COUNT

 

Guess you like

Origin www.cnblogs.com/yunxintryyoubest/p/12551459.html