Click here to Skip to main content
15,171,439 members
Home / Discussions / C#
   

C#

 
AnswerRe: First real C# program by C++ developer Pin
Richard MacCutchan12-Sep-12 23:26
mveRichard MacCutchan12-Sep-12 23:26 
AnswerRe: First real C# program by C++ developer Pin
jschell13-Sep-12 9:59
Memberjschell13-Sep-12 9:59 
AnswerRe: First real C# program by C++ developer Pin
wizardzz13-Sep-12 11:46
Memberwizardzz13-Sep-12 11:46 
AnswerRe: First real C# program by C++ developer Pin
Alan Balkany13-Sep-12 12:10
MemberAlan Balkany13-Sep-12 12:10 
AnswerRe: First real C# program by C++ developer Pin
Alan Balkany13-Sep-12 12:44
MemberAlan Balkany13-Sep-12 12:44 
QuestionDynamic TimeSlots Pin
Member 941347212-Sep-12 9:34
MemberMember 941347212-Sep-12 9:34 
AnswerRe: Dynamic TimeSlots Pin
Pete O'Hanlon12-Sep-12 10:46
mvaPete O'Hanlon12-Sep-12 10:46 
QuestionSieve of Eratosthenes Pin
WebMaster12-Sep-12 5:23
MemberWebMaster12-Sep-12 5:23 
I have implemented the "Sieve of Eratosthenes" algorithm in c# to find prime numbers below 2,000,000. The execution takes very time. How can I improve my implementation?

Here is my code:
C#
public class Number
{
   private int _Value;
   private bool _IsPrime;

   public int Value
   {
      get { return this._Value; }
      set { this._Value = value; }
   }
   public bool IsPrime
   {
      get { return this._IsPrime; }
      set { this._IsPrime = value; }
   }

   public Number(int value)
   {
       this.Value = value;
       this.IsPrim = true;
   }
}


C#
List<Number> numbers = new List<Number>();

for (int i = 2; i < 20000000; i++)
    numbers.Add(new Number(i));

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;

Meysam


modified 12-Sep-12 17:18pm.

AnswerRe: Sieve of Eratosthenes Pin
fjdiewornncalwe12-Sep-12 5:31
professionalfjdiewornncalwe12-Sep-12 5:31 
AnswerRe: Sieve of Eratosthenes Pin
Manfred Rudolf Bihy12-Sep-12 5:36
professionalManfred Rudolf Bihy12-Sep-12 5:36 
GeneralRe: Sieve of Eratosthenes Pin
WebMaster12-Sep-12 5:39
MemberWebMaster12-Sep-12 5:39 
GeneralRe: Sieve of Eratosthenes Pin
Manfred Rudolf Bihy12-Sep-12 5:45
professionalManfred Rudolf Bihy12-Sep-12 5:45 
GeneralRe: Sieve of Eratosthenes Pin
WebMaster12-Sep-12 5:56
MemberWebMaster12-Sep-12 5:56 
GeneralRe: Sieve of Eratosthenes Pin
Manfred Rudolf Bihy12-Sep-12 6:04
professionalManfred Rudolf Bihy12-Sep-12 6:04 
GeneralRe: Sieve of Eratosthenes Pin
Richard MacCutchan12-Sep-12 6:40
mveRichard MacCutchan12-Sep-12 6:40 
AnswerRe: Sieve of Eratosthenes Pin
J4amieC12-Sep-12 5:53
MemberJ4amieC12-Sep-12 5:53 
AnswerRe: Sieve of Eratosthenes Pin
Pete O'Hanlon12-Sep-12 5:54
mvaPete O'Hanlon12-Sep-12 5:54 
GeneralRe: Sieve of Eratosthenes Pin
WebMaster12-Sep-12 5:59
MemberWebMaster12-Sep-12 5:59 
GeneralRe: Sieve of Eratosthenes Pin
J4amieC12-Sep-12 6:02
MemberJ4amieC12-Sep-12 6:02 
GeneralRe: Sieve of Eratosthenes Pin
Manfred Rudolf Bihy12-Sep-12 6:06
professionalManfred Rudolf Bihy12-Sep-12 6:06 
GeneralRe: Sieve of Eratosthenes Pin
J4amieC12-Sep-12 6:07
MemberJ4amieC12-Sep-12 6:07 
GeneralRe: Sieve of Eratosthenes Pin
Pete O'Hanlon12-Sep-12 6:04
mvaPete O'Hanlon12-Sep-12 6:04 
GeneralRe: Sieve of Eratosthenes Pin
WebMaster12-Sep-12 6:16
MemberWebMaster12-Sep-12 6:16 
GeneralRe: Sieve of Eratosthenes Pin
Pete O'Hanlon12-Sep-12 6:22
mvaPete O'Hanlon12-Sep-12 6:22 
GeneralRe: Sieve of Eratosthenes Pin
J4amieC12-Sep-12 6:23
MemberJ4amieC12-Sep-12 6:23 

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.