Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# code to find Prime Numbers

C# code to find Prime Numbers

Scheduled Pinned Locked Moved C#
csharphelpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    diabolusgorgon
    wrote on last edited by
    #1

    Hi, I am trying to write a program that finds the average of all prime numbers between 1 and 100 and prints the average to the screen. But i can't use numbers from a predetermined prime numbers list. Can any one help me? Ryan H

    R 1 Reply Last reply
    0
    • D diabolusgorgon

      Hi, I am trying to write a program that finds the average of all prime numbers between 1 and 100 and prints the average to the screen. But i can't use numbers from a predetermined prime numbers list. Can any one help me? Ryan H

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Sounds like homework to me :sigh: Ill give you some help but unless you have a special problem you will have to implement it yourself. 1. To find all prime numbers between 1 and 100 you will have to make a loop and check every number if its a prime number (ok if you are clever you only test the unequal numbers). 2. To check if a number is prime you will have make another loop checking if the number to test can be divided by another number which is bigger than 1 and smaller than itself (again if you are clever you only test up to n/2). 3. To test if a number can be deivided by another one use the mod (%) operator. If a % b == 0 then the numbers can be divided. 4. Every successfully tested number could stored in an ArrayList. 5. After youve tested all numbers just loop through the list and caculate the average. Just as a note to the perfectionists: I know there are better algorithms for that but for such small numbers that will do it perfectly well.

      A D 2 Replies Last reply
      0
      • R Robert Rohde

        Sounds like homework to me :sigh: Ill give you some help but unless you have a special problem you will have to implement it yourself. 1. To find all prime numbers between 1 and 100 you will have to make a loop and check every number if its a prime number (ok if you are clever you only test the unequal numbers). 2. To check if a number is prime you will have make another loop checking if the number to test can be divided by another number which is bigger than 1 and smaller than itself (again if you are clever you only test up to n/2). 3. To test if a number can be deivided by another one use the mod (%) operator. If a % b == 0 then the numbers can be divided. 4. Every successfully tested number could stored in an ArrayList. 5. After youve tested all numbers just loop through the list and caculate the average. Just as a note to the perfectionists: I know there are better algorithms for that but for such small numbers that will do it perfectly well.

        A Offline
        A Offline
        Asad Hussain
        wrote on last edited by
        #3

        HOMEWORK!!!!! Here is the *simplest* algorithm i - Loop through all odd numbers. That eliminates all even numbers which by definition CANNOT be prime. ii - Write a function that loops thru from 3 - sqrt(number) and if the mod results in 0 at any point then that number is not a prime. No need to test dividing by 1 and 2 since every number can be divided by 1 and you have eliminated even numbers so no need to divide by 2. You could go up to n/2 but it is unnecessary. eg. you are testing 47 for prime: * sqrt(47) = 7 (roughly) * if any number from 3-7 cannot divide 47 then it is surely a prime so u tested 5 numbers instead of 24. iii - Store every successfully tested number in some sort of collection. iv - Iterate thru the collection and do the average

        D R 2 Replies Last reply
        0
        • R Robert Rohde

          Sounds like homework to me :sigh: Ill give you some help but unless you have a special problem you will have to implement it yourself. 1. To find all prime numbers between 1 and 100 you will have to make a loop and check every number if its a prime number (ok if you are clever you only test the unequal numbers). 2. To check if a number is prime you will have make another loop checking if the number to test can be divided by another number which is bigger than 1 and smaller than itself (again if you are clever you only test up to n/2). 3. To test if a number can be deivided by another one use the mod (%) operator. If a % b == 0 then the numbers can be divided. 4. Every successfully tested number could stored in an ArrayList. 5. After youve tested all numbers just loop through the list and caculate the average. Just as a note to the perfectionists: I know there are better algorithms for that but for such small numbers that will do it perfectly well.

          D Offline
          D Offline
          diabolusgorgon
          wrote on last edited by
          #4

          I know I have to make a loop and check every number if it is a prime number, But I'm still lost. I'm not sure on how to set it up. I don't know what an array list is either. This is only my fourth week in my programming class and I have never done any programming before, this stuff is really hard to understand.

          R 1 Reply Last reply
          0
          • A Asad Hussain

            HOMEWORK!!!!! Here is the *simplest* algorithm i - Loop through all odd numbers. That eliminates all even numbers which by definition CANNOT be prime. ii - Write a function that loops thru from 3 - sqrt(number) and if the mod results in 0 at any point then that number is not a prime. No need to test dividing by 1 and 2 since every number can be divided by 1 and you have eliminated even numbers so no need to divide by 2. You could go up to n/2 but it is unnecessary. eg. you are testing 47 for prime: * sqrt(47) = 7 (roughly) * if any number from 3-7 cannot divide 47 then it is surely a prime so u tested 5 numbers instead of 24. iii - Store every successfully tested number in some sort of collection. iv - Iterate thru the collection and do the average

            D Offline
            D Offline
            diabolusgorgon
            wrote on last edited by
            #5

            Simplest? I don't understand this very much. Write a function that loops thru from 3 - sqrt(number) and if the mod results in 0 at any point then that number is not a prime. What??? This is really hard to understand I am only in my 4th week of programming and its moving to fast. I haven't done any programming before. Could you please help me?

            1 Reply Last reply
            0
            • A Asad Hussain

              HOMEWORK!!!!! Here is the *simplest* algorithm i - Loop through all odd numbers. That eliminates all even numbers which by definition CANNOT be prime. ii - Write a function that loops thru from 3 - sqrt(number) and if the mod results in 0 at any point then that number is not a prime. No need to test dividing by 1 and 2 since every number can be divided by 1 and you have eliminated even numbers so no need to divide by 2. You could go up to n/2 but it is unnecessary. eg. you are testing 47 for prime: * sqrt(47) = 7 (roughly) * if any number from 3-7 cannot divide 47 then it is surely a prime so u tested 5 numbers instead of 24. iii - Store every successfully tested number in some sort of collection. iv - Iterate thru the collection and do the average

              R Offline
              R Offline
              Robert Rohde
              wrote on last edited by
              #6

              Hey don't shout directly into my ear! Now im hearing a ringing allaround :) And please explain why this algorithm should be easier to understand then mine :confused: (its nearly the same except a bit more optimization)

              1 Reply Last reply
              0
              • D diabolusgorgon

                I know I have to make a loop and check every number if it is a prime number, But I'm still lost. I'm not sure on how to set it up. I don't know what an array list is either. This is only my fourth week in my programming class and I have never done any programming before, this stuff is really hard to understand.

                R Offline
                R Offline
                Robert Rohde
                wrote on last edited by
                #7

                Ok, as its Sunday and I have nothing really important to do...

                public ArrayList FindPrimes(int maxNumber)
                {
                ArrayList result = new ArrayList();
                for (int i = 3; i <= maxNumber; i += 2)
                if (IsPrime(i))
                result.Add(i);
                return result;
                }

                public double GetPrimesAverage(int maxNumber)
                {
                return CalcAverage(FindPrimes(maxNumber));
                }

                public bool IsPrime(int number)
                {
                double max = Math.Sqrt(number);
                for (int i = 3; i <= max; i += 2)
                if (number % i == 0)
                return false;
                return true;
                }

                public double CalcAverage(ArrayList list)
                {
                double sum = 0;
                foreach (int number in list)
                sum += number;
                return sum / list.Count;
                }

                Generally its the the algorithm I already stated (including some optimizations I initially didnt want you to take care of :)). I tried to split up the functionality. The function names should be descriptive enough to tell you want each part is doing. Note that IsPrime gives only valid results for uneven numbers. As GetPrimesAverage gets an argument you can calculate the average for as much prime numbers as you want (cpu is the limit :) - dont test it with more than about 20 millions). To get the result for a maximum of 100 the following call had to be done: GetPrimesAverage(100) Do yourself the favor and read it carefully until you understand it. If you already know how to use a debugger it would be good if you just step through the code until you get it.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups