Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Created August 15, 2012 20:54
Show Gist options
  • Select an option

  • Save kevinmeredith/3363580 to your computer and use it in GitHub Desktop.

Select an option

Save kevinmeredith/3363580 to your computer and use it in GitHub Desktop.
Primes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<int> primes = new List<int>();
primes = getAllPrimes(50);
print(primes);
Console.ReadLine();
}
static public List<int> getAllPrimes(int n)
{
List<int> primes = new List<int>();
while (n > 1)
{
if (isPrimeNumber(n))
primes.Add(n);
--n;
}
return primes;
}
static private bool isPrimeNumber(int number)
{
int divisor = number - 1;
while (divisor > 1)
{
if (number % divisor == 0)
return false;
--divisor;
}
return true;
}
static void print(List<int> list)
{
Console.WriteLine("Primes:");
list.Reverse();
foreach (int item in list)
Console.Write(item + ", ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment