Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Prime Numbers (Eratosthenes)

  1. Feb 26, 2017 #1

    SGJ

    User Avatar

    Mod note: added code tags
    Code (C):
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {

      class Program
      {
        static void Main(string[] args)
        {
           List<int> number = new List<int>(1000); // int list for 1000 numbers
           for(int i = 0; i < 1000; i++) // for loop that populates the numbers array
           {
              number.Add (i + 1); // the first position in the array will equal 1 rather than zero
           }
           for (int i = 2; i <= 32; i++) // This is the first loop that will run the square root of 1000 times
           {  
              for (int j = 2; j < 32; j++) // Nested for loop that will run for the square root of 1000 times
              {
                   number.Remove(j * i); // every muptiple will be removed leaving only odd numbers
              }
           
              foreach (int numbers in number)
              {
                 Console.Write(" " + number.Count); // writes the numbers still left in the list
              }
           
          }
          Console.ReadKey(); // Keeps program open
        }
      }
    }
     
    Last edited by a moderator: Feb 26, 2017
  2. jcsd
  3. Feb 26, 2017 #2

    SGJ

    User Avatar

    So here is my problem (this is my first time using lists and this site so bear with me) whenever I try to run this it prints 970 and will repeat that number hundreds of times. I want it to print 1, 3, 5, 7, 11, 13, 17.... ect. Please help urgent C#.
     
  4. Feb 26, 2017 #3

    jack action

    User Avatar
    Science Advisor
    Gold Member

    number.Count should return the number of items in the list. So you are printing '970' ... 970 times!

    Try Console.Write(" " + numbers) instead.
     
  5. Feb 26, 2017 #4

    SGJ

    User Avatar

    I tried that and it printed 1-1000 several hundred times.
    I then tried just printing "number" but it printed "systems collection generic" another thousand or so times.
     
  6. Feb 26, 2017 #5

    jack action

    User Avatar
    Science Advisor
    Gold Member

    I'm not really good with C#, but this is a good resource where I found the following:

    Code (C):
    number.ForEach(Print);

    private static void Print(string s)
    {
        Console.Write(" " + s);
    }

     
    There is also a simple method:

    Code (C):
    Console.Write(number.ToString());
     
  7. Feb 26, 2017 #6
    My very first observation (being a C# programmer) is that, since you've included System.LINQ, you might want to consider fully using it by changing

    Code (Text):

           List<int> number = new List<int>(1000); // int list for 1000 numbers
           for(int i = 0; i < 1000; i++) // for loop that populates the numbers array
           {
              number.Add (i + 1); // the first position in the array will equal 1 rather than zero
           }
     
    to

    Code (Text):

    List<int> number = Enumerable.Range(1,1000).ToList();
     
     
  8. Feb 26, 2017 #7

    SGJ

    User Avatar

    Thank you for that!
    Do you have any solution as to why I am getting large repetitions of numbers starting at around 970?
     
  9. Feb 26, 2017 #8

    QuantumQuest

    User Avatar
    Gold Member

    The error lies in your for loops. Take a careful look again. As a hint, don't print out numbers until you have removed what you need first.
     
  10. Feb 26, 2017 #9

    Mark44

    Staff: Mentor

    Your algorithm is incorrect. After removing values from your list that are the products of numbers of the form a * b, where both a and b are in {2..32}, your list ends up with numbers that aren't prime. The first one I see starts at 74 (= 2 * 37). Also present are is and b = 82, 86, 94, 106, and a slew of others. Instead of removing numbers a * b with a and b in {2, ..., 32}, go through the entire list and remove multiples of 2, then multiples of 3, and so on, up to 32.
     
  11. Feb 26, 2017 #10

    Mark44

    Staff: Mentor

    BTW, your foreach loop is nested within the outer for loop, which causes it to run 31 times (printing out your list that number of times). It should NOT be nested within that outer for loop.

    Code (C):
    static void Main(string[] args)
    {
         List<int> number = new List<int>(1000); // int list for 1000 numbers
         for(int i = 0; i < 1000; i++) // for loop that populates the numbers array
         {
             number.Add (i + 1); // the first position in the array will equal 1 rather than zero
         }
         for (int i = 2; i <= 32; i++) // This is the first loop that will run the square root of 1000 times
         {
            for (int j = 2; j < 32; j++) // Nested for loop that will run for the square root of 1000 times
            {
                number.Remove(j * i); // every muptiple will be removed leaving only odd numbers
            }
         } // <---- Added by Mark44
           
        foreach (int numbers in number)
        {
           Console.Write(" " + number[numbers]); // writes the numbers still left in the list
        }
           
        Console.ReadKey(); // Keeps program open
    }
     
  12. Feb 26, 2017 #11

    QuantumQuest

    User Avatar
    Gold Member

    Also, as Mark44 points out your algorithm is incorrect. You will see it if you first correct the error according to my hint: the resulting numbers are not all primes.
     
  13. Feb 26, 2017 #12

    SGJ

    User Avatar

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1


    {

    class Program


    {

    static void Main(string[] args)


    {







    List<int> number = new List<int>(1000); // int list for 1000 numbers


    for (int i = 0; i < 1000; i++) // for loop that populates the numbers array


    {

    number.Add(i + 1); // the first position in the array will equal 1 rather than zero


    }



    for (int i = 2; i < 1000; i++) // This is the first loop that will run the square root of 1000 times


    {

    for (int j = 2; j < 1000; j++) // Nested for loop that will run for the square root of 1000 times


    {

    if (i * j < 1000)


    {

    number.Remove(j * i); // every muptiple will be removed leaving only odd numbers


    }





    }

    }

    foreach (int numbers in number)


    {

    Console.Write(" " + number[numbers]); // writes the numbers still left in the list


    }






    So this is as far as I got. Thanks to your help, the multiples stopped and i do have some primes but some of them are missing such as 13, all the primes in the 20's etc. The (" " + number[numbers]) line gives it a Out of range error.
     
  14. Feb 26, 2017 #13

    QuantumQuest

    User Avatar
    Gold Member

    I'll give a hint in a different direction. Try to fill your array with all multiples of 2 and 3. To do this, you need two nested loops, the first going from 2 to 1000 and the second might begin with the double of the variable in the first loop, but the tricky part is how to increment it in order to fill all the multiples. I leave it to you to tinker with it, till you figure it out. Then, you can print out whatever is not included in your array, using another loop. What will be these numbers you will print out?
     
  15. Feb 26, 2017 #14

    Mark44

    Staff: Mentor

    @SGJ, please use code tags when you post code. Not doing so loses any indentation you have.

    [code=c]using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    // etc. [/code]
     
  16. Feb 26, 2017 #15

    SGJ

    User Avatar

    Code (C):

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1


    {

    class Program

     
    {

    static void Main(string[] args)


    {







    List<int> number = new List<int>(1000); // int list for 1000 numbers


    for (int i = 0; i < 1000; i++) // for loop that populates the numbers array


    {

    number.Add(i + 1); // the first position in the array will equal 1 rather than zero


    }



    for (int i = 2; i < 1000; i++) // This is the first loop that will run the square root of 1000 times


    {

    for (int j = 2; j < 1000; j++) // Nested for loop that will run for the square root of 1000 times


    {

    if (i * j < 1000)


    {

    number.Remove(j * i); // every muptiple will be removed leaving only odd numbers


    }





    }

    }

    foreach (int numbers in number)


    {

    Console.Write(" " + number[numbers]); // writes the numbers still left in the list


    }












     
    // etc.
     
  17. Feb 26, 2017 #16

    SGJ

    User Avatar

    sorry im new to this, I posted the code in the comment above.
     
  18. Feb 26, 2017 #17

    Mark44

    Staff: Mentor

    Do you have a question about what you posted in #14?
     
  19. Feb 26, 2017 #18

    SGJ

    User Avatar

    yes, why do I get a range error at Console.WriteLine(" " + number[numbers])
     
  20. Feb 26, 2017 #19

    SGJ

    User Avatar

    Code (C):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
      class Program
      {

        static void Main(string[] args)
        {
           List<int> number = new List<int>(1000); // int list for 1000 numbers
           int[] check = new int[1000]; // another array for the mathematics.
           // for (int i = 0; i < 1000; i++) // for loop that populates the numbers array
           // {
              // number.Add(i + 1); // the first position in the array will equal 1 rather than zero
           // }

           for (int i = 0; i < 1000; i++) // This is the first loop that will run the square root of 1000 times
           {
              for (int j = 2; j < 1000; j++) // Nested for loop that will run for the square root of 1000 times
             {
                check[j] = i + 1; // populates check
                if(check[ i ] % check[j] == 1) // If the remainder of check position i and check position j is one (odd number test)
               {
                  number.Add(i); // The number is added to number
               }
            }
         }
     
         foreach (int numbers in number)
         {
            Console.Write(" " + number.Count); // writes the numbers still left in the list
         }


    so I decided to take a completely different route. Now I get a domain/range error at the line (check[ i] % check[j] ==1)

    I ONLY USED 1 AS A TEST.
     
    Last edited by a moderator: Feb 26, 2017
  21. Feb 26, 2017 #20

    Mark44

    Staff: Mentor

    I'm still looking at your code, but in the meantime, a couple of tips:
    1. Don't put any spaces in the starting code tag. It has to be [code=c], not [code = c]. The extra spaces breaks things.
    2. An array index of i (as in check[i]) is treated as a starting Italic tag, and you end up garbled code, as the browser "eats up" Italics tags, bold tags, etc. The workaround is to add a space before the i, as in check[ i].
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Prime Numbers (Eratosthenes)
  1. Prime number test (Replies: 4)

  2. Prime number code, C++ (Replies: 16)

Loading...