userS
userS

Reputation: 47

button that calc prime number in .net, c#

I need to write program that calculate prime numbers. I searched online and found a code that do that but i am new at .net and having truble to know what to write in the Button1_Click function. This is the code i took: http://www.dotnetperls.com/prime

This is the code i try to write to preform:

  namespace Test
  {
public partial class TestWebForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        IsPrime prime = new IsPrime();

    }
}

}

i know this is not good question but i really need help. Thank you!

Upvotes: 0

Views: 531

Answers (2)

Irfan TahirKheli
Irfan TahirKheli

Reputation: 3662

 Simply do it like this.


namespace Test
      {
    public partial class TestWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int number = int.Parse(txtNumber.Text);
            Response.Write(IsPrime(number));

        }

        private bool IsPrime(int number){
            int boundary = Math.Floor(Math.Sqrt(number));

            if (number == 1) return false;
            if (number == 2) return true;

            for (int i = 2; i <= boundary; ++i)  {
               if (number % i == 0)  return false;
            }

            return true;  
        }
    }

    }

Upvotes: 0

Royi Namir
Royi Namir

Reputation: 148524

1) In the link you provided ,they already have a class which is static class which has the method IsPrime.
2) you didn't include that class in your code. I did.
3) In the button click event - I test to see if 7 is a prime number
4) the result will be displayed in a blank page. ( true or false).

  namespace Test
  {
      public static class PrimeTool
      {
          public static bool IsPrime(int candidate)
          {
              // Test whether the parameter is a prime number.
              if ((candidate & 1) == 0)
              {
                  if (candidate == 2)
                  {
                      return true;
                  }
                  else
                  {
                      return false;
                  }
              }

              for (int i = 3;
                  (i * i) <= candidate; i += 2)
              {
                  if ((candidate % i) == 0)
                  {
                      return false;
                  }
              }
              return candidate != 1;
          }
      }



      public partial class TestWebForm: System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
          }

          protected void Button1_Click(object sender, EventArgs e)
          {
              bool prime = PrimeTool.IsPrime(7); //when a class is static  , you don't `new()` it.
              Response.Write("7 is prime=" + prime);

          }
      }
  }

Upvotes: 1

Related Questions