Published
Edited
Dec 26, 2021
1 star
Insert cell
# Prime Number
Insert cell
// 특정 숫자가 소수인지 판별하는데 유리 :: O(√n)
function isPrime(num) {
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) return false;
}
return true;
}
Insert cell
// 에라토스테네스의 체. 특정 수까지의 소수를 알아내는데에 유리 O(nlogn)
function getPrimes(num) {
const prime = [false, false, ...Array(num - 1).fill(true)];

for (let i = 2; i * i <= num; i++) {
if (prime[i]) {
for (let j = i * 2; j <= num; j += i) {
prime[j] = false;
}
}
}
return prime;
}
Insert cell
getPrimes(13)
Insert cell
isPrime(13)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more