Esempio n. 1
0
        public void Run()
        {
            int  a = -999, b = -999;
            long testILongInt = 0;
            long maxConsecutivePrimes = 0, maxACoefficient = 0, maxBCoefficient = 0;

            for (int i = a; i <= 999; i++)
            {
                for (int j = b; j <= 999; j++)
                {
                    int n = 0, consecutivePrimes = 0;
                    while (true)
                    {
                        testILongInt = GetQuadratic(i, j, n++);
                        if (testILongInt > 0 && Primes.IsPrime(testILongInt))
                        {
                            consecutivePrimes++;
                        }
                        else
                        {
                            if (consecutivePrimes > maxConsecutivePrimes)
                            {
                                maxConsecutivePrimes = consecutivePrimes;
                                maxACoefficient      = i;
                                maxBCoefficient      = j;
                            }
                            break;
                        }
                    }
                }
            }

            this.result = (maxACoefficient * maxBCoefficient).ToString();
        }
Esempio n. 2
0
        public long GetLengthWhereRationBelow(int percent)
        {
            long antallPrimetallFunnet = 0;
            long step    = 0;
            long len     = 1;
            long counter = 0;
            long current = 1;

            while (true)
            {
                counter++;

                //side komplett,sjekk ration
                if (counter % 4 == 1)
                {
                    if (counter > 1 && antallPrimetallFunnet * 10 < counter)
                    {
                        break;
                    }
                    //øk steg og sidelengde
                    step += 2;
                    len  += 2;
                }
                current += step;

                if (Primes.IsPrime(current, primliste))
                {
                    antallPrimetallFunnet++;
                }
            }
            return(len);
        }
Esempio n. 3
0
        public Fraction GetRatio(int sidelength)
        {
            long isPrime = 0;
            long start   = 1;
            long step    = 0;

            long len     = 1;
            long counter = 0;
            long current = start;

            while (len <= sidelength)
            {
                counter++;
                if (counter % 4 == 1)
                {
                    step += 2;
                    len  += 2;
                }
                current = current + step;

                if (Primes.IsPrime(current, primliste))
                {
                    isPrime++;
                }
            }

            return(new Fraction {
                Numerator = isPrime, Denominator = counter
            });
        }
Esempio n. 4
0
        public long GetSmallestOddCompisteNotSum()
        {
            bool goldbachsotherconjecture = true;
            long i = 1;

            while (goldbachsotherconjecture)
            {
                i += 2;

                if (!Primes.IsPrime(i))
                {
                    bool thisgoldbachsotherconjecture = false;

                    long j    = 1;
                    long rest = i - 2 * j * j;
                    while (!thisgoldbachsotherconjecture && rest > 0)
                    {
                        if (Primes.IsPrime(rest))
                        {
                            thisgoldbachsotherconjecture = true;
                        }
                        j++;
                        rest = i - 2 * j * j;
                    }

                    goldbachsotherconjecture = thisgoldbachsotherconjecture;
                }
            }
            return(i);
        }
Esempio n. 5
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            Primes.InitPrimes();

            int i = 9;

            while (true)
            {
                bool foundOddComposite = false;
                if (Primes.IsPrime(i))
                {
                    i += 2; continue;
                }
                long sqrt = (long)Math.Sqrt(i);
                for (int j = 1; j < sqrt; j++)
                {
                    long diff = i - 2 * j * j;
                    if (Primes.IsPrime(diff))
                    {
                        foundOddComposite = true;
                        break;
                    }
                }
                if (!foundOddComposite)
                {
                    return(i);
                }
                i += 2;
            }
        }
Esempio n. 6
0
        static bool CanStick(long n1, long n2)
        {
            if (n1 == n2)
            {
                return(false);
            }
            if (n2 < n1)
            {
                return(CanStick(n2, n1));
            }
            byte cachedValue = _stickCache[n1][n2];

            if (cachedValue == CAN_STICK)
            {
                return(true);
            }
            if (cachedValue == CAN_NOT_STICK)
            {
                return(false);
            }

            _stickChecks++;
            // Test n1 followed by n2
            long m2 = GetDigitCountAsMultOfTen(n2);
            long n  = n2 + m2 * 10 * n1;

            if (!_primes.IsPrime(n))
            {
                _stickCache[n1][n2] = CAN_NOT_STICK;
                return(false);
            }

            // Test n2 followed by n1
            long m1 = GetDigitCountAsMultOfTen(n1);

            n = n1 + m1 * 10 * n2;
            if (!_primes.IsPrime(n))
            {
                _stickCache[n1][n2] = CAN_NOT_STICK;
                return(false);
            }
            else
            {
                _stickCache[n1][n2] = CAN_STICK;
                return(true);
            }
        }
Esempio n. 7
0
 private long GetNextPrime(long next, List <long> listPrimeNumber)
 {
     while (!Primes.IsPrime(next, listPrimeNumber))
     {
         next++;
     }
     return(next);
 }
Esempio n. 8
0
        public void Run()
        {
            SortedSet <long> primes = new SortedSet <long>();
            SortedSet <long> generatedComposites = new SortedSet <long>();
            SortedSet <long> composites          = new SortedSet <long>();

            int  batchStart = 0, batchLength = 100;
            long topPrime = 0, topComposite = 3;

            while (true)
            {
                for (long i = batchStart; i < batchStart + batchLength; i++)
                {
                    if (Primes.IsPrime(i))
                    {
                        primes.Add(i);
                        topPrime = i;
                    }
                }

                for (long i = topComposite; i < topPrime; i++)
                {
                    if (i % 2 != 0 && !primes.Contains(i))
                    {
                        composites.Add(i);
                        topComposite = i;
                    }
                }

                foreach (long prime in primes)
                {
                    long generatedComposite = 0;
                    int  baseInt            = 1;
                    while (generatedComposite < topPrime)
                    {
                        generatedComposite = GenerateComposite(prime, baseInt++);
                        if (!generatedComposites.Contains(generatedComposite))
                        {
                            generatedComposites.Add(generatedComposite);
                        }
                    }
                }

                foreach (long composite in composites)
                {
                    if (!generatedComposites.Contains(composite))
                    {
                        this.result = composite.ToString();
                        return;
                    }
                }

                composites.Clear();
                batchStart += batchLength;
            }
        }
Esempio n. 9
0
        public void Run()
        {
            int  step        = 2;
            long currentInt  = 3;
            long countPrimes = 1;
            long diagonals   = 1;
            int  sideLength  = 1;

            float ratio = 100f;

            Direction current = Direction.Left;

            while (ratio > 10)
            {
                switch (current)
                {
                case Direction.Up:
                    step = step + 2;
                    break;

                case Direction.Left:
                case Direction.Right:
                case Direction.Down:
                default:
                    break;
                }

                currentInt += step;
                if (currentInt != 2 && Primes.IsPrime(currentInt))
                {
                    countPrimes++;
                }

                if (current == Direction.Right)
                {
                    diagonals  += 4;
                    ratio       = (float)100 * (float)countPrimes / (float)diagonals;
                    sideLength += 2;
                }


                if (current != Direction.Up)
                {
                    current++;
                }
                else
                {
                    current = Direction.Left;
                }
            }

            this.result = sideLength.ToString();
        }
Esempio n. 10
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            var signs = new List <int> {
                1, -1
            };
            var max = (int)input;

            Primes.InitPrimes(max);
            var bPrimes    = Primes.AllPrimes;
            var maxIndex   = 0;
            var maxProduct = new QuadraticCoefficients(0, 0);

            Primes.InitPrimes(80 * 80 + 80 * 1000 + 1000);
            var SquareHash = new Dictionary <int, long>();

            for (int i = 0; i <= max; i++)
            {
                SquareHash.Add(i, (long)Math.Pow(i, 2));
            }

            for (int a = -1 * max + 1; a < max; a += 2)
            {
                foreach (var prime in bPrimes)
                {
                    foreach (var sign in signs)
                    {
                        var  b = prime * sign;
                        bool isPrime;
                        var  tempIndex = 0;
                        do
                        {
                            var quad = SquareHash[tempIndex] + tempIndex * a + b;
                            tempIndex++;
                            isPrime = Primes.IsPrime(quad);
                        } while (isPrime);

                        if (tempIndex > maxIndex)
                        {
                            maxIndex   = tempIndex;
                            maxProduct = new QuadraticCoefficients(a, (int)b);
                        }
                    }
                }
            }
            if (Logging)
            {
                Console.WriteLine(String.Format("{0}:{1}", maxProduct.A, maxProduct.B));
            }
            return(maxProduct.A * maxProduct.B);
        }
Esempio n. 11
0
        public long SumOfStrongRightTruncatableHarshadPrimes(long max)
        {
            long sum = 0;

            prinmeListe = Primes.GetPrimeFactorsBelowNumber((long)Math.Sqrt(902200000800800));
            var file = File.CreateText(@"c:\temp\HarshadNumbers.txt");

            List <long> rightTruncatableHarshadNumbers = new List <long>();

            //finn rightTruncatableHarshadNumber melloem 10 og 100
            for (int n = 10; n < 100; n++)
            {
                if (IsRightTruncatableHarshadNumber(n))
                {
                    rightTruncatableHarshadNumbers.Add(n);
                }
            }

            for (int p = 1; p < max - 1; p++)
            {
                List <long> nextRightTruncatableHarshadNumbers = new List <long>();

                // 'left'-expand de kjente TruncatableHarshad nummerne
                foreach (var n in rightTruncatableHarshadNumbers)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        //left...
                        long t = n * 10 + i;

                        if (IsStrongHarshadNumber(n) && Primes.IsPrime(t, prinmeListe))
                        {
                            sum += t;
                            file.WriteLine(t.ToString());
                        }

                        //til neste 'runde'
                        if (IsRightTruncatableHarshadNumber(t))
                        {
                            nextRightTruncatableHarshadNumbers.Add(t);
                        }
                    }
                }

                rightTruncatableHarshadNumbers = nextRightTruncatableHarshadNumbers;
            }

            file.Close();
            return(sum);
        }
Esempio n. 12
0
 public void Run()
 {
     for (int i = 999999999; i > 0; i--)
     {
         if (i % 2 != 0 && i % 10 != 0 && i % 3 != 0 &&
             i % 5 != 0 && i % 7 != 0 &&
             MathUtils.IsPandigital(i) &&
             Primes.IsPrime(i))
         {
             this.result = i.ToString();
             break;
         }
     }
 }
Esempio n. 13
0
            public void Solve()
            {
                _found = false;
                for (var i = 1000; i < 10000; i++)
                {
                    IsPrime[i - 1000] = Primes.IsPrime(i);
                }

                for (var i = 1000; i < 10000; i++)
                {
                    if (i != 1487 && IsPrime[i - 1000])
                    {
                        var d0     = i % 10;
                        var d1     = (i % 100) / 10;
                        var d2     = (i % 1000) / 100;
                        var d3     = i / 1000;
                        var second =
                            Try(i, d3, d2, d0, d1) +
                            Try(i, d3, d0, d2, d1) +
                            Try(i, d3, d1, d2, d0) +
                            Try(i, d3, d1, d0, d2) +
                            Try(i, d3, d0, d1, d2) +
                            Try(i, d2, d3, d1, d0) +
                            Try(i, d2, d3, d0, d1) +
                            Try(i, d0, d3, d2, d1) +
                            Try(i, d1, d3, d2, d0) +
                            Try(i, d1, d3, d0, d2) +
                            Try(i, d0, d3, d1, d2) +
                            Try(i, d2, d0, d3, d1) +
                            Try(i, d2, d1, d3, d0) +
                            Try(i, d0, d2, d3, d1) +
                            Try(i, d1, d2, d3, d0) +
                            Try(i, d0, d1, d3, d2) +
                            Try(i, d1, d0, d3, d2) +
                            Try(i, d2, d1, d0, d3) +
                            Try(i, d2, d0, d1, d3) +
                            Try(i, d1, d2, d0, d3) +
                            Try(i, d0, d2, d1, d3) +
                            Try(i, d1, d0, d2, d3) +
                            Try(i, d0, d1, d2, d3);
                        if (second != 0)
                        {
                            var third = 2 * second - i;
                            WriteLine(i * 100000000L + second * 10000L + third);
                        }
                    }
                }
            }
Esempio n. 14
0
        public bool IsStrongRightTruncatableHarshadPrimes(long number)
        {
            prinmeListe = Primes.GetPrimeFactorsBelowNumber((long)number);
            long rest = number / 10;

            if (IsRightTruncatableHarshadNumber(rest) &&
                IsStrongHarshadNumber(rest) &&
                Primes.IsPrime(number, prinmeListe)
                )
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 15
0
        public void IsPrime()
        {
            for (int i = 1; i <= 100; i++)
            {
                Test(i);
            }

            Test(1000000000037);             // 10^12
            Test(1000000000039);
            Test(1L << 40);
            Test((1L << 40) - 1);

            void Test(long n)
            {
                Assert.AreEqual(Primes.Factorize(n).Length == 1, Primes.IsPrime(n));
            }
        }
Esempio n. 16
0
        private int SumOfAmicableParis(int input, bool Logging)
        {
            Primes.InitPrimes(input);
            for (int i = 2; i < input; i++)
            {
                if (Primes.IsPrime(i))
                {
                    continue;
                }

                int sumFactorsI;
                int sumFactorsAmicableI;
                if (_LongToSumOfProperFactorsHash.ContainsKey(i))
                {
                    sumFactorsI = _LongToSumOfProperFactorsHash[i];
                }
                else
                {
                    sumFactorsI = (int)Factors.SumFactors(i, true);
                    _LongToSumOfProperFactorsHash.Add(i, sumFactorsI);
                }

                if (_LongToSumOfProperFactorsHash.ContainsKey(sumFactorsI))
                {
                    sumFactorsAmicableI = _LongToSumOfProperFactorsHash[sumFactorsI];
                }
                else
                {
                    sumFactorsAmicableI = (int)Factors.SumFactors(sumFactorsI, true);
                    _LongToSumOfProperFactorsHash.Add(sumFactorsI, sumFactorsAmicableI);
                }

                if (i == sumFactorsAmicableI && i != sumFactorsI)
                {
                    if (!_AmicableNumbers.Contains(i))
                    {
                        _AmicableNumbers.Add(i);
                        _AmicableNumbers.Add(sumFactorsI);
                    }
                }
            }
            return(_AmicableNumbers.Sum());
        }
Esempio n. 17
0
        public bool IsConcatenatingPrimes(long prime1, long prime2)
        {
            string s1 = prime1.ToString() + prime2.ToString();
            long   p1 = long.Parse(s1);



            if (Primes.IsPrime(p1, primliste))
            {
                s1 = prime2.ToString() + prime1.ToString();
                p1 = long.Parse(s1);

                if (Primes.IsPrime(p1, primliste))
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 18
0
        public void Run()
        {
            long i = 0;

            // Generate primes up to half te current
            for (i = 0; i < 323; i++)
            {
                if (Primes.IsPrime(i))
                {
                    primes.Add(i);
                }
            }
            long currInt   = 646;
            int  currCount = 0;

            while (true)
            {
                if ((currInt + 1) % 2 == 0)
                {
                    if (Primes.IsPrime(++i))
                    {
                        primes.Add(i);
                    }
                }

                if (IsDivisibleByFourPrimes(++currInt))
                {
                    currCount++;
                }
                else
                {
                    currCount = 0;
                }

                if (currCount == 4)
                {
                    this.result = (currInt - 3).ToString();
                    return;
                }
            }
        }
Esempio n. 19
0
        private bool RecurivePerm(int number, int[] availableNumberlist, int currentLevel)
        {
            if (availableNumberlist.Length == 1)
            {
                if (Primes.IsPrime(number * 10 + availableNumberlist[0]))
                {
                    LargestPandigitalPrime = number * 10 + availableNumberlist[0];
                    return(true);
                }
                return(false);
            }

            foreach (int nextAviableNumber in availableNumberlist)
            {
                if (RecurivePerm(number * 10 + nextAviableNumber, availableNumberlist.Where(e => e != nextAviableNumber).ToArray(), currentLevel + 1))
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 20
0
        public void Sample()
        {
            // 素因数分解
            Console.WriteLine(string.Join(" * ", Primes.Factorize(2020)));
            // 2 * 2 * 5 * 101

            // 約数の列挙
            Console.WriteLine(string.Join(" ", Primes.Divisors(2020)));
            // 1 2 4 5 10 20 101 202 404 505 1010 2020

            // 素数判定
            Console.WriteLine(Primes.IsPrime(1000000007));
            // True

            // n 以下の素数の列挙
            Console.WriteLine(string.Join(" ", Primes.GetPrimes(100)));
            // 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

            // m 以上 M 以下の素数の列挙
            Console.WriteLine(string.Join(" ", Primes.GetPrimes(1000000000, 1000000100)));
            // 1000000007 1000000009 1000000021 1000000033 1000000087 1000000093 1000000097
        }
Esempio n. 21
0
            public void Solve()
            {
                var cPrimes = 0;
                var corner  = 1;

                for (var side = 3; side < int.MaxValue; side += 2)
                {
                    var inc = side - 1;
                    cPrimes +=
                        (Primes.IsPrime(corner + inc) ? 1 : 0) +
                        (Primes.IsPrime(corner + 2 * inc) ? 1 : 0) +
                        (Primes.IsPrime(corner + 3 * inc) ? 1 : 0) +
                        (Primes.IsPrime(corner + 4 * inc) ? 1 : 0);
                    if (10 * cPrimes < 2 * side - 1)
                    {
                        WriteLine(side);
                        break;
                    }

                    corner += 4 * inc;
                }
            }
Esempio n. 22
0
        private bool IsTruncatablePrime(int currNumber)
        {
            string numStr = currNumber.ToString();
            string subSrtRight = "", subSrtLeft = "";

            for (int i = 1; i <= numStr.Length; i++)
            {
                subSrtRight = numStr.Substring(numStr.Length - i);
                if (!Primes.IsPrime(Int32.Parse(subSrtRight)))
                {
                    return(false);
                }
                subSrtLeft = numStr.Substring(0, i);
                if (!Primes.IsPrime(Int32.Parse(subSrtLeft)))
                {
                    return(false);
                }
            }

            Console.WriteLine("Both ways truncatable prime:" + currNumber);
            return(true);
        }
Esempio n. 23
0
        public void Run()
        {
            int circPrimes = 0;

            for (int i = 2; i < 1000000; i++)
            {
                bool isFullPrime = true;
                foreach (int num in MathUtils.NumberRotations(i))
                {
                    if (!Primes.IsPrime(num))
                    {
                        isFullPrime = false;
                        break;
                    }
                }
                if (isFullPrime)
                {
                    circPrimes++;
                }
            }
            this.result = circPrimes.ToString();
        }
Esempio n. 24
0
        // A Harshad or Niven number is a number that is divisible by the sum of its digits.
        private bool IsStrongHarshadNumber(long number)
        {
            long sum = DigitSum(number);

            return(number % sum == 0 && Primes.IsPrime(number / sum, prinmeListe));
        }
Esempio n. 25
0
            public void ReturnsFalseGivenValuesLessThan2(ulong number)
            {
                bool result = Primes.IsPrime(number);

                Assert.False(result, number.ToString() + " should not be prime");
            }
Esempio n. 26
0
            public void RecognizesCompositeNumbers(ulong number)
            {
                bool result = Primes.IsPrime(number);

                Assert.False(result, number.ToString() + " should not be prime");
            }
Esempio n. 27
0
            public void RecognizesPrimeNumbers(ulong number)
            {
                bool result = Primes.IsPrime(number);

                Assert.True(result, number.ToString() + " should be prime");
            }
Esempio n. 28
0
        public void Run()
        {
            for (int i = 1000; i < 10000; i++)
            {
                if (Primes.IsPrime(i))
                {
                    byte[] bytes =
                    {
                        Byte.Parse(i.ToString()[0].ToString()),
                        Byte.Parse(i.ToString()[1].ToString()),
                        Byte.Parse(i.ToString()[2].ToString()),
                        Byte.Parse(i.ToString()[3].ToString()),
                    };
                    List <byte[]> permutations   = CombiUtils.IntCombinations(bytes, 4);
                    List <int>    primes         = new List <int>();
                    List <int>    solutionPrimes = new List <int>();
                    foreach (byte[] perm in permutations)
                    {
                        int prime = Int32.Parse(bytesToString(perm));
                        if (Primes.IsPrime(prime))
                        {
                            primes.Add(prime);
                        }
                    }
                    primes.Sort();
                    primes = primes.Where(x => x.ToString().Length == 4).ToList();
                    for (int firstIndex = 0; firstIndex < primes.Count(); firstIndex++)
                    {
                        bool found       = false;
                        int  secondIndex = 1;
                        while (secondIndex < primes.Count())
                        {
                            int diff = primes[secondIndex] - primes[firstIndex];
                            for (int l = primes.Count - 1; l > secondIndex; l--)
                            {
                                if (diff == primes[l] - primes[secondIndex] && diff != 0)
                                {
                                    solutionPrimes.Add(primes[firstIndex]);
                                    solutionPrimes.Add(primes[secondIndex]);
                                    solutionPrimes.Add(primes[l]);
                                    found = true;
                                    break;
                                }
                            }
                            if (found)
                            {
                                break;
                            }
                            secondIndex++;
                        }
                        if (found)
                        {
                            break;
                        }
                    }

                    if (solutionPrimes.Count == 3)
                    {
                        if (solutionPrimes[1] - solutionPrimes[0] == solutionPrimes[2] - solutionPrimes[1])
                        {
                            Console.WriteLine("Prime 1: {0} Prime 2: {1} Prime 3: {2} - Diff value: {3}",
                                              solutionPrimes[0],
                                              solutionPrimes[1],
                                              solutionPrimes[2],
                                              solutionPrimes[2] - primes[1]
                                              );
                        }

                        if (solutionPrimes[0] != 1487)
                        {
                            this.result = solutionPrimes[0] + "" + solutionPrimes[1] + "" + solutionPrimes[2];
                            return;
                        }
                    }
                }
            }
        }
Esempio n. 29
0
        public void IsPrime_ValidValues_ReturnsIfValueIsPrime(int testValue, bool expectedResult)
        {
            bool result = Primes.IsPrime(testValue);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
Esempio n. 30
0
        public void IsPrime_ShouldBeTrueForPrimesOnly(long number, bool expectedValue)
        {
            bool value = Primes.IsPrime(number);

            Assert.AreEqual(expectedValue, value);
        }