Huawei machine test - the best prime partner

Title Description

Description Title
If both positive integers and is a prime number, then the two positive integers called "prime partner", such as 2 and 5, 6 and 13, which can be applied to encrypted communications. Now the password you learn to design a program from existing N (N is an even number) positive integers pick out several pairs' prime partner ", the selection of programs as diverse as there are four positive integers: 2,5, 6,13, 5 and 6, if a group only get into a set of "prime partner", while the 2 and 5,6 and 13 consist obtained two "prime partner", can be composed of "prime partner" most the program is called "the best solution", of course you want to find out the password learn "best practices."
Input:
there is a positive even number N (N≤100), indicates the number of natural numbers to be selected. Specific numbers given later, in the range [2,30000].
Output:
Output an integer K, that you obtained "the best solution" consisting of "prime partner" of a number.

Description Input:
Input Description
1 inputs a positive even integer n
2 input n integers
output Description:
determined "best" consisting of "prime partner" logarithm.

Example 1

Entry

4
2 5 6 13

Export

2

The following is my implementation (running time expires, the god of seeking optimization algorithm). The basic idea is to use a recursive approach each pick two from the list


import time
import math


num = int(input("输入数量:"))
string = input("输入数组:").split(' ')
list_final =[]
list_input =[]
for i in string:
    list_input.append(int(i))

count =0
MAX_count=0
def is_pram(data):
    for i in range(2,int(math.sqrt(data))+1):
        if data % i == 0:
            return False
    return True

def param_calculate(list_data):
    total_param_count = 0
    for i in range(0,int(len(list_data)/2)):
        sum = list_data[i*2]+list_data[i*2+1]
        if is_pram(sum) == True:
            total_param_count += 1
    return total_param_count

def circulate(list_data):
    global count,MAX_count
    total_param_count = 0
    list_temp = list_data[:]
    if len(list_data)<=2:
        list_final.append(list_temp[0])
        list_final.append(list_temp[1])
         total_param_count = param_calculate(list_final)
        if MAX_count < total_param_count:
            MAX_count = total_param_count

        list_final.pop(-1)
        list_final.pop(-1)
        return 0
    else:
        for i in range(1,len(list_temp)):
            list_final.append(list_temp[0])
            list_final.append(list_temp[i])
            list_temp_2 = list_temp.copy()
            list_temp_2.pop(i)
            list_temp_2.pop(0)
            circulate(list_temp_2)
            list_final.pop(-1)
            list_final.pop(-1)

start=time.time()
circulate(list_input)
print("“最佳方案”组成“素数伴侣”的对数:",MAX_count)
end=time.time()
print("final is in %f ms" %((end-start)*1000))
Released nine original articles · won praise 9 · views 6612

Guess you like

Origin blog.csdn.net/ZuoFengYeCao/article/details/103740638