I have created a simple program to calculate primes as follows:
var db = new HighScoreEntities();
List<Int64> primes = new List<Int64>(){1};
for (Int64 x = 2; x < Int64.MaxValue; x++)
{
if (primes.FirstOrDefault(y=> x%y == 0) == 0){
primes.Add(x);
db.Primes.AddObject(Prime.CreatePrime(x));
db.SaveChanges();
}
}
My issue is that y is coming out with 225 on the first go through and what seems like random numbers afterwards. Why isn’t it iterating through the ‘primes’ list? I also tried using the Exists function with the same result.
1 isn’t a prime, so adding it to
primesis probably a bad start. It looks like on every loop iteration you are finding the first element inprimessuch that the remainder of x / 1 is 0, which will always be true.I didn’t try the program out myself so I could be wrong, but that should be a good place to start.