public PrimeSieve2(long upTo) { primes = new GrowableArray<bool>(upTo); primes.Set(0, true); primes.Set(1, true); PopulateSieveFrom(primes); }
public void TestArrayAccess() { int sz = 100; GrowableArray<int> ga = new GrowableArray<int>(sz); for(int i = 0; i < 2 * sz; ++i) { ga.Set(i, 13*i); Assert.AreEqual(i < sz ? sz : 2 * sz, ga.LongLength); } for(int i = 0; i < 2 * sz; ++i) { Assert.AreEqual(i*13, ga.Get(i)); } }
private static void PopulateSieveFrom(GrowableArray<bool> sieve, long from = 2) { long upTo = (long)Math.Sqrt(sieve.LongLength) + 1; for (long i = 2; i < upTo; i++) { if (!sieve.Get(i)) { var k = (i < from) ? FirstK(i, from) : 2 * i; while (k < sieve.LongLength) { sieve.Set(k,true); k += i; } } } }