Thread C# (sharp): Meine ersten Codeschnipsel... (11 answers)
Opened by havi at 2004-02-19 12:25

havi
 2004-02-19 12:27
#10541 #10541
User since
2003-08-04
2036 articles
BenutzerIn
[Homepage]
user image
Code: (dl )
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
using System;

class SieveOfEratosthenes {
static void Main() {

Console.Write("Enter a maximum integer to check: ");
int iMax = Int32.Parse(Console.ReadLine());
bool[] abIsPrime = new bool[iMax + 1];

// Das Array mit true initialisieren
for (int i = 0; i <= iMax; i++) {
abIsPrime[i] = true;
}

// Den Sieb-Algorithmus anwenden.
for (int i = 2; i * i <= iMax; i++) {
if (abIsPrime[i]) {
for (int j = 2; j <= iMax / i; j++) {
abIsPrime[i * j] = false;
} // for
} // if
} // for

// Die Primzahlen anzeigen
for (int i = 2; i <= iMax; i++) {
if (abIsPrime[i]) {
Console.Write("{0} ", i);
} // if
} // for

} // static void Main
} // class SieveOfEratosthenes

View full thread C# (sharp): Meine ersten Codeschnipsel...