Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0084acf

Browse files
authored
algorithm: sieve (TheAlgorithms#1205)
1 parent55f502e commit0084acf

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

‎Maths/SieveOfEratosthenesIntArray.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Function to get all prime numbers below a given number
3+
* This function returns an array of prime numbers
4+
*@see {@link https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes}
5+
*/
6+
7+
functionsieveOfEratosthenes(max){
8+
constsieve=[]
9+
constprimes=[]
10+
11+
for(leti=2;i<=max;++i){
12+
if(!sieve[i]){// If i has not been marked then it is prime
13+
primes.push(i)
14+
for(letj=i<<1;j<=max;j+=i){// Mark all multiples of i as non-prime
15+
sieve[j]=true
16+
}
17+
}
18+
}
19+
returnprimes
20+
}
21+
22+
export{sieveOfEratosthenes}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import{sieveOfEratosthenes}from'../SieveOfEratosthenesIntArray'
2+
import{PrimeCheck}from'../PrimeCheck'
3+
4+
describe('should return an array of prime numbers',()=>{
5+
it('should have each element in the array as a prime numbers',()=>{
6+
constn=100
7+
constprimes=sieveOfEratosthenes(n)
8+
primes.forEach(prime=>{
9+
expect(PrimeCheck(prime)).toBeTruthy()
10+
})
11+
})
12+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp