国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode] Kth Prime Number

z2xy / 2264人閱讀

Problem

Given the prime number n, output the number of prime numbers

Notice

n <= 100000
The prime number is defined as a natural number greater than 1, and there are no other factors except 1 and it itself.

Example

Given n = 3, return 2.

explanation:
[2,3,5], 3 is the second prime number.
Given n = 11, return 5.

explanation:
[2,3,5,7,11], 11 is the fifth prime number.

Solution
public class Solution {
    /**
     * @param n: the number
     * @return: the rank of the number
     */
    public int kthPrime(int n) {
        // write your code here
        int num = 0;
        for (int i = 1; i < n; i++) {
            boolean isPrime = true;
            for (int j = 1; j < i; j++) {
                if (j != 1 && i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                num++;
            }
        }
        return num;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/68601.html

相關(guān)文章

  • [LintCode] Kth Smallest Number in Sorted Matrix

    Problem Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5 Challenge O(k log n), n is the maximal n...

    mgckid 評(píng)論0 收藏0
  • [LintCode/LeetCode] Check Sum of K Primes

    Problem Given two numbers n and k. We need to find out if n can be written as sum of k prime numbers. Example Given n = 10, k = 2Return true // 10 = 5 + 5 Given n = 2, k = 2Return false Solution pub...

    lakeside 評(píng)論0 收藏0
  • [LintCode] Ugly Number

    Problem Write a program to check whether a given number is an ugly number`. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly ...

    raise_yang 評(píng)論0 收藏0
  • [LintCode/LeetCode] Super Ugly Number

    摘要:建兩個(gè)新數(shù)組,一個(gè)存數(shù),一個(gè)存。數(shù)組中所有元素初值都是。實(shí)現(xiàn)的過(guò)程是,一個(gè)循環(huán)里包含兩個(gè)子循環(huán)。兩個(gè)子循環(huán)的作用分別是,遍歷數(shù)組與相乘找到最小乘積存入再遍歷一次數(shù)組與的乘積,結(jié)果與相同的,就將加,即跳過(guò)這個(gè)結(jié)果相同結(jié)果只存一次。 Problem Write a program to find the nth super ugly number. Super ugly numbers a...

    wuyumin 評(píng)論0 收藏0
  • [LintCode] Kth Largest Element [PriorityQueue]

    摘要:可以不要用太簡(jiǎn)單的方法。先把它裝滿,再和隊(duì)列頂端的數(shù)字比較,大的就替換掉,小的就。遍歷完所有元素之后,頂部的數(shù)就是第大的數(shù)。 Problem Find K-th largest element in an array. Example In array [9,3,2,4,8], the 3rd largest element is 4.In array [1,2,3,4,5], the...

    Hwg 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<