Skip to content

Instantly share code, notes, and snippets.

@vdonchev
Last active August 29, 2015 14:27
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 vdonchev/8ef2dd8f28a8c5b8f118 to your computer and use it in GitHub Desktop.
Save vdonchev/8ef2dd8f28a8c5b8f118 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Primes
{
class Program
{
static void Main()
{
int startNum = int.Parse(Console.ReadLine());
int endNum = int.Parse(Console.ReadLine());
List<int> primes = Enumerable.Range(0, endNum + 1).ToList();
for (int i = 2; i <= Math.Sqrt(endNum); i++)
{
for (int j = i + i; j < endNum; j += i)
{
primes[j] = -1;
}
}
primes.RemoveAll(num => num < 2 || num == -1 || num < startNum);
Console.WriteLine("Primes in range ({0} - {1}): {2}", startNum, endNum, string.Join(", ", primes));
Console.WriteLine("Number of primes: {0}", primes.Count);
Console.WriteLine("Sum of found primes: {0}", primes.Sum());
Console.WriteLine("Primes ending with 1: {0}", primes.Count(n => n % 10 == 1));
Console.WriteLine("Primes ending with 3: {0}", primes.Count(n => n % 10 == 3));
Console.WriteLine("Primes ending with 7: {0}", primes.Count(n => n % 10 == 7));
Console.WriteLine("Primes ending with 9: {0}", primes.Count(n => n % 10 == 9));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment