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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP.NET/C# Prime Numbers

ASP.NET/C# Prime Numbers

Scheduled Pinned Locked Moved ASP.NET
csharpasp-net
4 Posts 3 Posters 2 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.
  • H Offline
    H Offline
    Harry2000
    wrote on last edited by
    #1

    Am try to tell if a number is prime or not. I only can get this to work with even number. It say that 15,21,25 35 ect. it said thatthey are prime number but there not. Prime number are only divisible by it self and one. Here is the code its in C#. This is for a math apllication that am work on. int iPrime = Int32.Parse(TextBox1.Text) for (int icount = 2; icount <= (iPrime / 2); icount++) { if((iPrime % icount) == 0) { Label1.Text = "False"; return; } Label1.Text = "True"; return; } Harrison Brock

    I 1 Reply Last reply
    0
    • H Harry2000

      Am try to tell if a number is prime or not. I only can get this to work with even number. It say that 15,21,25 35 ect. it said thatthey are prime number but there not. Prime number are only divisible by it self and one. Here is the code its in C#. This is for a math apllication that am work on. int iPrime = Int32.Parse(TextBox1.Text) for (int icount = 2; icount <= (iPrime / 2); icount++) { if((iPrime % icount) == 0) { Label1.Text = "False"; return; } Label1.Text = "True"; return; } Harrison Brock

      I Offline
      I Offline
      Ian Darling
      wrote on last edited by
      #2

      Your return for true is in the wrong place. try:for (int icount = 2; icount <= (iPrime / 2); icount++) { if((iPrime % icount) == 0) { Label1.Text = "False"; return; } } // end of for loop Label1.Text = "True"; return;
      -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

      A 1 Reply Last reply
      0
      • I Ian Darling

        Your return for true is in the wrong place. try:for (int icount = 2; icount <= (iPrime / 2); icount++) { if((iPrime % icount) == 0) { Label1.Text = "False"; return; } } // end of for loop Label1.Text = "True"; return;
        -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

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

        Good code. A few mathematics pointers though. You dont really need to check up to number / 2. If the numbers up til Ceiling(sqrt(iPrime/2)) dont return a mod result of 0 then the higher numbers wont either. e.g lets say u r testing 30 for prime (I know its not). Using the num/2 method the candidates are all numbers 1 through 15. Using the sqrt method the numbers are 1 through 6. Lets pick a number > 6 say 10 and the mod returns 0. But you have already tested 10 when you divided by 3 (3 * 10 = 30) Secondly, an even number cannot be prime so you can short circuit your code. Apologies for the not so major corrections but efficiency and performance are key to any successful coding endeavor.int test = Ceiling(sqrt(iPrime)); if ((iPrime % 2) == 0) { Label1.Text = "False"; return; } else for (int icount = 2; icount <= test; icount++) { if((iPrime % icount) == 0) { Label1.Text = "False"; return; } } Label1.Text = "True"; return;

        H 1 Reply Last reply
        0
        • A Asad Hussain

          Good code. A few mathematics pointers though. You dont really need to check up to number / 2. If the numbers up til Ceiling(sqrt(iPrime/2)) dont return a mod result of 0 then the higher numbers wont either. e.g lets say u r testing 30 for prime (I know its not). Using the num/2 method the candidates are all numbers 1 through 15. Using the sqrt method the numbers are 1 through 6. Lets pick a number > 6 say 10 and the mod returns 0. But you have already tested 10 when you divided by 3 (3 * 10 = 30) Secondly, an even number cannot be prime so you can short circuit your code. Apologies for the not so major corrections but efficiency and performance are key to any successful coding endeavor.int test = Ceiling(sqrt(iPrime)); if ((iPrime % 2) == 0) { Label1.Text = "False"; return; } else for (int icount = 2; icount <= test; icount++) { if((iPrime % icount) == 0) { Label1.Text = "False"; return; } } Label1.Text = "True"; return;

          H Offline
          H Offline
          Harry2000
          wrote on last edited by
          #4

          Thank It work good now. Harrison Brock

          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