From f3f683248de96ecc8be6041c3832ca8108125cfc Mon Sep 17 00:00:00 2001 From: Francois Botha Date: Fri, 21 Aug 2015 23:09:27 +0200 Subject: [PATCH] Fix prime number implementation. --- QLNet/Math/PrimeNumbers.cs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/QLNet/Math/PrimeNumbers.cs b/QLNet/Math/PrimeNumbers.cs index 0c39b2aa0..a89cdc06f 100644 --- a/QLNet/Math/PrimeNumbers.cs +++ b/QLNet/Math/PrimeNumbers.cs @@ -54,22 +54,17 @@ private PrimeNumbers() { } public static ulong get(int absoluteIndex) { if (primeNumbers_.empty()) - { - //int n = sizeof(firstPrimes)/sizeof(firstPrimes[0]); - int n = 8*sizeof(ulong)/sizeof(ulong); - //primeNumbers_.insert(primeNumbers_.end(),firstPrimes, firstPrimes+n); - //IEnumerable colection=firstPrimes..ForEach((i, x) => i ().GetRange(0,n)); - } - while (primeNumbers_.Count<=absoluteIndex) + primeNumbers_.AddRange(firstPrimes); + + while (primeNumbers_.Count<=absoluteIndex) nextPrimeNumber(); - return primeNumbers_[absoluteIndex]; + + return primeNumbers_[absoluteIndex]; } private static ulong nextPrimeNumber() { - //ulong p, n, m = primeNumbers_.back(); - ulong p, n, m = primeNumbers_.First(); + ulong p, n, m = primeNumbers_.Last(); do { // skip the even numbers m += 2; @@ -80,12 +75,10 @@ private static ulong nextPrimeNumber() p = primeNumbers_[i]; ++i; } - while ((m % p==1) && p <= n);//while ( m%p && p<=n ); + while ((m % p != 0) && p <= n); } while ( p<=n ); primeNumbers_.Add(m); return m; } - } - }