I would like a complete threshing of this code so that I can see what I did wrong and what I am using incorrectly.
I made this super simple, trying to learn a little bit about List<T>
while I was doing this. I have never actually tried to do this before, so I thought it would be a good learning experience, but it just seems too easy (simple), especially when I look at other people's renditions of this algorithm. Am I missing a vital piece of the algorithm?
Note: Console app, if you want to see all the primes you will have to set the buffer of your console.
static void Main(string[] args)
{
var upperLimit = 9999;
List<Int64> primes = new List<Int64>();
for (Int64 i = 2; i < upperLimit; i++)
{
primes.Add(i);
}
List<Int64> numbers = primes;
for (var i = 2; i < upperLimit; i++)
{
foreach (var number in numbers.ToArray())
{
if (number == i) { continue; }
if (number % i == 0)
{
primes.Remove(number);
}
}
numbers = primes;
}
primes.ForEach(delegate(Int64 prime)
{
Console.WriteLine(prime.ToString());
});
Console.WriteLine("The Last Prime is " + primes[primes.Count - 1]);
Console.WriteLine("what are you waiting for? Exit the program!");
Console.ReadLine();
}
I figured that I would give this a try after I saw this.
The reason for var number in numbers.ToArray()
is because it wouldn't let me .Remove(number)
from primes if I foreach
'ed through the list otherwise. Credit to this answer for the solution to my dilemma.
Follow-up can be found here