prime numbers c#

前端 未结 8 1673

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 wri

相关标签:
8条回答
  • 2020-12-11 18:50

    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.

    0 讨论(0)
  • 2020-12-11 18:56

    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.

    0 讨论(0)
  • 2020-12-11 18:57
    using System;
    class demo
    {
       static void Main()
       {
          int number;
          Console.WriteLine("Enter Number you Should be Checked Number is Prime or not Prime");
          number = Int32.Parse(Console.ReadLine());
          for(int i =2;i {
             if(number % i == 0)
             {
                Console.WriteLine("Entered number is not Prime");
                break;
             }
          }
          if(number % i !=0)
          {
             Console.WriteLine("Entered Number is Prime");
          }
    
          Console.ReadLine();
       }
    }
    
    0 讨论(0)
  • 2020-12-11 19:02

    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.

    0 讨论(0)
  • 2020-12-11 19:06

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

    0 讨论(0)
  • 2020-12-11 19:09

    Your approach is entirely wrong. Prime numbers are absolute and will never change. Your best bet is to pre-generate a long list of prime numbers. Then come up with an algorithm to quickly look up that number to determine if it is on the list. Then in your case (since you want to list all in the given range just do so). This solution will be much faster than any prime number finding algorithm implemented during run-time. If the integer entered is greater than your list then you can always implement the algorithm starting at that point.

    0 讨论(0)
提交回复
热议问题