generated from openscript/vscode-dev-container-tectonic-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
source.tex
47 lines (41 loc) · 1.29 KB
/
source.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
% see B6.1b
\chapter{Quellcode}
\begin{figure}[H]
\begin{codebox}[]
\begin{minted}{javascript}
/**
* @param {number} maxNumber
* @return {number[]}
*/
export default function sieveOfEratosthenes(maxNumber) {
const isPrime = new Array(maxNumber + 1).fill(true);
isPrime[0] = false;
isPrime[1] = false;
const primes = [];
for (let number = 2; number <= maxNumber; number += 1) {
if (isPrime[number] === true) {
primes.push(number);
/*
* Optimisation.
* Start marking multiples of `p` from `p * p`, and not from `2 * p`.
* The reason why this works is because, at that point, smaller multiples
* of `p` will have already been marked `false`.
*
* Warning: When working with really big numbers, the following line may cause overflow
* In that case, it can be changed to:
* let nextNumber = 2 * number;
*/
let nextNumber = number * number;
while (nextNumber <= maxNumber) {
isPrime[nextNumber] = false;
nextNumber += number;
}
}
}
return primes;
}
\end{minted}
\end{codebox}
\caption[\enquote{Sieb des Eratosthenes implementiert mit JavaScript} visualisiert mit Minted]{Sieb des Eratosthenes implementiert mit JavaScript}
\label{fig:sieve-of-eratosthenes}
\end{figure}