Project Euler 60: Prime pair sets

Primes 3, 7, 109, 673 is very interesting, which take any two prime numbers in an arbitrary order to form stitching is still a prime number. For example, 109,7109 and 1097 removed and 7 are prime numbers. Four primes are 792, and is the smallest prime number four has such properties. Numbers by five prime satisfying the above properties and minimum.

Analysis: This question makes the solution very surprising, and in fact this problem clique problem in graph theory related. To understand this, we need to know the basics a little graph theory. If you have a better understanding of graph theory, this section can be skipped. Graph theory is a branch of combinatorics, graph is the main object of study of graph theory. FIG pattern called by a number refers to a given vertex and edge connecting two vertices constituted such graphics are often used to describe the relationship between something. Vertex and used to represent things, is used to connect two vertices represent the relationship between two things have. Figure according to whether the directional edge which can be divided into a directed graph, according to whether the edge is entitled to no weight chart and can be divided into the right to re-map and have no right to re-map, we discussed here today just unweighted undirected graph, other FIG does not involve the type here.

Back to the issue here, the subject requires us to find the minimum number of five prime properties to meet and then we first meet these needs find prime properties. The nature of these primes i.e., forward and reverse splicing may still configured between any two primes. We can imagine these prime numbers become nodes in the graph, if they meet the nature of the subject said between two prime numbers, then we can draw an edge between the two prime numbers. Then the subject can be converted into a problem to find five vertices satisfying the condition in FIG consisting of a prime number, which is five vertices between any two of China Unicom. Now we can clearly see, this is actually the clique problem in graph theory. The so-called clique problem (clique problem) is to find a complete subgraph in one figure, between the holons twenty-two vertices in the graph are connected. A typical application clique problem is in social network analysis which, we can use the personal representative of graph vertices, with mutual understanding between the two sides on behalf of graph, then the clique problem is equivalent to find a small group in a crowd , all members of the small group know each other. As the figure below by the six vertices of FIG, {1, 2, 5} three vertices constitute a group, because they twenty-two between both interconnected.

Most of the clique problem is difficult, that is polynomial time algorithm does not exist, but there are some better algorithm for solving than violence, such as the famous Bron-Kerbosch algorithm , we can see the details of the algorithm here . Considering the python already has an excellent network analysis and graph theory library is networkx, so we directly call the library to solve the problem of the title, use this library, see the documentation . First, we want to generate prime numbers qualifying right, we wrote a function to determine the nature of the particular two functions in compliance with the title, and an optimized this little detail is greater than all primes Three of you the number of and in addition to three the remainder either as either a two, when the two prime numbers stitching together its Members and the number of exactly two primes, and the sum of each digit, so to meet the nature of the subject, each digit of the two primes in addition to three and the remainder must be the same as, or after stitching together you both the number of and the inevitable addition to more than thirty, that will be divisible by three can not be a prime number. Taking into account the number and the number of its Members and analog-togethers over greater than three, then we need only compare two prime numbers divided by three identical whether the remainder on it. Since it is determined whether the two prime numbers satisfying properties, compared with the remainder operation and then splice sub-digital-to-string determines whether an integer is a prime number then much faster, and therefore when the first judging judges whether the congruence properties, not satisfied is not after further operation needs, thereby saving time determination.

Note that, in the primes we seek to meet the conditions of the time, you need to set a reasonable upper bound, there can only be speculation, I first put on the definition of the ten thousand, prime all meet the conditions of less than ten thousand obtained Correct. Then some prime number input to networkx FIG objects, become an edges of the diagram, and in this figure, find_cliquesthe function to find all the number of vertices of the subgraph five (this function is actually used is an improvement over the Bron-Kerbosch algorithm ), and then obtain the minimum value of the respective vertices in the subgraph satisfy these conditions. In fact, within the boundaries of ten thousand, prime number satisfying the condition is really only one set, i.e. {13, 8389, 5701, 5197, 6733} five prime. To verify the set of prime numbers is indeed a prime number and a collection of more than the minimum, I beg hundred thousand all of the following conditions are satisfied, the set of prime numbers given above is indeed the smallest and therefore comply with all the conditions of the subject.

Finally visual work: The following diagram shows all primes less than one hundred and fifty, the meet title nature and size of the prime three set, can be clearly found that there are three pairs (already marked with cyan), are {3, 7, 109}, {3, 37, 67}, and {7, 19, 97}. It can be seen to solve the problem with the title of graph theory is quite elegant and intuitive.

Finally, solving the following code:

# time cost = 2.82 s ± 83.9 ms

from sympy import isprime,primerange
import networkx as nx
from networkx.algorithms.clique import find_cliques

def is_pair_prime(x,y):
    conc = lambda x,y:isprime(int(str(x)+str(y)))
    if x == 3:
        return conc(x,y) and conc(y,x)
    else:
        r = x % 3
        return y%3==r and conc(x,y) and conc(y,x)

def main(N=8400):
    res = []
    primes = list(primerange(3,N))
    index = 0
    for p in primes:
        index += 1
        for i in primes[index:]:
            if is_pair_prime(p,i):
                res.append((p,i))
    G = nx.Graph()
    G.add_edges_from(res)
    ans = [clique for clique in find_cliques(G) if len(clique)==5]
    return min(map(sum,ans))

Guess you like

Origin www.cnblogs.com/metaquant/p/11881378.html