Skip to content

Instantly share code, notes, and snippets.

@kashwaa
Created February 6, 2014 23:31
Show Gist options
  • Save kashwaa/8854715 to your computer and use it in GitHub Desktop.
Save kashwaa/8854715 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
long num = 600851475143;
int start = 2;
while (!isprime(num))
{
while (num % start != 0)
{
start = getNextPrime(start);
}
num /= start;
}
sw.Stop();
Console.WriteLine("calculted in {0} ",sw.Elapsed);
Console.WriteLine(num);
}
static int getNextPrime(int i)
{
int x = ++i;
while (!isprime(x))
{
x++;
}
return x;
}
static bool isprime(long n)
{
if (n < 2)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
int max_divisor = (int)Math.Sqrt(n);
int divisor = 5;
while (divisor <= max_divisor)
{
if (n % divisor == 0 || n % (divisor + 2) == 0)
return false;
divisor += 6;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment