primes/primes.cs

43 lines
686 B
C#
Raw Normal View History

2011-03-08 07:01:27 +00:00
// C# version of a Prime Number Finder
// Build under Mono .28
// haplo@mindstab.net
using System;
class Primes {
private static void Main(String[] argv)
{
int MAX=0;
if(argv.Length >= 1) {
try {
MAX = (int)Convert.ToDouble(argv[0]);
} catch {
Console.WriteLine("Invalid Max Num");
return;
}
} else {
Console.WriteLine("Useage: primes.exe [Max Num]");
return;
}
for(int cnt = 3; cnt <= MAX; cnt+= 2)
{
int test = (int) Math.Sqrt(cnt);
int isPrime = 1;
for(int i=3; i <= test; i+=2)
{
isPrime = cnt % i;
if( isPrime == 0) {
break;
}
}
if(isPrime != 0)
{
Console.WriteLine(cnt);
}
}
}
}