python中素数_Python中最接近的素数

1586010002-jmsa.png

I need a user to enter a number and enter out the closest prime number to the value they put in. I am struggling on how to check the prime numbers before and after the number they put in. The last part is to print the smaller value of the two prime numbers if they are the same distance away from the inputted number.

n = int(input("Enter n: "))

holder1 = n

holder2 = n

prime = True

holder3 = 0

holder4 = 0

for i in range(2,n):

if (n % i) == 0:

prime = False

if(prime == True):

print("The prime closest to " + str(n) + " is " + str(n))

else:

while (prime == False):

holder1 -= 1

holder2 += 1

for i in range(2,holder1):

if (n % i) == 0:

prime = False

else:

prime = True

holder3 = holder1

for i in range(2,holder2):

if (n % i) == 0:

prime = False

else:

prime = True

holder4 = holder2

if(abs(n - holder3) <= abs(n-holder4)):

print("The prime closest to " + str(n) + " is " + str(holder3))

elif (abs(n - holder3) > abs(n-holder4)):

print("The prime closest to " + str(n) + " is " + str(holder4))

解决方案

If I understood your question correctly, you are trying to find a way of finding the closest number to the inputted number. If this is the case, the Sieve of Eratosthenes method to calculate all of the prime numbers up to a given range, and then find the prime to the number you entered

# Import math for the infinity functionality

import math

# The Sieve of Eratosthenes method of calculating the primes less than the limit

def getPrimes(limit):

# The list of prime numbers

primes = []

# The boolean list of whether a number is prime

numbers = [True] * limit

# Loop all of the numbers in numbers starting from 2

for i in range(2, limit):

# If the number is prime

if numbers[i]:

# Add it onto the list of prime numbers

primes.append(i)

# Loop over all of the other factors in the list

for n in range(i ** 2, limit, i):

# Make them not prime

numbers[n] = False

# Return the list of prime numbers

return primes

# The number to find the closest prime of

number = int(input("Enter a number: > "))

# The list of primes using the function declared above

primes = getPrimes(number + 100)

# The distance away from the closest prime

maxDist = math.inf

# The closest prime

numb = 0

# Loop all of the primes

for p in primes:

# If the prime number is closer than maxDist

if abs(number - p) < maxDist:

# Set maxDist to the number

maxDist = abs(number - p)

# Set numb to the number

numb = p

# Print the output

print(numb, "is the closest prime number to the number you entered!")

I hope this answers your question

***** EDIT *****

You said that you cannot use the python math library, so below is the slightly adjusted code that does not use it:

# The Sieve of Eratosthenes method of calculating the primes less than the limit

def getPrimes(limit):

# The list of prime numbers

primes = []

# The boolean list of whether a number is prime

numbers = [True] * limit

# Loop all of the numbers in numbers starting from 2

for i in range(2, limit):

# If the number is prime

if numbers[i]:

# Add it onto the list of prime numbers

primes.append(i)

# Loop over all of the other factors in the list

for n in range(i ** 2, limit, i):

# Make them not prime

numbers[n] = False

# Return the list of prime numbers

return primes

# The number to find the closest prime of

number = int(input("Enter a number: > "))

# The list of primes using the function declared above

primes = getPrimes(number + 100)

# The distance away from the closest prime

maxDist = 99999999

# The closest prime

numb = 0

# Loop all of the primes

for p in primes:

# If the prime number is closer than maxDist

if abs(number - p) < maxDist:

# Set maxDist to the number

maxDist = abs(number - p)

# Set numb to the number

numb = p

# Print the output

print(numb, "is the closest prime number to the number you entered!")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值