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

algorithm: sieve#1205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
raklaptudirm merged 2 commits intoTheAlgorithms:masterfromddaniel27:patch-1
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletionsMaths/SieveOfEratosthenesIntArray.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
/**
* Function to get all prime numbers below a given number
* This function returns an array of prime numbers
* @see {@link https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes}
*/

function sieveOfEratosthenes (max) {
const sieve = []
const primes = []

for (let i = 2; i <= max; ++i) {
if (!sieve[i]) { // If i has not been marked then it is prime
primes.push(i)
for (let j = i << 1; j <= max; j += i) { // Mark all multiples of i as non-prime
sieve[j] = true
}
}
}
return primes
}

export { sieveOfEratosthenes }
12 changes: 12 additions & 0 deletionsMaths/test/SieveOfEratosthenesIntArray.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
import { sieveOfEratosthenes } from '../SieveOfEratosthenesIntArray'
import { PrimeCheck } from '../PrimeCheck'

describe('should return an array of prime numbers', () => {
it('should have each element in the array as a prime numbers', () => {
const n = 100
const primes = sieveOfEratosthenes(n)
primes.forEach(prime => {
expect(PrimeCheck(prime)).toBeTruthy()
})
})
})

[8]ページ先頭

©2009-2025 Movatter.jp