Click here to Skip to main content
15,881,812 members
Home / Discussions / C#
   

C#

 
GeneralRe: Sieve of Eratosthenes Pin
Pete O'Hanlon12-Sep-12 5:22
mvePete O'Hanlon12-Sep-12 5:22 
GeneralRe: Sieve of Eratosthenes PinPopular
J4amieC12-Sep-12 5:23
J4amieC12-Sep-12 5:23 
GeneralRe: Sieve of Eratosthenes Pin
WebMaster12-Sep-12 5:48
WebMaster12-Sep-12 5:48 
GeneralRe: Sieve of Eratosthenes Pin
J4amieC12-Sep-12 5:59
J4amieC12-Sep-12 5:59 
GeneralRe: Sieve of Eratosthenes Pin
WebMaster12-Sep-12 6:11
WebMaster12-Sep-12 6:11 
GeneralRe: Sieve of Eratosthenes Pin
Rage12-Sep-12 6:23
professionalRage12-Sep-12 6:23 
GeneralRe: Sieve of Eratosthenes Pin
Pete O'Hanlon12-Sep-12 6:25
mvePete O'Hanlon12-Sep-12 6:25 
AnswerProper implementation of Sieve of Eratosthenes Pin
Clifford Nelson12-Sep-12 10:45
Clifford Nelson12-Sep-12 10:45 
You made a big error in the algorithm as stated above. Also you add work with instantiating classes (struct probably better).

C#
private static bool[] _numbers;

static void Main(string[] args)
{
    var sw = new Stopwatch();
    sw.Start();
    Run();
    sw.Stop();
    Console.WriteLine("Milliseconds run: " + sw.ElapsedMilliseconds);
    Console.WriteLine("Primes: " + _numbers.Count(i => i));
    Console.ReadLine();
}

private static void Run()
{
    const int count = 2000000;
    _numbers = new bool[count];
    for (int i = 2; i < count; i++)
        _numbers[i] = true;

    int baseCounter = 1;

    while (baseCounter < count)
    {
        do
        {
            baseCounter++;
            if (baseCounter == count) return;
        } while (!_numbers[baseCounter]);

        int counter = baseCounter << 1;
        while (counter < count)
        {
            _numbers[counter] = false;
            counter += baseCounter;
        }
    }

GeneralRe: Proper implementation of Sieve of Eratosthenes Pin
WebMaster12-Sep-12 11:00
WebMaster12-Sep-12 11:00 
AnswerRe: Proper implementation of Sieve of Eratosthenes Pin
Clifford Nelson12-Sep-12 11:20
Clifford Nelson12-Sep-12 11:20 
GeneralRe: Proper implementation of Sieve of Eratosthenes Pin
Dave Kreskowiak12-Sep-12 12:44
mveDave Kreskowiak12-Sep-12 12:44 
GeneralRe: Proper implementation of Sieve of Eratosthenes Pin
Clifford Nelson12-Sep-12 13:07
Clifford Nelson12-Sep-12 13:07 
GeneralRe: Proper implementation of Sieve of Eratosthenes Pin
Dave Kreskowiak12-Sep-12 18:25
mveDave Kreskowiak12-Sep-12 18:25 
AnswerRe: Sieve of Eratosthenes Pin
Thomas Daniels12-Sep-12 5:29
mentorThomas Daniels12-Sep-12 5:29 
AnswerRe: Sieve of Eratosthenes Pin
Kenneth Haugland13-Sep-12 3:33
mvaKenneth Haugland13-Sep-12 3:33 
AnswerRe: Sieve of Eratosthenes Pin
PIEBALDconsult12-Sep-12 14:23
mvePIEBALDconsult12-Sep-12 14:23 
QuestionSubstitution of Paths in HyperLinkField with File Names Pin
ASPnoob12-Sep-12 4:23
ASPnoob12-Sep-12 4:23 
AnswerRe: Substitution of Paths in HyperLinkField with File Names Pin
Pete O'Hanlon12-Sep-12 4:55
mvePete O'Hanlon12-Sep-12 4:55 
GeneralRe: Substitution of Paths in HyperLinkField with File Names Pin
ASPnoob12-Sep-12 9:59
ASPnoob12-Sep-12 9:59 
QuestionRetrieve name of Printer using SendMessage Pin
toBeH_S11-Sep-12 22:10
toBeH_S11-Sep-12 22:10 
AnswerRe: Retrieve name of Printer using SendMessage Pin
Rage12-Sep-12 6:40
professionalRage12-Sep-12 6:40 
GeneralRe: Retrieve name of Printer using SendMessage Pin
toBeH_S12-Sep-12 8:33
toBeH_S12-Sep-12 8:33 
GeneralRe: Retrieve name of Printer using SendMessage Pin
Rage12-Sep-12 21:25
professionalRage12-Sep-12 21:25 
GeneralRe: Retrieve name of Printer using SendMessage Pin
toBeH_S16-Sep-12 15:18
toBeH_S16-Sep-12 15:18 
QuestionRandom Permutation Interface Pin
Skippums11-Sep-12 15:58
Skippums11-Sep-12 15:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.