Problem
Given the prime number n, output the number of prime numbers
Noticen <= 100000
The prime number is defined as a natural number greater than 1, and there are no other factors except 1 and it itself.
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.
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
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...
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...
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 ...
摘要:建兩個(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...
摘要:可以不要用太簡(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...
閱讀 3683·2021-11-16 11:41
閱讀 2891·2021-09-23 11:45
閱讀 695·2019-08-30 15:44
閱讀 548·2019-08-30 13:10
閱讀 1966·2019-08-30 12:49
閱讀 3534·2019-08-28 17:51
閱讀 1483·2019-08-26 12:20
閱讀 707·2019-08-23 17:56