程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[computer test questions (implementation language: python3)] find the two prime numbers that make up an even number

編輯:Python

Title Description
Any even number ( Greater than 2) It can be done by 2 It's a prime number , Make up even numbers of 2 There are many kinds of prime numbers , This topic requires the output of the prime pair with the smallest difference between two prime numbers constituting a specified even number .
This question contains several groups of sample input .
Input description :

 Enter an even number

Output description :

 Output two prime numbers

Example 1
Input

20

Output

7
13

The code implementation is as follows :

def func():
while True:
try:
n = int(input())
nums = []
# find n All prime numbers in
for i in range(n):
times = 0
for j in range(2,i):
if i%j == 0:
times +=1
break
if i >2 and times == 0:
nums.append(i)
mins = []
min1 = n
# Find the difference minimum prime pair
for i in range(len(nums)):
for j in range(i,len(nums)):
min_v = abs(nums[i]-nums[j])
if nums[i]+nums[j] ==n and min1>min_v:
min1 = min_v
mins = [nums[i],nums[j]]
for i in mins:
print(i)
except Exception as e:
#print(e)
break
if __name__ == '__main__':
func()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved