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);
        }
예제 #2
0
        XInt Swing(int n)
        {
            if (n < 33)
            {
                return(SmallOddSwing[n]);
            }

            var primorial = Task.Factory.StartNew <XInt>(() =>
                                                         this.sieve.GetPrimorial(n / 2 + 1, n));
            var count     = 0;
            var rootN     = XMath.FloorSqrt(n);
            var aPrimes   = this.sieve.GetPrimeCollection(3, rootN);
            var bPrimes   = this.sieve.GetPrimeCollection(rootN + 1, n / 3);
            var piN       = aPrimes.NumberOfPrimes + bPrimes.NumberOfPrimes;
            var primeList = new int[piN];

            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);
        }
        private XInt Swing(int n)
        {
            if (n < 33)
            {
                return(SmallOddSwing[n]);
            }

            int count = 0, rootN = XMath.FloorSqrt(n);

            var aPrimes = this.sieve.GetPrimeCollection(3, rootN);
            var bPrimes = this.sieve.GetPrimeCollection(rootN + 1, n / 3);

            foreach (var prime in aPrimes)
            {
                int q = n, p = 1;

                while ((q /= prime) > 0)
                {
                    if ((q & 1) == 1)
                    {
                        p *= prime;
                    }
                }

                if (p > 1)
                {
                    this.primeList[count++] = p;
                }
            }

            foreach (var prime in bPrimes.Where(prime => ((n / prime) & 1) == 1))
            {
                this.primeList[count++] = prime;
            }

            var primorial = this.sieve.GetPrimorial(n / 2 + 1, n);

            return(primorial * XMath.Product(this.primeList, 0, count));
        }
        private XInt NestedSquare(int len)
        {
            if (len == 0)
            {
                return(XInt.One);
            }

            var i    = 0;
            var mult = this.multiList[0];

            while (mult > 1)
            {
                if ((mult & 1) == 1)  // is mult odd ?
                {
                    this.primeList[len++] = this.primeList[i];
                }

                this.multiList[i++] = mult >> 1;
                mult = this.multiList[i];
            }

            return(XMath.Product(this.primeList, i, len - i)
                   * XInt.Pow(this.NestedSquare(i), 2));
        }