Example #1
0
        public void Test8()
        {
            PrimeNumbers PrimNum = new PrimeNumbers();
            bool         output  = PrimNum.IsAPrime(64);

            Assert.Equal(output.ToString(), IsPrime.False.ToString());
        }
        //Prime Numbers
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            ITrack track = new PrimeNumbers();

            race.track = track;
            this.Frame.Navigate(typeof(MainPage), race);
        }
        private static List <int> GetPrimeFactors(int num, int limit)
        {
            var res = new List <int>();

            foreach (var prime in PrimeNumbers.GetAllPrimes())
            {
                if (prime > num / 2)
                {
                    break;
                }

                var pow  = 0;
                var temp = num;
                while (temp % prime == 0)
                {
                    temp /= prime;
                    pow++;
                }

                if (pow > 0)
                {
                    res.Add((int)(Math.Pow(prime, pow)));
                    if (res.Count >= limit)
                    {
                        break;
                    }
                }
            }

            return(res);
        }
Example #4
0
        public void ShouldReturnPrimesUnder30()
        {
            // Arrange
            var primeNumbers    = new PrimeNumbers();
            var primesReturned  = false;
            var primesGenerated = new List <int>();

            // Primes taken from https://oeis.org/A000040
            var primesUnder30 = new List <int>
            {
                2,
                3,
                5,
                7,
                11,
                13,
                17,
                19,
                23,
                29
            };

            // Act
            foreach (var prime in primeNumbers.Generate(30))
            {
                Assert.IsTrue(primesUnder30.Contains(prime), $"Unexpected prime: {prime}");
                primesGenerated.Add(prime);
                primesReturned = true;
            }

            // Assert
            Assert.IsTrue(primesReturned, "Expected the on prime action to be called.");
            Assert.IsFalse(primesUnder30.Except(primesGenerated).Any(), "Expected all primes under 30 to be generated.");
        }
Example #5
0
        public void Start()
        {
            var primes = PrimeNumbers.GetPrimeSet(10000);
            int maxN   = -1;
            int bMax   = -99999;
            int aMax   = -99999;

            for (int a = -999; a < 1000; a++)
            {
                for (int b = -1000; b <= 1000; b++)
                {
                    int n = 0;
                    var estimatedPrime = n * n + a * n + b;

                    while (primes.Contains(estimatedPrime))
                    {
                        n++;
                        estimatedPrime = n * n + a * n + b;
                    }

                    if (n > maxN)
                    {
                        maxN = n;
                        aMax = a;
                        bMax = b;
                    }
                }
            }

            Trace.WriteLine($"Max n = {maxN}, a = {aMax}, b = {bMax}");
        }
Example #6
0
        public string PutById(int id)
        {
            List <Prime> primes = PrimeNumbers.GetPrimes(id);

            _service.Save(primes);
            return("Ok");
        }
Example #7
0
        public void PrimeFindIndexOfElementFails()
        {
            PrimeNumbers prime = new PrimeNumbers();
            int          input = 103;

            prime.FindIndex(input);
        }
Example #8
0
        private void primeNumber_Click(object sender, RoutedEventArgs e)
        {
            int n = Int32.Parse(inputTextBox.Text);


            resultLabel.Text = PrimeNumbers.FindPrimeNumbersSync(n).ToString();;
        }
Example #9
0
        public void PrimeGetNextNonPrime()
        {
            PrimeNumbers prime = new PrimeNumbers();
            int          input = 105;

            prime.GetNext(input);
        }
Example #10
0
        public void Test1(int userInput, int expected)
        {
            PrimeNumbers pnum = new PrimeNumbers();
            int          res  = PrimeNumbers.GetPrime(userInput);

            Assert.Equal(expected, res);
        }
Example #11
0
        public void GetListofPrimeNumbers_first_prime_number_is_2()
        {
            PrimeNumbers primeNumbers     = new PrimeNumbers();
            var          primeNumbersList = primeNumbers.GetListofPrimeNumbers(100);

            Assert.AreEqual(primeNumbersList.ToList()[0], 2);
        }
Example #12
0
        public void Nine()
        {
            int  a      = 9;
            bool result = PrimeNumbers.CheckPrime(a);

            Assert.False(result);
        }
Example #13
0
        public void GetListofPrimeNumbers_Should_return_not_null()
        {
            PrimeNumbers primeNumbers     = new PrimeNumbers();
            var          primeNumbersList = primeNumbers.GetListofPrimeNumbers(100);

            Assert.IsNotNull(primeNumbersList);
        }
        public void PrimeTest1(int first, int expected)
        {
            PrimeNumbers num    = new PrimeNumbers();
            int          result = num.GetPrime(first);

            Assert.Equal(expected, result);
        }
Example #15
0
 public void PrimeShouldPrintFirstNumber()
 {
     PrimeNumbers prime = new PrimeNumbers();
     int expected = 1;
     int actual = prime.GetFirst();
     Assert.AreEqual(expected, actual);
 }
Example #16
0
        public void Test7()
        {
            PrimeNumbers PrimNum = new PrimeNumbers();
            bool         output  = PrimNum.IsAPrime(89);

            Assert.Equal(output.ToString(), IsPrime.True.ToString());
        }
Example #17
0
        public void Start()
        {
            var primes = PrimeNumbers.GetPrimeSet(10000000);

            foreach (var prime in primes.OrderByDescending(z => z))
            {
                var digits = Numbers.ToDigits(prime);
                Array.Sort(digits);

                bool isPandigital = true;

                for (int i = 0; i < digits.Length; i++)
                {
                    if (i + 1 != digits[i])
                    {
                        isPandigital = false;
                        break;
                    }
                }

                if (isPandigital)
                {
                    Console.WriteLine($"Highest pandigital prime is {prime}");
                    break;
                }
            }
        }
Example #18
0
        public void Three()
        {
            int  a      = 3;
            bool result = PrimeNumbers.CheckPrime(a);

            Assert.True(result);
        }
        public void Test1(int userInput, int expected)
        {
            PrimeNumbers PNums  = new PrimeNumbers();
            int          result = PNums.GetPrime(userInput);

            Assert.Equal(expected, result);
        }
Example #20
0
        public static long Solve()
        {
            //long answer = 2L * 5L * 3L * 7L * 11L * 13L * 17L * 19L * 23L * 2L * 2L;
            double ratio = 15499.0 / 94744.0; //0.1635881955585578
            var primes = new PrimeNumbers();
            long denominator = 1L;
            double r = 1;

            foreach (var prime in primes)
            {
                // 2L * 5L * 3L * 7L * 11L * 13L * 17L * 19L * 23L
                r = (denominator * prime).phi() / ((denominator * prime) - 1.0);
                if (r > ratio)
                    denominator *= prime;
                else
                {
                    r = denominator.phi() / (denominator - 1.0);
                    break;
                }
            }

            while (r > ratio)
            {
                denominator *= primes.First();
                r = denominator.phi() / (denominator - 1.0);
                if (r < ratio)
                    break;
            }

            return denominator;
        }
Example #21
0
        public void TestPrimes(int n, int expected)
        {
            PrimeNumbers numbers = new PrimeNumbers();
            int          result  = numbers.GetPrime(n);

            Assert.Equal(expected, result);
        }
Example #22
0
        public void NintySeven()
        {
            int  a      = 97;
            bool result = PrimeNumbers.CheckPrime(a);

            Assert.True(result);
        }
Example #23
0
        /// <summary>
        /// Performs prime number search
        /// </summary>
        private async void Start()
        {
            using (StartCommand.RunWithDisabled())
                using (_cancellationTokenSource = new CancellationTokenSource())
                {
                    var progress = new ProgressFilterProxy(new Progress <double>(d => Progress = (int)(d * 100)));
                    try
                    {
                        var primes =
                            await _sieveOfEratosthenesCalculator.MakeSieveAsync((int)NumberRange, progress,
                                                                                _cancellationTokenSource.Token);

                        Progress   = 100;
                        ResultText = $"Znaleziono {primes.Count} liczb pierwszych";
                        PrimeNumbers.Clear();
                        PrimeNumbers.AddRange(primes,
                                              ObservableCollectionEx <int> .ECollectionChangeNotificationMode.Reset);
                    }
                    catch (OperationCanceledException ex) when(ex.CancellationToken == _cancellationTokenSource.Token)
                    {
                        ResultText = "Operacja zatrzymana";
                        PrimeNumbers.Clear();
                    }
                    finally
                    {
                        _cancellationTokenSource = null;
                    }
                }
        }
Example #24
0
        public void TestAll(int userNum, int expected)
        {
            PrimeNumbers primeNum = new PrimeNumbers();
            int          final    = primeNum.GetPrime(userNum);

            Assert.Equal(expected, final);
        }
Example #25
0
        public static void Start()
        {
            var circularPrimes = new List <int>();
            var primes         = PrimeNumbers.GetPrimeSet(1000000);

            foreach (var prime in primes)
            {
                var digits = Numbers.ToDigits(prime);

                bool isCircular = true;

                for (int i = 1; i < digits.Length; i++)
                {
                    if (!primes.Contains(Rotate(digits)))
                    {
                        isCircular = false;
                    }
                }

                if (isCircular)
                {
                    circularPrimes.Add(prime);
                }
            }

            Console.WriteLine($"Number of circular primes below one million: {circularPrimes.Count}");
        }
Example #26
0
 public void PrimeShouldPrintNextNumber()
 {
     PrimeNumbers prime = new PrimeNumbers();
     int input = 2;
     int expected = 3;
     int actual = prime.GetNext(input);
     Assert.AreEqual(expected, actual);
 }
Example #27
0
        public void Test5()
        {
            int       choice = 5;
            ArrayList myList = new ArrayList();

            PrimeNumbers.CreatePrimeList(myList);
            Assert.Equal(11, PrimeNumbers.GetPrime(myList, choice));
        }
Example #28
0
 public void PrimeShouldPrintVariableSequenceOfNumbers()
 {
     PrimeNumbers prime = new PrimeNumbers();
     int input = 8;
     int[] expected = new int[] { 1, 2, 3, 5, 7, 11, 13, 17 };
     int[] actual = prime.GetSequence(input);
     CollectionAssert.AreEqual(expected, actual);
 }
Example #29
0
        public void PrimeShouldPrintFirstNumber()
        {
            PrimeNumbers prime    = new PrimeNumbers();
            int          expected = 2;
            int          actual   = prime.GetFirst();

            Assert.AreEqual(expected, actual);
        }
Example #30
0
        public ulong Run(ulong targetNumber)
        {
            var primes = new PrimeNumbers();

            var primeFactors = primes.GetPrimeFactors(targetNumber);

            return(primeFactors.Max());
        }
        public void Execute()
        {
            var primeNumber = new PrimeNumbers();

            var number = primeNumber.RandomNumber(0, 5000);

            primeNumber.PrimeNumberString(number);
        }
Example #32
0
 public void PrimePrintSequenceOfNumbers()
 {
     PrimeNumbers prime = new PrimeNumbers();
     int length = 7;
     string expected = "1 2 3 5 7 11 13";
     string actual = prime.ToString(prime.GetSequence(length));
     Assert.AreEqual(expected, actual);
 }
Example #33
0
 public Rsa(Int64 firstPrime, Int64 secondPrime)
 {
     if (PrimeNumbers == null || !PrimeNumbers.Any())
     {
         GeneratorPrimeNumbers(MinValue, MaxValue);
     }
     GeneralKeys(firstPrime, secondPrime);
 }
Example #34
0
        public void GetListofPrimeNumbers_one_element_basic_test()
        {
            PrimeNumbers primeNumbers     = new PrimeNumbers();
            var          primeNumbersList = primeNumbers.GetListofPrimeNumbers(1);

            Assert.IsNotNull(primeNumbersList);
            Assert.AreEqual(primeNumbersList.ToList()[0], 2);
        }
Example #35
0
        public void GetListofPrimeNumbers_list_count_same_as_input_number()
        {
            PrimeNumbers primeNumbers     = new PrimeNumbers();
            int          count            = 100;
            var          primeNumbersList = primeNumbers.GetListofPrimeNumbers(count);

            Assert.AreEqual(primeNumbersList.Count(), count);
        }
        public int Execute(int numberToFactor1, int numberToFactor2)
        {
            var primeNumbers = new PrimeNumbers().Execute(Min(numberToFactor1, numberToFactor2)).ToArray();

            var factors1 = GetFactors(InitFactorsList(), numberToFactor1, FilterPrimeNumbers(numberToFactor1, primeNumbers).ToArray());
            var factors2 = GetFactors(InitFactorsList(), numberToFactor2, FilterPrimeNumbers(numberToFactor2, primeNumbers).ToArray());

            var factorGroups1 = factors1.GroupBy(o => o).Select(g => new FactorGroup(g.Key, g.Count()));
            var factorGroups2 = factors2.GroupBy(o => o).Select(g => new FactorGroup(g.Key, g.Count()));

            var minFactorGroups = from g1 in factorGroups1
                          join g2 in factorGroups2 on g1.Factor equals g2.Factor
                          select new FactorGroup(g2.Factor, Min(g1.Count, g2.Count));

            return minFactorGroups.Select(Power).Multiply();
        }
Example #37
0
        public static long Solve()
        {
            var longest = 0;
            var count = 0;
            var limit = 1000000;
            var primes = new PrimeNumbers(limit);
            var test = 0;
            var queue = new Queue<int>();

            foreach (var prime in primes)
            {
                test += prime;
                if (test >= limit)
                    break;

                queue.Enqueue(prime);

                if (primes.IsPrime(test) && queue.Count > count)
                {
                    longest = test;
                    count = queue.Count;
                }
            }

            while (!primes.IsPrime(test))
            {
                queue.Dequeue();
                test = queue.Sum();

                if (primes.IsPrime(test) && queue.Count > count)
                {
                    longest = test;
                    count = queue.Count;
                }
            }

            return longest;
        }
    static void Main()
    {
        int n = Int32.Parse(Console.ReadLine());

        bool[] primes = new bool[n+1];

        for (int i = 2; i <= n; i++)
        {
            primes[i] = true;
        }

        PrimeNumbers prime = new PrimeNumbers();

        prime.SieveOfEratosthenes(primes);

        for (int i = 2; i <= n; i++)
        {
            if (primes[i] == true)
            {
                Console.Write(i + " ");
            }
        }
        Console.WriteLine();
    }
Example #39
0
 public void Teardown()
 {
     _instance = null;
 }
Example #40
0
 public void Setup()
 {
     _instance = new PrimeNumbers();
 }
Example #41
0
 public void PrimeEnsureICanCreateClassInstance()
 {
     PrimeNumbers prime = new PrimeNumbers();
     Assert.IsNotNull(prime);
 }
        public int Execute(int nthPrimeNumber)
        {
            var primeNumbers = new PrimeNumbers().Execute(10000).ToArray();

            return primeNumbers[nthPrimeNumber - 1];
        }
 public void TestCleanup()
 {
     primeNumbers = null;
 }
 public void TestInitialize()
 {
     primeNumbers = new PrimeNumbers();
 }