Beispiel #1
0
    static PPATH()
    {
        // 10000 because zero-based indices. This isn't great as we don't need 10000
        // (as most #s don't represent primes), but hopefully it doesn't matter. Edges
        // exist between primes (a vertex whose index is prime) and other primes,
        // when there's a one-digit swap to go between them (swaps are reversible).
        _primeGraph = new SimpleGraph(10000);
        var primeDecider = new SieveOfEratosthenesDecider(9999);

        // If n is a prime, connect to the primes greater than it, within a one-digit swap. Only
        // greater than because lesser primes were already connected to it earlier in the loop.
        for (int n = 1001; n <= 9999; n += 2)
        {
            if (!primeDecider.IsOddPrime(n)) continue;

            int nSwapped = n + 1000;
            while (nSwapped % 10000 > n)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                    _primeGraph.AddEdge(n, nSwapped);

                nSwapped += 1000;
            }

            nSwapped = n + 100;
            while (nSwapped % 1000 > n % 1000)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                    _primeGraph.AddEdge(n, nSwapped);

                nSwapped += 100;
            }

            nSwapped = n + 10;
            while (nSwapped % 100 > n % 100)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                    _primeGraph.AddEdge(n, nSwapped);

                nSwapped += 10;
            }

            nSwapped = n + 2;
            while (nSwapped % 10 > n % 10)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                    _primeGraph.AddEdge(n, nSwapped);

                nSwapped += 2;
            }
        }
    }
Beispiel #2
0
    public static string Solve()
    {
        var decider = new SieveOfEratosthenesDecider(_100Million);
        var output = new StringBuilder();

        output.Append(2);
        output.AppendLine();
        int count = 1;

        for (int n = 3; n <= _100Million; n += 2)
        {
            if (decider.IsOddPrime(n) && ++count == 101)
            {
                output.Append(n);
                output.AppendLine();
                count = 1;
            }
        }

        return output.ToString();
    }
Beispiel #3
0
 public bool IsOddPrime(int n)
 => _decider.IsOddPrime(n);
Beispiel #4
0
    static PPATH()
    {
        // 10000 because zero-based indices. This isn't great as we don't need 10000
        // (as most #s don't represent primes), but hopefully it doesn't matter. Edges
        // exist between primes (a vertex whose index is prime) and other primes,
        // when there's a one-digit swap to go between them (swaps are reversible).
        _primeGraph = new SimpleGraph(10000);
        var primeDecider = new SieveOfEratosthenesDecider(9999);

        // If n is a prime, connect to the primes greater than it, within a one-digit
        // swap. Only greater than because lesser primes were already connected to it
        // earlier in the loop.
        for (int n = 1001; n <= 9999; n += 2)
        {
            if (!primeDecider.IsOddPrime(n))
            {
                continue;
            }

            int nSwapped = n + 1000;
            while (nSwapped % 10000 > n)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                {
                    _primeGraph.AddEdge(n, nSwapped);
                }

                nSwapped += 1000;
            }

            nSwapped = n + 100;
            while (nSwapped % 1000 > n % 1000)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                {
                    _primeGraph.AddEdge(n, nSwapped);
                }

                nSwapped += 100;
            }

            nSwapped = n + 10;
            while (nSwapped % 100 > n % 100)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                {
                    _primeGraph.AddEdge(n, nSwapped);
                }

                nSwapped += 10;
            }

            nSwapped = n + 2;
            while (nSwapped % 10 > n % 10)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                {
                    _primeGraph.AddEdge(n, nSwapped);
                }

                nSwapped += 2;
            }
        }
    }