Count primes below n · 数小于 n 的质数
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Count primes below n
Return how many prime numbers are strictly less than n. A prime has no divisors other than 1 and itself. For each candidate k, test divisors only up to √k (i.e. while d * d <= k) — once one divides k evenly, it isn't prime.
中文
数小于 n 的质数
返回严格小于 n 的质数有多少个。质数除了 1 和它自身没有其他因数。对每个候选数 k,只需测试到 √k 为止的因数(即 d * d <= k)—— 一旦有一个能整除 k,它就不是质数。
Complete countPrimes(int n) returning how many prime numbers are strictly less than n. Example: countPrimes(10) is 4 (2, 3, 5, 7). · 完成 countPrimes(int n),返回严格小于 n 的质数个数。例如:countPrimes(10) 是 4(2、3、5、7)。
Click Run to see the output here. · 点击“运行”查看此处输出。