Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

204. Count Primes

Count the number of prime numbers less than a non-negative number, n.

Example 1:

1
2
3
Input: n = 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

Example 2:

1
2
Input: n = 0
Output: 0

Example 3:

1
2
Input: n = 1
Output: 0

質數(Prime number),又稱素數,指在大於1的自然數中,除了1和該數自身外,無法被其他自然數整除的數(也可定義為只有1與該數本身兩個正因數的數)。- wikipedia

判斷一個數是否是質數,只需判斷它是否能被小於它開根後的所有數整除。

所以從2開始算到開根,其中有合數就設為false。


如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels