-2

I need to make a GUI application that checks prime numbers when the user input two numbers. The criteria is that I make two methods, one that checks if a number is prime and that is my isPrime(int n) and another that checks which of the two inputs is lower/higher so that the program knows where to start.

I can't manage to do this. When I run the script I don't have any output in Listbox.

My code so far:

private void button1_Click(object sender, EventArgs e)
{
    int num1, num2;
    num1 = Int32.Parse(textBox1.Text);
    num2 = Int32.Parse(textBox2.Text);

    for (int i = num1; i <= num2; i++) //Method that checks which of the two inputs is lower/hihger
    {
        if (isPrime(i))
        {
            for (int j = 2; j <= 1 / 2; j++)
            {
                if (i % j == 0)
                {
                    break;
                }
            }
            if (i == 0 && i != 1)
            {
                listBox1.Items.Add(i);
            }
        }
    }
}

private static bool isPrime(int n) //Method to check prime numbers
{
    for (int i = 2; i < n; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 2
    `j <= 1 / 2` ? Should that be `i / 2`? Also `i == 0 && i != 1` doesn't make any sense. Neither does the entire "method for checking which input is lower/higher." – Guy Incognito Jul 15 '20 at 08:51
  • Can you explain your "Method that checks which of the two inputs is lower/higher" loop? I'm not sure how it's supposed to work, or what it's intended to do with the one that it's identified as lower or higher. – Rup Jul 15 '20 at 08:53
  • Is the goal to put all of the prime numbers between num1 and num2 into a listbox? – Rup Jul 15 '20 at 08:54
  • @GuyIncognito Yes, sir! j <=i/2 – Peter Petrovski Jul 15 '20 at 08:54
  • @Rup Yes, sir! So when I run the GUI application I input two numbers and I need to get all prime numbers between them. – Peter Petrovski Jul 15 '20 at 08:55
  • If you just need to swap num1 and num2 so that num1 <= num2 then you could e.g. `if (num1 > num2) { int temp = num1; num1 = num2; num2 = temp; }` – Rup Jul 15 '20 at 08:55
  • Could you, please, describe the *initial problem*? Given two numbers `num1` and `num2` you want to obtain a collection of what? Could you provide some *examples*? What is the desired result for, say, `num1 = 1, num2 = 10`? – Dmitry Bychenko Jul 15 '20 at 08:59
  • Do you really want to _compute_ primes each time (and on the GUI Thread)? I'd pre-compute all primes in the int32 span of numbers and have them in some kind of list. Then you only need to query that list for the span of [Min(num1, num2), Max(num1, num2)]. – Fildor Jul 15 '20 at 09:00
  • @Rup So I created the method isPrime(int n) that check if the number is prime and I have to create the second method where I need loop from the lower number to the higher number and then call for method isPrime(i)... but I can't get output – Peter Petrovski Jul 15 '20 at 09:01
  • @DmitryBychenko Desire result is to get all prime numbers between num1 and num2, that can be any number – Peter Petrovski Jul 15 '20 at 09:02
  • 1
    You don't get output because `if (i == 0 && i != 1)` will never be true. – Fildor Jul 15 '20 at 09:02
  • @Fidor I see that mistake, thanks! – Peter Petrovski Jul 15 '20 at 09:04

3 Answers3

2

Let's start from isPrime. Since it takes int n as an argument, n can be up to int.MaxValue ~ 2_000_000_000; 2 billions iterations is a bit too many when we can easily implement a routine with sqrt(2_000_000_000) / 2 ~ 30_000 loops only:

    private static bool isPrime(int n) {
      if (n <= 1)
        return false;
      else if (n % 2 == 0)
        return n == 2;

      int max = (int)(Math.Sqrt(n) + 0.5);

      for (int divisor = 3; divisor <= max; divisor += 2)
        if (n % divisor == 0)
          return false;

      return true;   
    }     

Now, it's time to get the primes between num1 and num2:

    for (int i = num1; i <= num2; ++i)
      if (isPrime(i))
        listBox1.Items.Add(i);

please, note, that since we've optimized isPrime we can keep for loop being very simple.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

Your isPrime function is correct, just minor change you need is that update condition to i <= Math.Sqrt(n) as for (int i = 2; i <= Math.Sqrt(n); i++). That would be more efficient. Sieve of Eristhostenes.

You can also refer below links to find better and efficient way to check isPrime. As suggested by @Fildor in comments.

To find min or max value from two inputs use Math.Min & Math.Max like shown below. And update your for loop as below.

private void button1_Click(object sender, EventArgs e)
{
    int num1, num2;
    num1 = Math.Min(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text));
    num2 = Math.Max(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text));

    for (int i = num1; i <= num2; i++) //Method that checks which of the two inputs is lower/hihger
    {
        if (i != 0 && i != 1 && isPrime(i))
        {               
            listBox1.Items.Add(i);
        }
    }
}

private static bool isPrime(int n) //Method to check prime numbers
{
    for (int i = 2; i <= Math.Sqrt(n); i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}
Karan
  • 12,059
  • 3
  • 24
  • 40
  • You do realize that this is inefficient as hell, do you? (Given OP's implementation of `isPrime`) – Fildor Jul 15 '20 at 09:05
  • @Fildor I can't see any other way of doing it... sorry – Peter Petrovski Jul 15 '20 at 09:06
  • @Fildor feels free to suggest a better `isPrime` implementation – Cleptus Jul 15 '20 at 09:07
  • 1
    Do a search for "[Sieve of Eristhostenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)" – Fildor Jul 15 '20 at 09:08
  • @bradbury9 I wouldn't at all. I'd have a precompiled set of primes to look up. – Fildor Jul 15 '20 at 09:09
  • 1
    And you even can find some on here: https://stackoverflow.com/a/1510186/982149 , https://stackoverflow.com/a/1072205/982149 – Fildor Jul 15 '20 at 09:16
  • 1
    @Fildor, Thanks man, One of the link I have already added. Also added another one as it would definitely help more. – Karan Jul 15 '20 at 09:22
  • @Karan Sorry for bothering you again, my method for checking prime numbers is not working when I put down Math.Sqrt(n), I mean if I check from 1 to 10 it's showing 2, 3, 4,5, 7, and 9... am I doing something wrong or what do I need to change there? – Peter Petrovski Jul 18 '20 at 12:23
  • That was my bad. You should update condition in for loop as `i <= Math.Sqrt(n)`. Updated answer also. – Karan Jul 19 '20 at 11:46
0

Just to add another view to the problem:

I wouldn't check ( as in "compute" ) primes here at all.

Int32 contains a pretty finite number of primes, which allows us to either precompute or even just download a list from the internet if you can find one.

As soon as we have it, all you need to do is a lookup of those that happen to be in the desired range.

Fildor
  • 14,510
  • 4
  • 35
  • 67