tags:

views:

2216

answers:

5
+1  Q: 

prime numbers c#

Hi I'm new to C#. And I would like to program something like, displaying the prime numbers in a listbox if user will input any integer in the textbox. (that means, if they write 10, it will display the prime numbers from 0-10, or 20 from 0-20, etc).

What should I consider first, before I do the programming? I know there are many examples in the internet, but first I would like to know what will I need?

Thanks for the tip;-)

=== Thanks guys. So you're suggesting that it's better to do it first in the Console application? I did an example of "For Loop" using Console Application a very simple one, but then when I tried to do it in the Windows Form Application, I'm not sure how to implement it. I'm afraid that if I keep doing examples in the Console, then I'll have difficulty to do it in Windows Form Apps. What do you think?

====== Hello again,

I need some feedback with my code:

        Console.WriteLine("Please enter your integer: ");
        long yourInteger;
        yourInteger = Int32.Parse(Console.ReadLine());

        //displaying the first prime number and comparing it to the given integer
        for (long i = 2; i <= yourInteger; i = i + 1)
        {
            //Controls i if its prime number or not
            if ((i % 2 != 0) || (i == 2))
            {
                Console.Write("{0} ", i);
            }

        }
+5  A: 

You'll need to know:

  • How to read user input from a Windows application
  • How to generate prime numbers within a range
  • How to write output in the way that you want

I strongly suggest that you separate these tasks. Once you've got each of them working separately, you can put them together. (Marc suggests writing a console app for the prime number section - that's a good suggestion if you don't want to get into unit testing yet. If you've used unit testing in other languages, it's reasonably easy to get up and running with NUnit. A console app will certainly be quicker to get started with though.)

In theory, for a potentially long-running task (e.g. the user inputs 1000000 as the first number) you should usually use a background thread to keep the UI responsive. However, I would ignore that to start with. Be aware that while you're computing the primes, your application will appear to be "hung", but get it working at all first. Once you're confident with the simple version, you can look at BackgroundWorker and the like if you're feeling adventurous.

Jon Skeet
I think you should mark the phrase "I would ignore that to start with" as Bold. Good answer, just as usual, John! :)
Galilyou
Thanks Jon I'll take note this advice;-)
tintincute
+9  A: 

Well, first of all I'd think about how to find prime numbers, and write that in a console app that reads a line, does the math, and writes the results (purely because that is the simplest thing you can do, and covers the same parsing etc logic you'll need later).

When you are happy with the prime number generation, then look at how to do winforms - how to put a listbox, textbox and button on a form; how to handle the click event (of the button), and how to read from the textbox and write values into the listbox. Your prime code should be fairly OK to take "as is"...

If you don't already have an IDE, then note that C# Express is free and will cover all of the above.

Marc Gravell
Rather express than sharpdevelop? Their bold refusal of supporting addins kinda made it a nogo for me once, or am i missing sth?
Peter
Indeed it doesn't support add-ins. Either product would probably be fine; simply that IMO it is easier to get examples etc that use the terminology/layout (etc) from the MS version. For somebody "new to C#" that may be important.
Marc Gravell
Ok I think I'll do this first in the console and if it works then I'll transfer it to winforms. Thanks Marc for the input
tintincute
+2  A: 

Here's a great "naive" prime number algorithm, that would be perfect for your needs: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

toxvaerd
+1  A: 

Here is a response to the edit:

Thanks guys. So you're suggesting that it's better to do it first in the Console application? I did an example of "For Loop" using Console Application a very simple one, but then when I tried to do it in the Windows Form Application, I'm not sure how to implement it. I'm afraid that if I keep doing examples in the Console, then I'll have difficulty to do it in Windows Form Apps. What do you think?

If you want to present the prime numbers as a windows forms application then you need to design the user interface for it as well. That is a bit overkill for such a small problem to be solved. The easiest design you can do is to fill up a ListBox in your form (example).

If you're really keen on learning Windows Forms or WPF then there are several resources for this.

Spoike
thanks Spoike that's what I would like to do, just display the prime numbers in the listbox with a single button.maybe I could start stating the range 1-100. and display it by single click. what do you think?
tintincute
You can try to have a TextBox to input the number on when to stop the range (instead of input a range such as "1-100" because then you'll need to do string manipulation), one button to start finding the prime numbers and a listbox to display the results. Simple as that.
Spoike
+1  A: 

I discussed creating prime numbers using the Sieve of Eratosthenes on my blog here:

http://blogs.msdn.com/mpeck/archive/2009/03/03/Solving-Problems-in-CSharp-and-FSharp-Part-1.aspx

The code looks like this...

public IEnumerable<long> GetPrimes(int max)
{
    var nonprimes = new bool[max + 1];

    for (long i = 2; i <= max; i++)
    {
        if (nonprimes[i] == false)
        {
            for (var j = i * i; j <= max; j += i)
            {
                nonprimes[j] = true;
            }

            yield return i;
        }
    }
}

With this code you can write statements like this...

var primes = SieveOfEratosthenes.GetPrimes(2000);

... to get an IEnumerable of primes up to 2000.

All the code can be found on CodePlex at http://FSharpCSharp.codeplex.com.

The code is "as is" and so you should look at it to determine whether it suits your needs, whether you need to add error checking etc, so treat it as a sample.

Martin Peck
this is very elegant. I'll study this example, thanks Martin ;-)
tintincute
Good example and blog post but you lose style points for your use of `== false` in the conditional.
Konrad Rudolph