Example #1
0
        static void Main(string[] args)
        {
            long elementAt = new SieveOfEratosthenes(500000).Primes().ElementAt(10000);

            Console.WriteLine(elementAt);
            Console.ReadLine();
        }
Example #2
0
        public void IsSeqIntTest()
        {
            var sieve = new SieveOfEratosthenes();

            Assert.Equal(sieve.IsSeqInt(1000), true);
            Assert.Equal(sieve.IsSeqInt(889), false);
        }
Example #3
0
        public void NonDivisorTest()
        {
            var sieve        = new SieveOfEratosthenes();
            var expectResult = new int[] { 2, 4, 3, 2, 0 };

            Assert.Equal(expectResult, sieve.CountOfNonDivisor(new int[] { 3, 1, 2, 3, 6 }));
        }
Example #4
0
    public static void Main()
    {
        SieveOfEratosthenes sieve = new SieveOfEratosthenes(100000);

        sieve.Run();
        int[] q = new int[100010];
        for (int i = 1; i <= 100000; i++)
        {
            if (sieve.IsPrime(i) && sieve.IsPrime((i + 1) / 2))
            {
                q[i] = q[i - 1] + 1;
            }
            else
            {
                q[i] = q[i - 1];
            }
        }
        int Q = NextInt();

        for (int i = 0; i < Q; i++)
        {
            int a = NextInt() - 1, b = NextInt();
            Console.WriteLine(q[b] - q[a]);
        }
    }
Example #5
0
        public void SieveOfEratosthenesReturnsEmptyListWhenGiven0()
        {
            var results = SieveOfEratosthenes.GeneratePrimesUpTo(0);

            Assert.NotNull(results);
            Assert.False(results.Any());
        }
Example #6
0
File: Q1.cs Project: Tilps/Stash
 static void Main(string[] args)
 {
     SieveOfEratosthenes c = new SieveOfEratosthenes();
     object o = c.lastScratch(8);
     PrintObj(o);
     System.Console.In.ReadLine();
 }
Example #7
0
    public static void Main()
    {
        int N = Cin <int>();
        SieveOfEratosthenes soe = new SieveOfEratosthenes(1000010);

        soe.Run();
        Console.WriteLine(soe.IsPrime(N) ? "YES" : "NO");
    }
 private static long SieveSolver()
 {
     var sieve = new SieveOfEratosthenes((long) Math.Sqrt(NUM));
     //var sieve = new SieveOfEratosthenes(50000);
     return (from p in sieve.Primes()
             where NUM%p == 0
             select p).Max();
 }
Example #9
0
        public void TestPrime()
        {
            //arrange
            int[] expected = Prime.TestNumbers;
            Prime sequence = new SieveOfEratosthenes(expected.Last());

            //act assert
            TestSequence(sequence, expected);
        }
Example #10
0
 public void TestGetPrimes_MultipleSievesRequired()
 {
     //arrange
     int max = 150000000;
     //act
     Sequence   sequence = new SieveOfEratosthenes(max);
     List <int> primes   = sequence.Numbers;
     //assert no error
 }
 public void Solve()
 {
     /* ans: 142,913,828,922
      * long.MaxValue   9,223,372,036,854,775,807
      * ulong.MaxValue  18,446,744,073,709,551,615
      */
     var sieve = new SieveOfEratosthenes(2000000);
     var result = sieve.Primes().Sum();
     Console.WriteLine(GetType() + ": " + result);
 }
Example #12
0
        public void PrimesTest()
        {
            var sieve        = new SieveOfEratosthenes();
            var expectPrimes = new bool[] { false, false, true, true, false, true, false, true, false, false, false };

            Assert.Equal(expectPrimes, sieve.Primes(10));
            var expectSemiPrimes = new bool[] { false, false, false, false, true, false, true, false, false, true, true, false, false };

            Assert.Equal(expectSemiPrimes, sieve.SemiPrimes(12));
        }
Example #13
0
        public void SieveOfEratosthenesTest()
        {
            //N: 10, Time: 0 ms
            //N: 100, Time: 2 ms
            //N: 1000, Time: 2706 ms
            //N: 10000, Time: 2590731 ms ~ 43.178 min

            SieveOfEratosthenes t = new SieveOfEratosthenes(30);

            t.DoSieve();
        }
Example #14
0
        public void PrimesWithin_Returns_PrimeNumbersWithinRange()
        {
            int[] expectedPrimes =
            {
                2,   3,  5,  7, 11, 13, 17, 19, 23, 29,
                31, 37, 41, 43, 47, 53, 59, 61, 67,
                71, 73, 79, 83, 89, 97, 101
            };
            var primes = SieveOfEratosthenes.PrimesWithin(102);

            Assert.Equal(expectedPrimes, primes);
        }
Example #15
0
        private static void Main(string[] args)
        {
            const double targetNumber        = 600851475143;
            var          sieveOfEratosthenes = new SieveOfEratosthenes((long)Math.Sqrt(targetNumber));

            foreach (long l in sieveOfEratosthenes.Primes().Reverse().Where(l => targetNumber % l == 0))
            {
                Console.WriteLine(l);
                break;
            }
            Console.ReadLine();
        }
Example #16
0
        public void TestGetPrimes_To104729()
        {
            //arrange
            int max = 104729;
            //act
            Sequence   sequence = new SieveOfEratosthenes(max);
            List <int> primes   = sequence.Numbers;

            //assert
            Assert.AreEqual(10000, primes.Count);
            Assert.AreEqual(2, primes[0]);
            Assert.AreEqual(104729, primes[primes.Count - 1]);
        }
Example #17
0
        public void SieveOfEratosthenesGeneratesCorrectResults()
        {
            var results = SieveOfEratosthenes.GeneratePrimesUpTo(MaxNumber);

            Assert.NotNull(results);
            Assert.True(results.Any());
            Assert.Equal(results.Count(), 25);
            Assert.DoesNotContain(1, results);
            Assert.Contains(2, results);
            Assert.Contains(7, results);
            Assert.Contains(23, results);
            Assert.Contains(41, results);
            Assert.Contains(97, results);
        }
Example #18
0
        /// <summary>
        /// The main program execution path
        /// </summary>
        /// <param name="args">
        /// The arguments supplied
        /// </param>
        public static void Main(string[] args)
        {
            var input     = new ConsoleInput(args);
            var generator = new SieveOfEratosthenes();

            Console.WriteLine("Generating prime numbers...");

            var output = generator.GeneratePrimeNumbers <ConsoleOutput>(input);

            output.WritePrimeTable();

            Console.WriteLine("Please any key to quit");
            Console.ReadKey();
        }
Example #19
0
        public void TestGetPrimes_To10()
        {
            //arrange
            int max = 10;
            //act
            Sequence   sequence = new SieveOfEratosthenes(max);
            List <int> primes   = sequence.Numbers;

            //assert
            Assert.AreEqual(4, primes.Count);
            Assert.AreEqual(2, primes[0]);
            Assert.AreEqual(3, primes[1]);
            Assert.AreEqual(5, primes[2]);
            Assert.AreEqual(7, primes[3]);
        }
Example #20
0
        public void PrintAllPrimesThatAreAtLeastTheDoulbeOfThePrevious()
        {
            const int startPrime = 31;
            //           Int32.Max = 2147483647
            const int targetNumber = 2100000000;
            var       primes       = SieveOfEratosthenes.GetAllPrimeNumbers(targetNumber)
                                     .SkipWhile(number => number < startPrime)
                                     .ToArray();
            var doubledPrimes = FilterDoublePrimes(primes);

            _output.WriteLine($"All primes from {startPrime} up to {targetNumber} that are at least double the previous prime.");
            foreach (var doubledPrime in doubledPrimes)
            {
                _output.WriteLine($"{doubledPrime},");
            }
        }
Example #21
0
        public void Base()
        {
            var       sut          = new SieveOfEratosthenes();
            const int input        = 31;
            var       primesUpTo31 = new [] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };

            var result = sut.FindPrimesUpTo(input);

            for (var i = 0; i < result.Length; i++)
            {
                if (primesUpTo31.Contains(i))
                {
                    Assert.True(result[i]);
                }
            }
        }
 public void TestSieveSum()
 {
     foreach (int n in new[]
         {
             1234, 23864, 15892, 10953, 9113,
             8852, 24049, 12318, 24606, 9065,
             1806, 18358, 19330, 9866, 8792,
             14852, 5287, 14560, 25234, 29748,
             15579, 174, 8924, 10325, 23220,
         })
     {
         var sieve = new SieveOfEratosthenes(n);
         //Console.WriteLine("{0},{1}", sieve.Primes().Sum(), sieve.Primes().Count());
         Console.WriteLine("({0},{1}),", n, sieve.Primes().Sum());
     }
 }
Example #23
0
        public void FourPrimesTest()
        {
            var sieve = new SieveOfEratosthenes();
            //Assert.Equal(sieve.FindFourPrimes(1000), new int[4]{1,3,4,5});
            var primes = sieve.Primes(1000);

            // 863 * 811 * 563 * 313
            Assert.Equal(true, sieve.IsSeqInt(123334444567));
            Assert.Equal(new bool[4] {
                primes[863], primes[811], primes[563], primes[313]
            }, new bool[4] {
                true, true, true, true
            });
            Assert.Equal(sieve.FindFourPrimes2(1000), new int[4] {
                1, 3, 4, 5
            });
        }
Example #24
0
    public static void Main()
    {
        int N = NextInt();
        SieveOfEratosthenes sieve = new SieveOfEratosthenes(55555);

        sieve.Run();
        foreach (var v in sieve.list)
        {
            if (v % 5 == 2)
            {
                Console.Write(v + " ");
                N--;
            }
            if (N == 0)
            {
                break;
            }
        }
        Console.WriteLine();
    }
    private void GetPrimesEratosthenes()
    {
        var a = new SieveOfEratosthenes((ulong)range);

        Debug.Log($"Total {a.Count} prime numbers in range {range}");
    }
Example #26
0
        public void GetAllPrimes(int targetNumber, int[] expectedPrimeNumbers)
        {
            var actual = SieveOfEratosthenes.GetAllPrimeNumbers(targetNumber);

            actual.Should().BeEquivalentTo(expectedPrimeNumbers);
        }
Example #27
0
        public void SieveOfEratosthenes()
        {
            var result = new SieveOfEratosthenes().Calculate(N);

            Assert.Equal(_primes, result);
        }
        public void GetPrimeNumbersTests(int count, int[] expected)
        {
            var actual = SieveOfEratosthenes.GetPrimeNumbers(count);

            CollectionAssert.AreEqual(expected, actual);
        }
Example #29
0
 public void TestInitialize()
 {
     Sieve = new SieveOfEratosthenes();
 }
 public void GetPrimeNumbers_LengthOfSequenceLessThanOne_ThrowArgumentException(int count)
 => Assert.Throws <ArgumentException>(() => SieveOfEratosthenes.GetPrimeNumbers(count),
                                      message: "Method throws ArgumentException in case length of the sequence is less than 1.");
Example #31
0
 public static bool IsPrime(this int value)
 {
     return(SieveOfEratosthenes.IsPrime(value));
 }