I know what this code does but I don't know How it works if someone can explain to me how it works I would be glad

const int Last_Number  = 1000;
int Primes = 0;
Array Numbers = new Array(Last_Number, true);

for (int i = 2; i <  Last_Number; i++)
{
     if ( Numbers[i])
       {
           for (int j = i * 2; j < Last_Number;  j += i)
              { Numbers[j] = false; }
        }
}

for (int i = 1; i <  Last_Number; i++)
{
     if ( Numbers[i])
       {
          Primes++;
          Console.Out.WriteLine (i);
        }
}

 Console.Out.WriteLine (i);
( "\n" + Primes + " Primes found in the range 2-" + Last_Number );

What part don't you understand?

What part don't you understand?

I don't understand this part

Array Numbers = new Array(Last_Number, true);

for (int i = 2; i < Last_Number; i++)
{
if ( Numbers)
{
for (int j = i * 2; j < Last_Number; j += i)
{ Numbers[j] = false; }
}
}

It's setting the array elements positioned at multiples of i to false. That's because they're not prime numbers (beacuse they're a multiple of some number).

It's setting the array elements positioned at multiples of i to false. That's because they're not prime numbers (beacuse they're a multiple of some number).

I Would like to have a step by step explanation of how this pies of code works. thanks

Which steps don't you understand?

Which steps don't you understand?

All of the code, i don't know how it gets the prime numbers

Simulate the code by hand, on paper. Then you'll understand.

Simulate the code by hand, on paper. Then you'll understand.

I'm am new in the IT thing and looking for someone to help me with the code(all of it).

I'm willing to help you with this under a couple of conditions (I reserve the right to add more later)

#1 - Please tell me where you got the code and why you want to know. Don't lie, if it sounds as though you are I'm out.

#2 - I want you to put your comments before each line, and I want you to tell me what you think it is doing. I do NOT want anything similar to "I have no idea" - give me your best guess if you can't come up with a rough idea.

I'm am new in the IT thing and looking for someone to help me with the code(all of it).

So your real problem is that you don't understand C#? Then you need to learn C#.

I'm willing to help you with this under a couple of conditions (I reserve the right to add more later)

#1 - Please tell me where you got the code and why you want to know. Don't lie, if it sounds as though you are I'm out.

#2 - I want you to put your comments before each line, and I want you to tell me what you think it is doing. I do NOT want anything similar to "I have no idea" - give me your best guess if you can't come up with a rough idea.

#1 I got the code from the internet and I am preparing for an assessment that I am writing tomorrow and this will most definitely be one of the answers in the assessment thats why i need to know

#2
// Declaration=1000
const int Last_Number = 1000;
//Declaration=0
int Primes = 0;
//Creating an array and set it = to an new array Last number and make it true
Array Numbers = new Array(Last_Number, true);
//for loop declare i=2,if i<last_number in array add i to i
for (int i = 2; i < Last_Number; i++)
{
// Substitute i into the array
if ( Numbers)
{
//for loop declare j=2,if j<last_number in array add 1 to j
for (int j = i * 2; j < Last_Number; j += i)
//Substitute i into the array and make it false
{ Numbers[j] = false; }
}
}
//for loop declare i=1,if i<last_number in array add i to i
for (int i = 1; i < Last_Number; i++)
{
if ( Numbers)
{
//add primes
Primes++;
//write values of i
Console.Out.WriteLine (i);
}
}
//new line, write the prime numbers, prime numbers found in the range 2 - 1000
Console.Out.WriteLine (i);
( "\n" + Primes + " Primes found in the range 2-" + Last_Number );

start quote:

// Declaration=1000
 const int Last_Number = 1000;
//Declaration=0
 int Primes = 0;
//Creating an array and set it = to an new array Last number and make it true
Array Numbers = new Array(Last_Number, true);
//for loop declare i=2,if i<last_number in array add i to i
for (int i = 2; i < Last_Number; i++)
{
// Substitute i into the array
if ( Numbers[i])
{
//for loop declare j=2,if j<last_number in array add 1 to j
for (int j = i * 2; j < Last_Number; j += i)
//Substitute i into the array and make it false
{ Numbers[j] = false; }
}
}
//for loop declare i=1,if i<last_number in array add i to i
for (int i = 1; i < Last_Number; i++)
{
if ( Numbers[i])
{
//add primes
Primes++;
//write values of i
Console.Out.WriteLine (i);
}
}
//new line, write the prime numbers, prime numbers found in the range 2 - 1000
Console.Out.WriteLine (i);
( "\n" + Primes + " Primes found in the range 2-" + Last_Number );

end quote.

so much is either wrong with this code, or I am too new to understand. For instance, the array declaration is unlike anything I've ever used. It tries to take Last_Number and make it a const int with a value of 1000 then try to use the same variable name in an array declaration. Then without declaring 'Numbers' it tries to assign a value of the array into that. It then tries to take an array of numbers and give it a boolean value. There are many more errors or things done that I don't normally use. It would be a little easier to read if you put things in code tags but it's a mess. Maybe someone else can help, I apologize - I should have looked at it earlier.

The Array class has no public constructors so

Array Numbers = new Array(Last_Number, true);
will not work. ( Is this Java?)
What I would do :
bool[] Numbers = new bool[Last_Number];
And initialise all the elements to true in a for loop.
If you insist you can use something like
Array Numbers = Array.CreateInstance(typeof(bool), Last_Number);
But in this case you cannot use indexing with [], you'll have to use enumerators.
The rest of the code is an algorithm of an old Greek called Erasthothenes. The web is filled with implementations and explanations. I strongly advise you to try the suggestion of Raskahil Fol! Write the numbers 1 until 100 on a paper. Erase all the multiples of 2, but leave 2. Now wipe out all the multiples of the next number on your list(which is 3) but leave the 3 untouched. Continue and see what happens.

More rubbish in the last part of your code :

for (int i = 1; i < Last_Number; i++)
            {
                if (Numbers[i])
                {
                    Primes++;
                    Console.Out.WriteLine(i);
                }
            }

            Console.Out.WriteLine(i);
            ("\n" + Primes + " Primes found in the range 2-" + Last_Number);

Console.Out.WriteLine is the same as Console.WriteLine.
On line 10 leave out the (i); then join line 10 and 11.
In case you like to know, the for loop prints out the indexes of the Numbers array who are still true after the previous actions. Finally it prints out the total of these printouts, which is equal to the number of primes in this range.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.