Пример #1
0
        public long Solve()
        {
            var primes = new Prime((int)DivisoNumbers);

            var sortedPrimes = new SortedDictionary<long, PrimeDivisors>();
            foreach (var prime in primes.PrimeList)
            {
                sortedPrimes.Add(prime, new PrimeDivisors(prime));
            }

            while (true)
            {
                var minkey = sortedPrimes.Keys.First();
                var maxKey = sortedPrimes.Keys.Last();

                var minValue = sortedPrimes[minkey];

                var newKey = minkey * minkey;
                if (newKey > maxKey)
                {
                    break;
                }
                sortedPrimes.Add(newKey, minValue.UpdatePower());

                sortedPrimes.Remove(minkey);
                sortedPrimes.Remove(maxKey);
            }

            return sortedPrimes.Select(
                primeDivisorse => (long)Math.Pow(primeDivisorse.Value.Prime, primeDivisorse.Value.Power) % Modulo
            ).Aggregate(
                1L, (current, coefficient) => (current * coefficient) % Modulo
            );
        }
Пример #2
0
        private const long Limit = 30000; // jen odhad nemam to nijak podlozeny

        #endregion Fields

        #region Methods

        public long Solve()
        {
            var primes = new Prime(Limit).PrimeList;
            var result = long.MaxValue;

            var primePairs = this.MakePrimePairs(primes);

            foreach (var primePair in primePairs)
            {
                var prime1 = primePair.Key;
                foreach (var prime2 in primePair.Value.Where(t => t >= prime1))
                {
                    var posibilitiesForPrime3 = primePairs[prime2].Where(t => t > prime2 && primePairs[prime1].Contains(t)).ToArray();
                    foreach (var prime3 in posibilitiesForPrime3)
                    {
                        var posibilitiesForPrime4 = primePairs[prime3].Where(t => t > prime3 && posibilitiesForPrime3.Contains(t)).ToArray();
                        foreach (var prime4 in posibilitiesForPrime4)
                        {
                            var posibilitiesForPrime5 = primePairs[prime4].Where(t => t > prime4 && posibilitiesForPrime4.Contains(t)).ToArray();
                            foreach (var prime5 in posibilitiesForPrime5)
                            {
                                var tempResult = prime1 + prime2 + prime3 + prime4 + prime5;
                                if (tempResult < result)
                                    result = tempResult;
                            }
                        }
                    }
                }
            }

            return result;
        }
Пример #3
0
        public void TestAddPrime()
        {
            List<User> lst = clientService.SearchUser("del");

            if (lst.Count >= 1)
            {
                User me = lst[0];

                Prime prime = new Prime()
                {
                    User_id = me.Id,
                    Price = 2000,
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now,
                    Label = "Yolo prime"
                };

                Assert.IsTrue(clientService.addPrime(me.Id,prime));

                lst = clientService.SearchUser("del");
                me = lst[0];

                int nbPrime = clientService.GetPrimesByUserId(me.Id).Count;

                Assert.AreEqual(me.Primes.Count,nbPrime);
            }
        }
Пример #4
0
        public long Solve()
        {
            this.minPrimes = new Dictionary<string, MinPrime>();

            var primes = new Prime(1000000L).PrimeList;
            Console.WriteLine(primes.Count);

            foreach (var prime in primes)
            {
                var keys = this.GetPrimeKeys(prime.ToString(CultureInfo.InvariantCulture));

                foreach (var key in keys.Distinct())
                {
                    if (!this.minPrimes.ContainsKey(key))
                    {
                        this.minPrimes.Add(key, new MinPrime(prime));
                    }

                    this.minPrimes[key].AddPrime();
                    if (this.minPrimes[key].Count == 8)
                    {
                        return this.minPrimes[key].Number;
                    }
                }
            }

            return 0;
        }
Пример #5
0
        static void Main(string[] args)
        {
            var prime = new Prime();
            var primes = prime.Primes();

            Console.WriteLine("{0}", 2000000.DownTo(2).Where(x => prime.IsPrime(x)).Aggregate((System.Numerics.BigInteger)0, (x, y) => x + y));
            Console.ReadKey();
        }
Пример #6
0
        public long Solve()
        {
            var primes = new Prime((long)Math.Sqrt(Limit) + 1).PrimeList;

            var squerePrimes = new List<long>();
            var cubePrimes = new List<long>();
            var fourthPrimes = new List<long>();

            var cubeLimit = false;
            var fourthLimit = false;
            foreach (var prime in primes)
            {
                var squere = prime * prime;
                squerePrimes.Add(squere);

                if (!cubeLimit)
                {
                    var cube = squere * prime;

                    if (cube > Limit)
                    {
                        cubeLimit = true;
                        continue;
                    }
                    cubePrimes.Add(cube);
                }

                if (!fourthLimit)
                {
                    var fourth = squere * squere;
                    if (fourth > Limit)
                    {
                        fourthLimit = true;
                        continue;
                    }
                    fourthPrimes.Add(fourth);
                }
            }

            Console.WriteLine(squerePrimes.Count);
            Console.WriteLine(cubePrimes.Count);
            Console.WriteLine(fourthPrimes.Count);

            var numbers = new HashSet<long>();

            foreach (var cubePrime in cubePrimes)
            {
                foreach (var squerePrime in squerePrimes)
                {
                    foreach (var fourthPrime in fourthPrimes)
                    {
                        numbers.Add(cubePrime + squerePrime + fourthPrime);
                    }
                }
            }

            return numbers.Count(t => t < Limit);
        }
Пример #7
0
 public static void Test(Prime primes)
 {
     Stopwatch primesStopwatch = new Stopwatch();
     primesStopwatch.Start();
     primes.CheckPrimes();
     primesStopwatch.Stop();
     Type type = typeof (Prime);
     Console.WriteLine($"{type} completed in {primesStopwatch.ElapsedMilliseconds} Milliseconds");
 }
Пример #8
0
        static void Main(string[] args)
        {
            long x = 600851475143;

            var prime = new Prime();

            Console.WriteLine(((long)Math.Floor(Math.Sqrt(x))).DownTo(3).AsParallel().Where(prime.IsPrime).First(y => x % y == 0));

            Console.ReadKey();
        }
Пример #9
0
        public long Solve()
        {
            var primeUtilis = new Prime(Limit);
            var primeSum = primeUtilis.PrimeList.Where(t => t < Limit).Sum();
            var array = new long[primeSum + 1];
            var array2 = new Dictionary<long, long> { { 0, 1 } };
            array[0] = 1;

            var sp = 0L;
            foreach (var prime in primeUtilis.PrimeList.Where(t => t < Limit))
            {
                sp += prime;
                for (var i = sp; i > prime - 1; i--)
                {
                    array[i] = (array[i] + array[i - prime]) % Modulo;
                }

                foreach (var oldItem in array2.OrderByDescending(t => t.Key))
                {
                    var newKey = oldItem.Key + prime;
                    if (!array2.ContainsKey(newKey))
                    {
                        array2.Add(newKey, 0);
                    }

                    array2[newKey] = (array2[newKey] +  oldItem.Value) % Modulo;
                }
            }

            var primeUtils2 = new Prime(sp);

            var sum = 0L;
            for (var i = 0; i <= sp; i++)
            {
                if (primeUtils2.IsPrime(i))
                {
                    sum = (sum + array[i]) % Modulo;
                }
            }

            var sum2 = 0L;
            foreach (var a in array2)
            {
                if (primeUtils2.IsPrime(a.Key))
                {
                    sum2 = (sum2 + a.Value)%Modulo;
                }
            }

            Console.WriteLine(sum);
            Console.WriteLine(sum2);

            return sum;
        }
Пример #10
0
        public long Solve()
        {
            var primes = new Prime(Limit);

            var numberDivisors = new Dictionary<int, int>();
            for (var number = 1; number <= Limit; number++)
            {
                var decomposition = primes.GetDecomposition(number);

                var sum = 1;
                foreach (var divisor in decomposition)
                {
                    sum *= this.GetSumDivisors(divisor);
                }
                sum -= number;

                if (sum < Limit && sum != 1)
                {
                    numberDivisors.Add(number, sum);
                }
            }

            var lines = new Dictionary<long, long>();
            foreach (var numberDivisor in numberDivisors)
            {
                var minValueStart = numberDivisor.Key;
                var nextValue = numberDivisor.Value;
                var group = new HashSet<int> { minValueStart };
                var currentCount = 1;

                while (minValueStart != nextValue && nextValue > minValueStart && currentCount == group.Count)
                {
                    if (numberDivisors.ContainsKey(nextValue))
                    {
                        group.Add(nextValue);
                        nextValue = numberDivisors[nextValue];
                        currentCount++;
                    }
                    else
                    {
                        nextValue = 0;
                    }
                }

                if (nextValue == minValueStart && group.Count > 1)
                {
                    lines.Add(minValueStart, group.Count);
                }
            }

            return lines.Where(t => t.Value == lines.Max(r => r.Value)).Min(t => t.Key);
        }
Пример #11
0
        // 100 ->  2,549
        // 1000 -> 314,034
        // EM(10^5) = 3717852515
        // EM(10^6) = 386778427463
        public long Solve()
        {
            Console.WriteLine("start");
            var primes = new Prime(Limit);

            Console.WriteLine("PrimeList Done");

            var numberAll = new SortedDictionary<long, NumberInfo>
            {
                {1, new NumberInfo(1).Add(1, 1) }
            };

            foreach (var prime in primes.PrimeList.Where(t => t <= Limit))
            {
                numberAll.Add(prime, new NumberInfo(prime).Add(prime, 1));

                for (var n = prime + prime; n <= Limit; n = n + prime)
                {
                    var number = n;
                    var exp = 0;
                    while (number % prime == 0)
                    {
                        exp++;
                        number /= prime;
                    }

                    if (!numberAll.ContainsKey(n))
                    {
                        numberAll.Add(n, new NumberInfo(n).Add(prime, exp));
                    }
                    else
                    {
                        numberAll[n].Add(prime, exp);
                    }
                }
            }

            Console.WriteLine("Decomposition done");

            var sum = 0L;
            foreach (var number in numberAll)
            {
                if (number.Key % 10000 == 0)
                {
                    Console.WriteLine(number.Key);
                }

                sum += ComputeMaximum(number.Key, numberAll);
            }

            return sum;
        }
Пример #12
0
        private const long Limit = 7654321L; // 87654321 je delitelne 3

        #endregion Fields

        #region Methods

        public long Solve()
        {
            var primes = new Prime(Limit);

            var currentMaxPandigital = 2134L;
            foreach (var prime in primes.PrimeList)
            {
                if (this.IsPandigital(prime))
                {
                    currentMaxPandigital = currentMaxPandigital < prime ? prime : currentMaxPandigital;
                }
            }

            return currentMaxPandigital;
        }
Пример #13
0
        public bool addPrime(long idUser, Prime prime)
        {
            User user = Entities.Users.First(i => i.Id == idUser);

            if (user == null)
                return false;

            if (prime == null)
                return false;

            prime.User_id = idUser;

            Entities.Primes.Add(prime);
            Entities.SaveChanges();

            return true;
        }
Пример #14
0
        public long Solve()
        {
            var primes = new Prime(Limit).PrimeList;
            var sum = 0L;

            foreach (var prime in primes)
            {
                if (prime < 5)
                    continue;

                if (prime > Limit)
                    break;

                sum += this.PrimeKFactorial(prime);
            }

            return sum;
        }
Пример #15
0
        public long Solve()
        {
            var prime = new Prime((long)1000000);

            for (var number = 2; number <= 1000000; number++)
            {
                var totient = prime.Totient(number);

                if (number % 10000 == 0)
                {
                    Console.WriteLine("{0} - {1} - {2}", number, totient, (decimal)totient / (number - 1));
                }

                if ((decimal)totient / (number - 1) < Limit)
                    return number;
            }

            return 0;
        }
Пример #16
0
        /// <summary>
        ///     The prime factors of 13195 are 5, 7, 13 and 29.
        ///     Calculate the largest Prime of 600851475143
        /// </summary>
        /// <returns></returns>
        public int Problem3()
        {
            var number = 600851475143;
            //long number = 13195;

            var limit = (int) Math.Sqrt(number);
            var p = new Prime();
            var primes = p.SieveOfEratosthenes(limit).ToList();
            var max = 0;
            for (var i = primes.Count() - 1; i > 0; i--)
            {
                if (number%primes[i] == 0)
                {
                    //Console.WriteLine("{0} is prime", primes[i]);
                    max = primes[i];
                    break;
                }
            }

            return max;
        }
Пример #17
0
        public long Solve()
        {
            var primeUtil = new Prime(Limit);
            var numbers = new HashSet<long>();

            foreach (var prime in primeUtil.PrimeList)
            {
                if (prime < Limit)
                    numbers.Add(prime + 1);
            }

            Console.WriteLine(numbers.Count);
            Console.WriteLine(numbers.Max());

            var factorization = new Dictionary<long, Dictionary<long, long>>();
            foreach (var number in numbers)
            {
                factorization.Add(number, primeUtil.GetDecomposition(number));
            }

            Console.WriteLine("Faktorizace Done");

            return 0;
        }
Пример #18
0
 public Problem58()
 {
     Prime.Initialize(TEN_THOUSAND);
 }
Пример #19
0
        public void NumberConstructor_CreateInstanceOfANumber_Number()
        {
            Prime newNumber = new Prime(20);

            Assert.AreEqual(typeof(Prime), newNumber.GetType());
        }
Пример #20
0
 public void IsPrimeParam(int candidate)
 {
     Assert.IsTrue(Prime.IsPrime(candidate), candidate + " Should be Prime!");
 }
Пример #21
0
 /// <inheritdoc/>
 public override string ToString() => $"SrpParameters.Create<{Hasher.AlgorithmName}>(\"{Prime.ToHex()}\", \"{Generator.ToHex()}\")";
Пример #22
0
 public void PrimeStringLooksGoodData(int candidate, string hcAnswer)
 {
     //Assert.AreEqual("Primes found: 7, 5, 3, 2, 1", Prime.ListPrimesLTEQ(candidate));
     Assert.AreEqual(hcAnswer, Prime.ListPrimesLTEQ(candidate));
 }
Пример #23
0
        public void ShuffleCountPrimeTest()
        {
            long shuffles = 101741582076661;

            Assert.True(Prime.IsPrime(shuffles));
        }
Пример #24
0
 /// <summary> Clear all scenes off the stack (if any) and push a new scene. </summary>
 public static void Set(string filepath)
 {
     Set(Prime.GetNewInstance <PrimeScene>(filepath));
 }
Пример #25
0
        public void PrimeTestPrimeCanBeInstantiated()
        {
            Prime test = new Prime();

            Assert.IsNotNull(test);
        }
Пример #26
0
 public void IsPrime()
 {
     //Assert.Fail();
     Assert.IsTrue(Prime.IsPrime(3), "3 should be prime");
 }
Пример #27
0
    private static void test02()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests FAURE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    23 January 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int DIM_NUM = 3;

        int dim;
        int i;

        double[] r = new double[DIM_NUM];
        int      seed_in;
        int      seed_out;

        Console.WriteLine("");
        Console.WriteLine("TEST02");
        Console.WriteLine("  FAURE computes the next element of a Faure sequence.");
        Console.WriteLine("");
        Console.WriteLine("  In this test, we demonstrate how the SEED can be");
        Console.WriteLine("  manipulated to skip ahead in the sequence, or");
        Console.WriteLine("  to come back to any part of the sequence.");

        int qs = Prime.prime_ge(DIM_NUM);

        Console.WriteLine("");
        Console.WriteLine("  Using dimension DIM_NUM =   " + DIM_NUM + "");
        Console.WriteLine("  The underlying base is QS = " + qs + "");

        Console.WriteLine("");
        Console.WriteLine("  Note that on the first call to FAURE, if");
        Console.WriteLine("  SEED is negative, it is reset to a value that");
        Console.WriteLine("  is the recommended starting point:");

        int seed = -1;

        Console.WriteLine("");
        Console.WriteLine("  Seed  Seed   Faure");
        Console.WriteLine("  In    Out");
        Console.WriteLine("");

        FaureData data = new();

        for (i = 1; i <= 5; i++)
        {
            seed_in = seed;
            Faure.faure(ref data, DIM_NUM, ref seed, ref r);
            seed_out = seed;
            string cout = seed_in.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  "
                          + seed_out.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  ";
            for (dim = 0; dim < DIM_NUM; dim++)
            {
                cout += r[dim].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "  ";
            }

            Console.WriteLine(cout);
        }

        Console.WriteLine("");
        Console.WriteLine("  However, if the input value of SEED is 0,");
        Console.WriteLine("  then no initial skipping is done.");

        seed = 0;

        Console.WriteLine("");
        Console.WriteLine("  Seed  Seed   Faure");
        Console.WriteLine("  In    Out");
        Console.WriteLine("");

        for (i = 1; i <= 10; i++)
        {
            seed_in = seed;
            Faure.faure(ref data, DIM_NUM, ref seed, ref r);
            seed_out = seed;
            string cout = seed_in.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  "
                          + seed_out.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  ";
            for (dim = 0; dim < DIM_NUM; dim++)
            {
                cout += r[dim].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "  ";
            }

            Console.WriteLine(cout);
        }

        Console.WriteLine("");
        Console.WriteLine("  Jump ahead by increasing SEED:");
        Console.WriteLine("");

        seed = 100;

        Console.WriteLine("");
        Console.WriteLine("  Seed  Seed   Faure");
        Console.WriteLine("  In    Out");
        Console.WriteLine("");

        for (i = 1; i <= 5; i++)
        {
            seed_in = seed;
            Faure.faure(ref data, DIM_NUM, ref seed, ref r);
            seed_out = seed;
            string cout = seed_in.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  "
                          + seed_out.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  ";
            for (dim = 0; dim < DIM_NUM; dim++)
            {
                cout += r[dim].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "  ";
            }

            Console.WriteLine(cout);
        }

        Console.WriteLine("");
        Console.WriteLine("  Jump back by decreasing SEED:");
        Console.WriteLine("");

        seed = 3;

        Console.WriteLine("");
        Console.WriteLine("  Seed  Seed   Faure");
        Console.WriteLine("  In    Out");
        Console.WriteLine("");

        for (i = 1; i <= 10; i++)
        {
            seed_in = seed;
            Faure.faure(ref data, DIM_NUM, ref seed, ref r);
            seed_out = seed;
            string cout = seed_in.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  "
                          + seed_out.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  ";
            for (dim = 0; dim < DIM_NUM; dim++)
            {
                cout += r[dim].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "  ";
            }

            Console.WriteLine(cout);
        }
    }
Пример #28
0
    private static void test03()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST03 tests FAURE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    04 June 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int dim_base = 10;
        int       dim_num;

        Console.WriteLine("");
        Console.WriteLine("TEST03");
        Console.WriteLine("  FAURE computes the next element of a Faure sequence.");
        Console.WriteLine("");
        Console.WriteLine("  In this test, we try some large dimensions.");

        FaureData data = new();

        for (dim_num = dim_base; dim_num <= 6 * dim_base; dim_num += dim_base)
        {
            double[] r = new double[dim_num];

            int seed = -1;
            int qs   = Prime.prime_ge(dim_num);

            Console.WriteLine("");
            Console.WriteLine("  Using dimension DIM_NUM =   " + dim_num + "");
            Console.WriteLine("  The underlying base is QS = " + qs + "");
            Console.WriteLine("");
            Console.WriteLine("  Seed  Seed   Faure");
            Console.WriteLine("  In    Out");
            Console.WriteLine("");

            int i;
            for (i = 1; i <= 2; i++)
            {
                int seed_in = seed;
                Faure.faure(ref data, dim_num, ref seed, ref r);
                int seed_out = seed;
                Console.WriteLine("  " + seed_in.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                                  + "  " + seed_out.ToString(CultureInfo.InvariantCulture).PadLeft(8) + "");
                string cout = "                    ";
                int    dim;
                for (dim = 0; dim < dim_num; dim++)
                {
                    cout += "  " + r[dim].ToString(CultureInfo.InvariantCulture).PadLeft(10);
                    if ((dim + 1) % 5 == 0 || dim + 1 == dim_num)
                    {
                        Console.WriteLine(cout);
                    }

                    switch ((dim + 1) % 5)
                    {
                    case 0 when dim + 1 < dim_num:
                        cout += "                    ";
                        break;
                    }
                }
            }
        }
    }
Пример #29
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests FAURE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    23 January 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int DIM_MAX = 4;

        int dim_num;

        double[] r = new double[DIM_MAX];

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  FAURE computes the next element of a Faure sequence.");
        Console.WriteLine("");
        Console.WriteLine("  In this test, we call FAURE repeatedly.");

        FaureData data = new();

        for (dim_num = 2; dim_num <= DIM_MAX; dim_num++)
        {
            int seed = -1;
            int qs   = Prime.prime_ge(dim_num);

            Console.WriteLine("");
            Console.WriteLine("  Using dimension DIM_NUM =   " + dim_num + "");
            Console.WriteLine("  The underlying base is QS = " + qs + "");
            Console.WriteLine("");
            Console.WriteLine("  Seed  Seed   Faure");
            Console.WriteLine("  In    Out");
            Console.WriteLine("");

            int i;
            for (i = 1; i <= 10; i++)
            {
                int seed_in = seed;
                Faure.faure(ref data, dim_num, ref seed, ref r);
                int    seed_out = seed;
                string cout     = seed_in.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  "
                                  + seed_out.ToString(CultureInfo.InvariantCulture).PadLeft(6) + "  ";
                int dim;
                for (dim = 0; dim < dim_num; dim++)
                {
                    cout += r[dim].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "  ";
                }

                Console.WriteLine(cout);
            }
        }
    }
Пример #30
0
        private static bool CopySlot(ref Entry oldEntry, DictionaryImpl <TKey, TKeyStore, TValue> newTable)
        {
            Debug.Assert(newTable != null);

            // Blindly set the hash from 0 to TOMBPRIMEHASH, to eagerly stop
            // fresh put's from claiming new slots in the old table when the old
            // table is mid-resize.
            var hash = oldEntry.hash;

            if (hash == 0)
            {
                hash = Interlocked.CompareExchange(ref oldEntry.hash, TOMBPRIMEHASH, 0);
                if (hash == 0)
                {
                    // slot was not claimed, copy is done here
                    return(true);
                }
            }

            if (hash == TOMBPRIMEHASH)
            {
                // slot was trivially copied, but not by us
                return(false);
            }

            // Prevent new values from appearing in the old table.
            // Box what we see in the old table, to prevent further updates.
            // NOTE: Read of the value below must happen before reading of the key,
            // however this read does not need to be volatile since we will have
            // some fences in between reads.
            object oldval = oldEntry.value;

            // already boxed?
            Prime box = oldval as Prime;

            if (box != null)
            {
                // volatile read here since we need to make sure
                // that the key read below happens after we have read oldval above
                Volatile.Read(ref box.originalValue);
            }
            else
            {
                do
                {
                    box = EntryValueNullOrDead(oldval) ?
                          TOMBPRIME :
                          new Prime(oldval);

                    // CAS down a box'd version of oldval
                    // also works as a complete fence between reading the value and the key
                    object prev = Interlocked.CompareExchange(ref oldEntry.value, box, oldval);

                    if (prev == oldval)
                    {
                        // If we made the Value slot hold a TOMBPRIME, then we both
                        // prevented further updates here but also the (absent)
                        // oldval is vacuously available in the new table.  We
                        // return with true here: any thread looking for a value for
                        // this key can correctly go straight to the new table and
                        // skip looking in the old table.
                        if (box == TOMBPRIME)
                        {
                            return(true);
                        }

                        // Break loop; oldval is now boxed by us
                        // it still needs to be copied into the new table.
                        break;
                    }

                    oldval = prev;
                    box    = oldval as Prime;
                }while (box == null);
            }

            if (box == TOMBPRIME)
            {
                // Copy already complete here, but not by us.
                return(false);
            }

            // Copy the value into the new table, but only if we overwrite a null.
            // If another value is already in the new table, then somebody else
            // wrote something there and that write is happens-after any value that
            // appears in the old table.  If putIfMatch does not find a null in the
            // new table - somebody else should have recorded the null-not_null
            // transition in this copy.
            object originalValue = box.originalValue;

            Debug.Assert(originalValue != TOMBSTONE);

            // since we have a real value, there must be a nontrivial key in the table
            // regular read is ok because value is always CASed down after the key
            // and we ensured that we read the key after the value with fences above
            var  key           = oldEntry.key;
            bool copiedIntoNew = newTable.PutSlotCopy(key, originalValue, hash);

            // Finally, now that any old value is exposed in the new table, we can
            // forever hide the old-table value by gently inserting TOMBPRIME value.
            // This will stop other threads from uselessly attempting to copy this slot
            // (i.e., it's a speed optimization not a correctness issue).
            // Check if we are not too late though, to not pay for MESI RFO and
            // GC fence needlessly.
            if (oldEntry.value != TOMBPRIME)
            {
                oldEntry.value = TOMBPRIME;
            }

            // if we failed to copy, it means something has already appeared in
            // the new table and old value should have been copied before that (not by us).
            return(copiedIntoNew);
        }
Пример #31
0
 public void IsPrime()
 {
     Assert.IsTrue(Prime.IsPrime(3), "3 should be prime");
 }
Пример #32
0
 public void IsSquareTest(int candidate, bool myanswer)
 {
     Assert.AreEqual(Prime.IsSquare(candidate), myanswer);
 }
Пример #33
0
 public void TestPrimes4()
 {
     Assert.AreEqual(false, Prime.IsPrime(4));
 }
Пример #34
0
        public void DeckCountPrimeTest()
        {
            long deckCount = 119315717514047;

            Assert.True(Prime.IsPrime(deckCount));
        }
Пример #35
0
 public void TestPrimesSum6()
 {
     Assert.AreEqual(10, Prime.IsPrimeSum(6));
 }
Пример #36
0
 /// <summary> Push a new scene onto the stack. </summary>
 public static void Push(string filepath, bool hideSceneBelow = true)
 {
     Push(Prime.GetNewInstance <PrimeScene>(filepath), hideSceneBelow);
 }
Пример #37
0
 public bool addPrime(long idUser, Prime prime)
 {
     return salaireDAL.addPrime(idUser, prime);
 }
Пример #38
0
 public bool addPrime(long idUser, Prime prime)
 {
     return(salaireDAL.addPrime(idUser, prime));
 }
Пример #39
0
 public void IsPrime_TrueForLargePrime( )
 {
     Assert.IsTrue(Prime.IsPrime(9876103));
 }
Пример #40
0
 private void OnQuitButtonPressed()
 {
     Prime.Quit();
 }
Пример #41
0
 public void IsPrime_FalseForLargeNonPrime( )
 {
     Assert.IsFalse(Prime.IsPrime(3447));
 }
Пример #42
0
 public void Dispose()
 {
     Prime.ClearAll();
 }
Пример #43
0
 public override void _Ready()
 {
     Value = Prime.GetRandomFloat(MinFloat, MaxFloat);
 }
Пример #44
0
 public void TestPrimesSum8()
 {
     Assert.AreEqual(17, Prime.IsPrimeSum(8));
 }
Пример #45
0
        //By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
        //What is the 10 001st prime number?
        public int Problem7()
        {
            var limit = 120000;
            var p = new Prime();
            var primes = p.SieveOfEratosthenes(limit).ToList();
            //var util = new Utility();
            //util.WriteEnumerable(primes);

            var prime1001 = primes[10000];
            return prime1001;
        }
Пример #46
0
 public void TestPrimes17()
 {
     Assert.AreEqual(true, Prime.IsPrime(17));
 }
Пример #47
0
 public SimplifyForm()
 {
     Instances++;
     InitializeComponent();
     Prime.InitializePrimes();
 }
Пример #48
0
        private IEnumerable<long> GetHammingPrimes(IEnumerable<long> hammingNumbers)
        {
            var primeUtils = new Prime((long)Math.Ceiling(Math.Sqrt(Limit)) + 100);

            foreach (var hammingNumber in hammingNumbers)
            {
                if (primeUtils.IsPrimeCompute(hammingNumber + 1) && hammingNumber < Limit && hammingNumber > 5)
                {
                    yield return hammingNumber + 1;
                }
            }
        }
Пример #49
0
 public void Save(Prime prime)
 {
     _primes.InsertOne(prime);
 }
Пример #50
0
 public static void Main(string[] args)
 {
     Prime primeNumbers1 = new Prime(2000000);
     for (int i = 0; i < 10; i++)
         Console.WriteLine(primeNumbers1.next());
 }
Пример #51
0
        private MathList?BuildInternal(bool oneCharOnly, char stopChar = '\0', MathList?r = null)
        {
            if (oneCharOnly && stopChar > '\0')
            {
                throw new InvalidCodePathException("Cannot set both oneCharOnly and stopChar");
            }
            r ??= new MathList();
            MathAtom?prevAtom = null;

            while (HasCharacters)
            {
                if (Error != null)
                {
                    return(null);
                }
                MathAtom atom;
                switch (GetNextCharacter())
                {
                case var ch when oneCharOnly && (ch == '^' || ch == '}' || ch == '_' || ch == '&'):
                    SetError($"{ch} cannot appear as an argument to a command");
                    return(r);

                case var ch when stopChar > '\0' && ch == stopChar:
                    return(r);

                case '^':
                    if (prevAtom == null || prevAtom.Superscript.IsNonEmpty() || !prevAtom.ScriptsAllowed)
                    {
                        prevAtom = new Ordinary(string.Empty);
                        r.Add(prevAtom);
                    }
                    // this is a superscript for the previous atom.
                    // note, if the next char is StopChar, it will be consumed and doesn't count as stop.
                    this.BuildInternal(true, r: prevAtom.Superscript);
                    if (Error != null)
                    {
                        return(null);
                    }
                    continue;

                case '_':
                    if (prevAtom == null || prevAtom.Subscript.IsNonEmpty() || !prevAtom.ScriptsAllowed)
                    {
                        prevAtom = new Ordinary(string.Empty);
                        r.Add(prevAtom);
                    }
                    // this is a subscript for the previous atom.
                    // note, if the next char is StopChar, it will be consumed and doesn't count as stop.
                    this.BuildInternal(true, r: prevAtom.Subscript);
                    if (Error != null)
                    {
                        return(null);
                    }
                    continue;

                case '{':
                    MathList?sublist;
                    if (_currentEnvironment != null && _currentEnvironment.Name == null)
                    {
                        // \\ or \cr which do not have a corrosponding \end
                        var oldEnv = _currentEnvironment;
                        _currentEnvironment = null;
                        sublist             = BuildInternal(false, '}');
                        _currentEnvironment = oldEnv;
                    }
                    else
                    {
                        sublist = BuildInternal(false, '}');
                    }
                    if (sublist == null)
                    {
                        return(null);
                    }
                    prevAtom = sublist.Atoms.LastOrDefault();
                    r.Append(sublist);
                    if (oneCharOnly)
                    {
                        return(r);
                    }
                    continue;

#warning TODO Example
                //https://phabricator.wikimedia.org/T99369
                //https://phab.wmfusercontent.org/file/data/xsimlcnvo42siudvwuzk/PHID-FILE-bdcqexocj5b57tj2oezn/math_rendering.png
                //dt, \text{d}t, \partial t, \nabla\psi \\ \underline\overline{dy/dx, \text{d}y/\text{d}x, \frac{dy}{dx}, \frac{\text{d}y}{\text{d}x}, \frac{\partial^2}{\partial x_1\partial x_2}y} \\ \prime,
                case '}' when oneCharOnly || stopChar != 0:
                    throw new InvalidCodePathException("This should have been handled before.");

                case '}':
                    SetError("Missing opening brace");
                    return(null);

                case '\\':
                    var command = ReadCommand();
                    var done    = StopCommand(command, r, stopChar);
                    if (done != null)
                    {
                        return(done);
                    }
                    if (Error != null)
                    {
                        return(null);
                    }
                    if (ApplyModifier(command, prevAtom))
                    {
                        continue;
                    }
                    if (LaTeXSettings.FontStyles.TryGetValue(command, out var fontStyle))
                    {
                        var oldSpacesAllowed = _textMode;
                        var oldFontStyle     = _currentFontStyle;
                        _textMode         = (command == "text");
                        _currentFontStyle = fontStyle;
                        var childList = BuildInternal(true);
                        if (childList == null)
                        {
                            return(null);
                        }
                        _currentFontStyle = oldFontStyle;
                        _textMode         = oldSpacesAllowed;
                        prevAtom          = childList.Atoms.LastOrDefault();
                        r.Append(childList);
                        if (oneCharOnly)
                        {
                            return(r);
                        }
                        continue;
                    }
                    switch (AtomForCommand(command, stopChar))
                    {
                    case null:
                        SetError(Error ?? "Internal error");
                        return(null);

                    case var a:
                        atom = a;
                        break;
                    }
                    break;

                case '&': // column separation in tables
                    if (_currentEnvironment != null)
                    {
                        return(r);
                    }
                    var table = BuildTable(null, r, false, stopChar);
                    if (table == null)
                    {
                        return(null);
                    }
                    return(new MathList(table));

                case '\'': // this case is NOT in iosMath
                    int i = 1;
                    while (ExpectCharacter('\''))
                    {
                        i++;
                    }
                    atom = new Prime(i);
                    break;

                case ' ' when _textMode:
                    atom = new Ordinary(" ");
                    break;

                case var ch when ch <= sbyte.MaxValue:
                    if (LaTeXSettings.ForAscii((sbyte)ch) is MathAtom asciiAtom)
                    {
                        atom = asciiAtom;
                    }
                    else
                    {
                        continue; // Ignore ASCII spaces and control characters
                    }
                    break;

                case var ch:
                    // not a recognized character, display it directly
                    atom = new Ordinary(ch.ToStringInvariant());
                    break;
                }
                atom.FontStyle = _currentFontStyle;
                r.Add(atom);
                prevAtom = atom;
                if (oneCharOnly)
                {
                    return(r); // we consumed our character.
                }
            }
            if (stopChar > 0)
            {
                if (stopChar == '}')
                {
                    SetError("Missing closing brace");
                }
                else
                {
                    // we never found our stop character.
                    SetError("Expected character not found: " + stopChar.ToStringInvariant());
                }
            }
            return(r);
        }