Skip to content

Instantly share code, notes, and snippets.

@deniskyashif
Last active October 23, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deniskyashif/f3206659121ea8ce762f to your computer and use it in GitHub Desktop.
Save deniskyashif/f3206659121ea8ce762f to your computer and use it in GitHub Desktop.
using System;
class Program
{
private const ulong N = 600851475143;
static void Main()
{
bool[] composites = new bool[(ulong)Math.Sqrt(N)];
ulong length = (ulong)composites.Length;
ulong lpf = 1;
// sieve of Eratosthenes algorithm
for (ulong i = 2; (i * i) < length; i++)
{
if (!composites[i])
{
for (ulong k = i; (i * k) < length; k++)
{
composites[i*k] = true;
}
}
}
for (ulong i = length - 1; i > 1; i -= 2)
{
if (!composites[i] && (N % i) == 0)
{
lpf = i;
break;
}
}
Console.WriteLine(lpf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment