public PrimeFactors(long number) { var primes = new PrimeGenerator(); long remainingToFactor = number; long largestPrime = number; // if we don't find one it's a prime. primes.CurrentNumber = 1; while (primes.CurrentNumber <= remainingToFactor) { primes.GetNext(); // Pull off as many factors as we can with this prime while (remainingToFactor % primes.CurrentNumber == 0) { MyFactors.Add(primes.CurrentNumber); remainingToFactor = remainingToFactor / primes.CurrentNumber; } } if (MyFactors.Count == 0) { // it's prime; MyFactors.Add(1); MyFactors.Add(number); } }
public long GetNthPrime(int n) { var generator = new PrimeGenerator(); for (int i = 0; i < n; i++) generator.GetNext(); return generator.CurrentNumber; }
public long GetSumOfPrimes(int maxPrime) { long result = 0; var generator = new PrimeGenerator(); while (generator.GetNext() < maxPrime) { result += generator.CurrentNumber; } return result; }
public void GetNextTest() { long[] primes = { 2, 3, 5, 7, 11, 13, 17 }; var generator = new PrimeGenerator(); foreach (long prime in primes) { Assert.AreEqual(prime, generator.GetNext()); } }