Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Hi all,

Read it carefully and give the C# code for the below

Using 1000 sized One-Dimensional Array ,write a program that finds all primes.

Directions :
2 is a prime ,
Starting with 4,erase all multiples of 2 larger than 2
Erase 4,6,8,10,12,14,16,....etc.
Find next number to 2 ,that is not erased,
Similarly Scan entire array & remaining in the array should be Prime Numbers.

Thanks in advance

S.Naveen...
Posted
Updated 23-Sep-10 12:21pm
v2
Comments
Dylan Morley 22-Sep-10 11:30am    
Homework
Smithers-Jones 22-Sep-10 11:32am    
Lazy bum.
Toli Cuturicu 23-Sep-10 18:22pm    
We know what the Eratostene's sieve is. You don't know. So, you don't have to explain it to us!
Yusuf 23-Sep-10 18:42pm    
Hmmm.... unable to do your own homework, huh?

Just find a web site that lists all prime numbers under 1000, stick 'em in an array, and there's your list of prime numbers.
 
Share this answer
 
Comments
Toli Cuturicu 23-Sep-10 18:20pm    
Or you may calculate them by hand using the sieve in less than ten minutes. (any 12 year old in a normal school in my country should be able to do this)
Quote: "Read it carefully" - OK, did that.

Quote: "give the C# code for the below" - I don't think so.

If you do not have the skills to do your own work then I suggest you take a class or read a book. If you are just too lazy then I suggest you employ someone, the jobs page is over there.
 
Share this answer
 
long limit,sqrLimit,i,j,n;
            long[] A = new long[1001];

            limit = 1000;
            sqrLimit = Convert.ToInt64(Math.Sqrt(1000));

            for (i = 0; i <= limit; i++)
                A[i] = 0;

                for (i = 2; i <= sqrLimit + 1; i++)
                    for (j = i + i; j <= limit; j = j + i)
                        A[j] = 1;

            n = 0;
            for (i = 2; i <= limit; i++)
                if (A[i] == 0)
                {
                    A[n] = i;
                    n++;
                }
 
Share this answer
 
v3
Comments
Richard MacCutchan 22-Sep-10 13:27pm    
Perhaps if you had read my answer you would realise that giving people code in this way just helps them to cheat their way to success or graduation. But in the long term they are the ones who will suffer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900