Skip to content

Instantly share code, notes, and snippets.

@kashwaa
Last active August 29, 2015 14:01
Show Gist options
  • Save kashwaa/33f32846a54a382dd95a to your computer and use it in GitHub Desktop.
Save kashwaa/33f32846a54a382dd95a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Euler50
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int firstAddedPrime = 2;
int lastAddedPrime = 2;
while (sum+GetNextPrime(lastAddedPrime) < 1000000 )
{
sum += lastAddedPrime;
lastAddedPrime=GetNextPrime(lastAddedPrime);
}
while (!IsPrime(sum))
{
sum -= firstAddedPrime;
firstAddedPrime = GetNextPrime(firstAddedPrime);
}
Console.WriteLine(sum);
}
public static int GetNextPrime(int n = 1)
{
int i = n + 1;
while (!IsPrime(i))
{
i++;
}
return i;
}
public static bool IsPrime(int n)
{
int result = 0;
int start = 2;
int end = n;
for (int i = start; i < end; i += 1)
{
if (n % i == 0)
{
start = i;
end = n / i;
result += start == end ? 1 : 2;
}
}
return result==0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment