0
int number;
Console.Write("Enter a number: ");
number = int.Parse(Console.ReadLine());


int ctr = 2;
while(ctr <= number)
{
if(number % ctr == 0)
break;
ctr++;
}
if(ctr == number)
Console.WriteLine("Prime");
else
Console.WriteLine("Not Prime");

Console.ReadKey();

Edited by happygeek: fixed formatting

int number;
     Console.Write("Enter a number: ");
     number = int.Parse(Console.ReadLine());
     
     int ctr = 2;
     while(ctr <= number)
     {
           if(number % ctr == 0)
                break;
           ctr++;
     }
     if(ctr == number)
         Console.WriteLine("Prime");
     else
         Console.WriteLine("Not Prime");

     Console.ReadKey();
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by ddanbe
0

The short explaintion of a more efficient version:

When finding prime numbers, you should only test 2 and 3 to the square root of the number in increments of 2 (eg odd numbers), even if you arent going to use a list of generated prime numbers to reduce the time further.

int number;
     Console.Write("Enter a number: ");
     number = int.Parse(Console.ReadLine());
     
     int ctr = 2;
     int sq = (int)Math.Sqrt(number);
     while(ctr < sq)
     {
           if(number % ctr == 0)
                break;
           if (ctr==2) { ctr++; } else {ctr+=2;}
     }
     if(ctr == sq)
         Console.WriteLine("Prime");
     else
         Console.WriteLine("Not Prime as divides by "+ctr.ToString());

     Console.ReadKey();
0

LizR you are quite right using sqrt!
But!
Which is faster in the long run?
Counting to the number or counting to the sqrt of the number?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.