static int ConsumeCpu(IEnumerable<string> args) { int duration = 0; var options = new OptionSet() { {"duration=", v => duration = Int32.Parse(v)}, }; options.Parse(args); Stopwatch stopwatch = Stopwatch.StartNew(); var threads = new List<Thread>(); for (var i = 0; i < 10; i++) { var thread = new Thread(() => { while (duration > stopwatch.ElapsedMilliseconds) { var sieve = new PrimeSieve(500); sieve.ComputePrimes(); } }); threads.Add(thread); } foreach (var thread in threads) thread.Start(); foreach (var thread in threads) thread.Join(); return SUCCEEDED; }
private void PrimeFactors(int n) { var maxBound = n / 3; var lastList = this.primeList.Length - 1; var start = this.tower[1] == 2 ? 1 : 0; var sieve = new PrimeSieve(n); for (var section = start; section < this.primeList.Length; section++) { var primes = sieve.GetPrimeCollection(this.tower[section] + 1, this.tower[section + 1]); foreach (var prime in primes) { this.primeList[section][this.listLength[section]++] = prime; if (prime > maxBound) continue; var np = n; do { var k = lastList; var q = np /= prime; do if ((q & 1) == 1) //if q is odd { this.primeList[k][this.listLength[k]++] = prime; } while (((q >>= 1) > 0) && (prime <= this.bound[--k])); } while (prime <= np); } } }
private int PrimeFactors(int n) { var sieve = new PrimeSieve(n); var primes = sieve.GetPrimeCollection(3, n); int maxBound = n / 2, count = 0; foreach (var prime in primes) { var m = prime > maxBound ? 1 : 0; if (prime <= maxBound) { var q = n; while (q >= prime) { m += q /= prime; } } this.primeList[count] = prime; this.multiList[count++] = m; } return count; }
public XInt Factorial(int n) { if (n < 20) { return XMath.Factorial(n); } this.sieve = new PrimeSieve(n); return this.RecFactorial(n); }
public static Xint Binomial(int n, int k) { if (0 > k || k > n) { throw new System.ArgumentOutOfRangeException("n", "Binomial: 0 <= k and k <= n required, but n was " + n + " and k was " + k); } if (k > n / 2) { k = n - k; } int rootN = (int)System.Math.Floor(System.Math.Sqrt(n)); Xint binom = Xint.One; var primeCollection = new PrimeSieve(n).GetPrimeCollection(); foreach (int prime in primeCollection) // Equivalent to a nextPrime() function. { if (prime > n - k) { binom *= prime; continue; } if (prime > n / 2) { continue; } if (prime > rootN) { if (n % prime < k % prime) { binom *= prime; } continue; } int exp = 0, r = 0, N = n, K = k; while (N > 0) { r = (N % prime) < (K % prime + r) ? 1 : 0; exp += r; N /= prime; K /= prime; } if (exp > 0) { binom *= Xint.Pow((Xint) prime, exp); } } return binom; }
static Options() { DefaultInitialConcurrency = HardwareInfo.GetProcessorCountPo2Factor(16); var capacity = HardwareInfo.GetProcessorCountPo2Factor(128, 128); CapacityPrimeSieve = new PrimeSieve(capacity + 1024); while (!CapacityPrimeSieve.IsPrime(capacity)) { capacity--; } DefaultInitialCapacity = capacity; }
public XInt Factorial(int n) { if (n < 20) { return XMath.Factorial(n); } this.sieve = new PrimeSieve(n); var pLen = (int)(2.0 * (XMath.FloorSqrt(n) + (double)n / (XMath.Log2(n) - 1))); this.primeList = new int[pLen]; var exp2 = n - XMath.BitCount(n); return this.RecFactorial(n) << exp2; }
static Options() { var capacity = Math.Min(16_384, HardwareInfo.ProcessorCountPo2 * 128); CapacityPrimeSieve = new PrimeSieve(capacity + 1024); while (!CapacityPrimeSieve.IsPrime(capacity)) { capacity--; } DefaultInitialCapacity = capacity; // Debug.WriteLine($"{nameof(ComputedRegistry)}.{nameof(Options)}.{nameof(DefaultInitialCapacity)} = {DefaultInitialCapacity}"); }
public object Solve() { var limit = 8389; // The greatest prime in the answer //var limit = 10.Power(4); _sieve = new PrimeSieve(); var attempt = LookForSolution(limit, 1); return(attempt > 0 ? attempt : LookForSolution(limit, 2)); }
// TODO public object Solve() { _sieve = new PrimeSieve(1000); long sum = 0; // Hmm.. All candidates are odd prime combinations whose product is less than 50 000 000. Research(); // Nope... just... totally wrong. //const int limit = 50000000; //var sqrt = limit.Sqrt().Ceiling().ToInt(); //var primes = new PrimeSieve(limit).Primes.Skip(1).ToArray(); //var couldHaveProperty = new bool[limit]; //for (long index = 0; index < couldHaveProperty.Length; index++) //{ // couldHaveProperty[index] = index.IsOdd(); //} //foreach (var prime in primes.TakeWhile(p => p < sqrt)) //{ // var primeSquared = prime.Squared(); // Always odd // for (var multiple = primeSquared; multiple < limit; multiple += primeSquared * 2) // { // couldHaveProperty[multiple] = false; // } //} //for (long index = 1; index < couldHaveProperty.Length; index += 2) //{ // if (!couldHaveProperty[index]) continue; // couldHaveProperty[index] = (index * 2 + 1).IsPrime(); //} //var candidates = new List<long>(); //for (var index = 1; index < couldHaveProperty.Length; index++) //{ // if (!couldHaveProperty[index]) continue; // candidates.Add(index * 2); //} return(0); //return candidates.Where(HasProperty).Sum(); }
public void Deciding() { var sieve = new PrimeSieve(1000); sieve.IsPrime(2).ShouldBeTrue(); sieve.IsPrime(3).ShouldBeTrue(); sieve.IsPrime(5).ShouldBeTrue(); sieve.IsPrime(151).ShouldBeTrue(); sieve.IsPrime(9973).ShouldBeTrue(); sieve.IsPrime(999983).ShouldBeTrue(); sieve.IsPrime(6).ShouldBeFalse(); sieve.IsPrime(1000001).ShouldBeFalse(); sieve.IsPrime(1320359782305982082).ShouldBeFalse(); }
public object Solve() { // Easily done by hand, but generalized to be fast for numbers as large as 10^5 // (Fun fact: The product at this magnitude is an insane 43452 digits) var number = 20; var sieve = new PrimeSieve(number); BigInteger product = 1; foreach (var prime in sieve.GetPrimes()) { product *= GetLargestPowerOfPrime(prime, number); } return(product); }
public XInt Factorial(int n) { if (n < 20) { return(XMath.Factorial(n)); } this.sieve = new PrimeSieve(n); var pLen = (int)(2.0 * (XMath.FloorSqrt(n) + (double)n / (XMath.Log2(n) - 1))); this.primeList = new int[pLen]; var exp2 = n - XMath.BitCount(n); return(this.RecFactorial(n) << exp2); }
public object Solve() { const int arbitraryLimit = 200000; var sieve = new PrimeSieve(arbitraryLimit); var numPrimeFactors = new int[arbitraryLimit]; foreach (var prime in sieve.GetPrimes()) { for (var number = prime; number < arbitraryLimit; number += prime) { numPrimeFactors[number]++; } } return(FindTheSmallestOfFourConsecutive(arbitraryLimit, number => numPrimeFactors[number])); }
public XInt Factorial(int n) { if (n < 20) { return(XMath.Factorial(n)); } var sieve = new PrimeSieve(n); var task2 = Task.Factory.StartNew <XInt>(() => { this.results2 = new IAsyncResult[XMath.FloorLog2(n)]; this.swingDelegate2 = Swing2; this.taskCounter2 = 0; var N = n; // -- It is more efficient to add the big swings // -- first and the small ones later! while (N >= Smallswing) { this.results2[this.taskCounter2++] = this.swingDelegate2.BeginInvoke(sieve, N, null, null); N >>= 1; } return(this.RecFactorial2(n)); }); this.results3 = new IAsyncResult[XMath.FloorLog2(n)]; this.swingDelegate3 = Swing3; this.taskCounter3 = 0; var m = n; // -- It is more efficient to add the big swings // -- first and the small ones later! while (m >= Smallswing) { this.results3[this.taskCounter3++] = this.swingDelegate3.BeginInvoke(sieve, m, null, null); m >>= 1; } var task3Result = this.RecFactorial3(n); return((task2.Result * task3Result) << (n - XMath.BitCount(n))); }
private static XInt Swing3(PrimeSieve sieve, int n) { var primorial = Task.Factory.StartNew <XInt>(() => { var start = sieve.NextPrime(n / 2); start = sieve.NextPrime(start); return(sieve.GetPrimorial(start, n, 2)); }); var count = 0; var rootN = XMath.FloorSqrt(n); var startPrime = sieve.NextPrime(rootN); startPrime = sieve.NextPrime(startPrime); var aPrimes = sieve.GetPrimeCollectionEveryOther(5, rootN); var bPrimes = sieve.GetPrimeCollectionEveryOther(startPrime, n / 3); var primeList = new int[aPrimes.NumberOfPrimes + bPrimes.NumberOfPrimes]; foreach (var prime in aPrimes) { int q = n, p = 1; while ((q /= prime) > 0) { if ((q & 1) == 1) { p *= prime; } } if (p > 1) { primeList[count++] = p; } } foreach (var prime in bPrimes.Where(prime => ((n / prime) & 1) == 1)) { primeList[count++] = prime; } var primeProduct = XMath.Product(primeList, 0, count); return(primeProduct * primorial.Result); }
public XInt Factorial(int n) { if (n < 20) { return XMath.Factorial(n); } var sieve = new PrimeSieve(n); this.results = new IAsyncResult[XMath.FloorLog2(n)]; this.swingDelegate = Swing; this.taskCounter = 0; var N = n; // -- It is more efficient to add the big swings // -- first and the small ones later! while (N >= Smallswing) { this.results[this.taskCounter++] = this.swingDelegate.BeginInvoke(sieve, N, null, null); N >>= 1; } return this.RecFactorial(n) << (n - XMath.BitCount(n)); }
public XInt Factorial(int n) { if (n < 20) { return(XMath.Factorial(n)); } this.sieve = new PrimeSieve(n); var log2N = XMath.FloorLog2(n); var source = new int[log2N]; int h = 0, shift = 0, length = 0; // -- It is more efficient to add the big intervals // -- first and the small ones later! Is it? while (h != n) { shift += h; h = n >> log2N--; if (h > 2) { source[length++] = h; } } var results = new XInt[length]; Parallel.For(0, length, currentIndex => results[currentIndex] = this.Swing(source[currentIndex]) ); XInt p = XInt.One, r = XInt.One, rl = XInt.One; for (var i = 0; i < length; i++) { var t = rl * results[i]; p = p * t; rl = r; r = r * p; } return(r << shift); }
public XInt Factorial(int n) { if (n < 20) { return XMath.Factorial(n); } var sieve = new PrimeSieve(n); var task2 = Task.Factory.StartNew<XInt>(() => { this.results2 = new IAsyncResult[XMath.FloorLog2(n)]; this.swingDelegate2 = Swing2; this.taskCounter2 = 0; var N = n; // -- It is more efficient to add the big swings // -- first and the small ones later! while (N >= Smallswing) { this.results2[this.taskCounter2++] = this.swingDelegate2.BeginInvoke(sieve, N, null, null); N >>= 1; } return this.RecFactorial2(n); }); this.results3 = new IAsyncResult[XMath.FloorLog2(n)]; this.swingDelegate3 = Swing3; this.taskCounter3 = 0; var m = n; // -- It is more efficient to add the big swings // -- first and the small ones later! while (m >= Smallswing) { this.results3[this.taskCounter3++] = this.swingDelegate3.BeginInvoke(sieve, m, null, null); m >>= 1; } var task3Result = this.RecFactorial3(n); return (task2.Result * task3Result) << (n - XMath.BitCount(n)); }
public object Solve() { var limit = 5 * 10.Power(7); var primeLimit = limit.Sqrt().Ceiling().ToLong(); var sieve = new PrimeSieve(primeLimit); var squareRootPrimes = sieve.GetPrimes().ToArray(); var cubeRootPrimes = squareRootPrimes.Where(p => p <= limit.NthRoot(3)).ToArray(); var fourthRootPrimes = squareRootPrimes.Where(p => p <= limit.NthRoot(4)).ToArray(); var numbers = new HashSet <long>(); foreach (var p1 in squareRootPrimes) { foreach (var p2 in cubeRootPrimes) { var partialSum = p1.Power(2) + p2.Power(3); if (partialSum > limit) { break; } foreach (var p3 in fourthRootPrimes) { var sum = partialSum + p3.Power(4); if (sum < limit && !numbers.Contains(sum)) { numbers.Add(sum); } } } } return(numbers.Count); }
public XInt Factorial(int n) { if (n < 20) { return(XMath.Factorial(n)); } this.sieve = new PrimeSieve(n); var log2N = XMath.FloorLog2(n); SwingDelegate swingDelegate = this.Swing; var results = new IAsyncResult[log2N]; int h = 0, shift = 0, taskCounter = 0; // -- It is more efficient to add the big intervals // -- first and the small ones later! while (h != n) { shift += h; h = n >> log2N--; if (h > 2) { results[taskCounter++] = swingDelegate.BeginInvoke(h, null, null); } } XInt p = XInt.One, r = XInt.One, rl = XInt.One; for (var i = 0; i < taskCounter; i++) { var t = rl * swingDelegate.EndInvoke(results[i]); p = p * t; rl = r; r = r * p; } return(r << shift); }
private static void Main() { var primeList = PrimeSieve.PrimesList(30); foreach (var prime in primeList) { Console.Write(prime + " "); } var sieveOfEratosthenes = PrimeSieve.SieveOfEratosthenes(30); var primes = sieveOfEratosthenes .Select((x, y) => new { Index = y, IsPrime = x }) .Where(x => x.IsPrime && x.Index >= 1) .Select(x => x.Index) .ToList(); primes.ForEach(Console.WriteLine); }
public XInt Factorial(int n) { if (n < 20) { return(XMath.Factorial(n)); } var sieve = new PrimeSieve(n); this.results = new IAsyncResult[XMath.FloorLog2(n)]; this.swingDelegate = Swing; this.taskCounter = 0; var N = n; // -- It is more efficient to add the big swings // -- first and the small ones later! while (N >= Smallswing) { this.results[this.taskCounter++] = this.swingDelegate.BeginInvoke(sieve, N, null, null); N >>= 1; } return(this.RecFactorial(n) << (n - XMath.BitCount(n))); }
private static XInt Swing(PrimeSieve sieve, int n) { var primorial = Task.Factory.StartNew<XInt>(() => sieve.GetPrimorial(n / 2 + 1, n)); var count = 0; var rootN = XMath.FloorSqrt(n); var aPrimes = sieve.GetPrimeCollection(3, rootN); var bPrimes = sieve.GetPrimeCollection(rootN + 1, n / 3); var primeList = new int[aPrimes.NumberOfPrimes + bPrimes.NumberOfPrimes]; foreach (var prime in aPrimes) { int q = n, p = 1; while ((q /= prime) > 0) { if ((q & 1) == 1) { p *= prime; } } if (p > 1) { primeList[count++] = p; } } foreach (var prime in bPrimes.Where(prime => ((n / prime) & 1) == 1)) { primeList[count++] = prime; } var primeProduct = XMath.Product(primeList, 0, count); return primeProduct * primorial.Result; }
public static List<int> GetPrimesUpToN(int n) { var output = new List<int>(); var sieve = new PrimeSieve(n); for (int index = 2; index <= n; index++) { //If not already identified as a non-prime if (!sieve.IsComposite(index)) { //Add every multiple of index up to n for (int multiple = 2; multiple * index <= n; multiple++) { sieve.SetComposite(multiple * index); } } } for (int index = 2; index <= n; index++) { if (!sieve.IsComposite(index)) output.Add(index); } return output; }
public XInt Factorial(int n) { if (n < 20) { return XMath.Factorial(n); } this.sieve = new PrimeSieve(n); var log2N = XMath.FloorLog2(n); SwingDelegate swingDelegate = this.Swing; var results = new IAsyncResult[log2N]; int h = 0, shift = 0, taskCounter = 0; // -- It is more efficient to add the big intervals // -- first and the small ones later! while (h != n) { shift += h; h = n >> log2N--; if (h > 2) { results[taskCounter++] = swingDelegate.BeginInvoke(h, null, null); } } XInt p = XInt.One, r = XInt.One, rl = XInt.One; for (var i = 0; i < taskCounter; i++) { var t = rl * swingDelegate.EndInvoke(results[i]); p = p * t; rl = r; r = r * p; } return r << shift; }
private void PrimeFactors(int n) { var maxBound = n / 3; var lastList = this.primeList.Length - 1; var start = this.tower[1] == 2 ? 1 : 0; var sieve = new PrimeSieve(n); for (var section = start; section < this.primeList.Length; section++) { var primes = sieve.GetPrimeCollection(this.tower[section] + 1, this.tower[section + 1]); foreach (var prime in primes) { this.primeList[section][this.listLength[section]++] = prime; if (prime > maxBound) { continue; } var np = n; do { var k = lastList; var q = np /= prime; do { if ((q & 1) == 1) //if q is odd { this.primeList[k][this.listLength[k]++] = prime; } }while (((q >>= 1) > 0) && (prime <= this.bound[--k])); } while (prime <= np); } } }
private int PrimeFactors(int n) { var sieve = new PrimeSieve(n); var primeCollection = sieve.GetPrimeCollection(3, n); int maxBound = n / 2, count = 0; foreach (var prime in primeCollection) { var m = prime > maxBound ? 1 : 0; if (prime <= maxBound) { var q = n; while (q >= prime) { m += q /= prime; } } this.primeList[count] = prime; this.multiList[count++] = m; } return(count); }
public XInt Factorial(int n) { if (n < 20) { return XMath.Factorial(n); } this.sieve = new PrimeSieve(n); var log2N = XMath.FloorLog2(n); var source = new int[log2N]; int h = 0, shift = 0, length = 0; // -- It is more efficient to add the big intervals // -- first and the small ones later! Is it? while (h != n) { shift += h; h = n >> log2N--; if (h > 2) { source[length++] = h; } } var results = new XInt[length]; Parallel.For(0, length, currentIndex => results[currentIndex] = this.Swing(source[currentIndex]) ); XInt p = XInt.One, r = XInt.One, rl = XInt.One; for (var i = 0; i < length; i++) { var t = rl * results[i]; p = p * t; rl = r; r = r * p; } return r << shift; }
public object Solve() { var sieve = new PrimeSieve(Limit); return(sieve.GetPrimes().Sum()); }
public XInt Factorial(int n) { if (n < 20) { return XMath.Factorial(n); } var rootN = XMath.FloorSqrt(n); var log2N = XMath.FloorLog2(n); var section = new XInt[log2N + 1]; for (var i = 0; i < section.Length; i++) { section[i] = XInt.One; } var sieve = new PrimeSieve(n); var primes = sieve.GetPrimeCollection(3, rootN); foreach (var prime in primes) { int k = 0, m = 0, q = n; do { m += q /= prime; } while (q >= 1); while (m > 0) { if ((m & 1) == 1) { section[k] *= prime; } m = m / 2; k++; } } var j = 2; var low = n; while (low != rootN) { var high = low; low = n / j++; if (low < rootN) { low = rootN; } var primorial = sieve.GetPrimorial(low + 1, high); if (primorial != XInt.One) { int k = 0, m = j - 2; while (m > 0) { if ((m & 1) == 1) { section[k] *= primorial; } m = m / 2; k++; } } } var factorial = section[log2N]; for (var i = log2N - 1; i >= 0; --i) { factorial = XInt.Pow(factorial,2) * section[i]; } var exp2N = n - XMath.BitCount(n); return factorial << exp2N; }
public object Solve() { var sieve = new PrimeSieve(LargeNumber.Sqrt().Ceiling().ToLong()); return(LargeNumber.GetPrimeFactors(sieve).Max()); }
public PrimeExtensionTests() { _sieve = new PrimeSieve(); }
public XInt Factorial(int n) { if (n < 20) { return(XMath.Factorial(n)); } var rootN = XMath.FloorSqrt(n); var log2N = XMath.FloorLog2(n); var section = new XInt[log2N + 1]; for (var i = 0; i < section.Length; i++) { section[i] = XInt.One; } var sieve = new PrimeSieve(n); var primes = sieve.GetPrimeCollection(3, rootN); foreach (var prime in primes) { int k = 0, m = 0, q = n; do { m += q /= prime; } while (q >= 1); while (m > 0) { if ((m & 1) == 1) { section[k] *= prime; } m = m / 2; k++; } } var j = 2; var low = n; while (low != rootN) { var high = low; low = n / j++; if (low < rootN) { low = rootN; } var primorial = sieve.GetPrimorial(low + 1, high); if (primorial != XInt.One) { int k = 0, m = j - 2; while (m > 0) { if ((m & 1) == 1) { section[k] *= primorial; } m = m / 2; k++; } } } var factorial = section[log2N]; for (var i = log2N - 1; i >= 0; --i) { factorial = XInt.Pow(factorial, 2) * section[i]; } var exp2N = n - XMath.BitCount(n); return(factorial << exp2N); }
public void TestFindLargestPrimeFactorOf11() { long testNumber = 11; long result = PrimeSieve.findLargestPrimeFactor(testNumber); }