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

C#

 
GeneralRe: Sieve of Eratosthenes Pin
WebMaster12-Sep-12 4:59
WebMaster12-Sep-12 4:59 
GeneralRe: Sieve of Eratosthenes Pin
J4amieC12-Sep-12 5:02
J4amieC12-Sep-12 5:02 
GeneralRe: Sieve of Eratosthenes Pin
Manfred Rudolf Bihy12-Sep-12 5:06
professionalManfred Rudolf Bihy12-Sep-12 5:06 
GeneralRe: Sieve of Eratosthenes PinPopular
J4amieC12-Sep-12 5:07
J4amieC12-Sep-12 5:07 
GeneralRe: Sieve of Eratosthenes Pin
Pete O'Hanlon12-Sep-12 5:04
mvePete O'Hanlon12-Sep-12 5:04 
GeneralRe: Sieve of Eratosthenes Pin
WebMaster12-Sep-12 5:16
WebMaster12-Sep-12 5:16 
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 
See how point 2 is different from your implementation, and how the description of point 3 is TOTALLY different from your implementation

By way of example I mocked up 2 implementations. The first uses your code:

static void Brute(int max)
{
    var numbers = new List<Number>();
    for (int i = 2; i < max; i++)
        numbers.Add(new Number(i));

    var sw = new Stopwatch();
    sw.Start();
    for (int i = 0; i < numbers.Count - 1; i++)
        if (numbers[i].IsPrime)
            for (int j = i + 1; j < numbers.Count - 1; j++)
                if (numbers[j].Value % numbers[i].Value == 0)
                    numbers[j].IsPrime = false;

    sw.Stop();
    Console.WriteLine("Brute: {0}ms",sw.ElapsedMilliseconds);
}


I reduced it to only going to 200000, as it took too long to go all the way to 20000000 for the purpose of this demo:

Result:
Brute: 152009ms


Then I mocked up a very quick Sieve implementation without the enhancements that can be made (described in the wiki article)

-- Code example removed. If this is homework, im not doing it for you.

Result:
Sieve:15ms


The proof, as they say, is in the pudding. In this case Sieve pudding is ready just a little bit quicker
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 
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 

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.