[英]Python - function returns true when a given number is a prime number or else false
Hi I'm a beginner and I'm stuck on this question that wants me to use only while loop to solve.嗨,我是初学者,我被困在这个问题上,希望我只使用 while 循环来解决。 The question wants me to write a function that returns True when the given number is a prime number and it returns False if the given number is not a prime number.
这个问题要我写一个 function,当给定数字是素数时返回 True,如果给定数字不是素数,则返回 False。
My code so far:到目前为止我的代码:
def is_prime(n):
i = 2
while i <= n//2:
if n%i != 0:
return True
else:
return False
i+=1
The problem I have is I think my code displays the correct output for numbers 4 and above and it returns 'None' for 1, 2, and 3. I've debugged it and I think the problem is the while loop condition.我遇到的问题是我认为我的代码为数字 4 及以上显示正确的 output 并且它为 1、2 和 3 返回“无”。我已经调试过它,我认为问题是 while 循环条件。 But I don't know how to fix it.
但我不知道如何解决它。 I would appreciate it if any of you pros can help me out!
如果你们中的任何一个专业人士可以帮助我,我将不胜感激!
edit: I changed the while condition but 1 still returns None.. and 2 returns False when it's supposed to return True编辑:我更改了 while 条件,但 1 仍然返回 None .. 并且 2 在应该返回 True 时返回 False
def is_prime(n):
i = 2
while i <= n:
if n%i != 0:
return True
else:
return False
i+=1
import math;
def is_prime(n):
i = 2
while i < max(math.sqrt(n),2):
if n%i != 0:
return True
else:
return False
if i == 2:
i+=1
else
i+=2
You could hard-code these 3 cases, in case you dont want to use sqrt
:你可以硬编码这 3 种情况,以防你不想使用
sqrt
:
def is_prime(n):
i = 2
if n in (1,3):
return True
elif n == 2:
return False
while i <= n//2:
if n%i != 0:
return True
else:
return False
i+=1
for x in range(1, 5):
print(x, '=', is_prime(x))
Output: Output:
(1, '=', True)
(2, '=', False)
(3, '=', True)
(4, '=', False)
Want to get really fancy?想要变得非常花哨吗? Make a Sieve of Eratosthenes:
做一个埃拉托色尼筛子:
def is_prime(n):
a = list()
# Assume all are prime
a[0:n+1] = (n+1)*[1]
# Start with removing even numbers
i = 2
while i*i <= n:
print ("I: ", i)
# Set all divisible by i to 0
a[0:n+1:i] = len(a[0:n+1:i])*[0]
# If a[n] is zero, return False
if a[n] == 0:
return False
# Increment i until we have a prime number
while a[i] == 0:
i+=1
if a[n] == 0:
return False
else:
return True
If you want to impress your lesson teacher you can show him a fast probabilistic prime number isprime for numbers larger than 2**50.如果你想给你的课老师留下深刻印象,你可以给他看一个快速的概率素数 isprime for numbers 大于 2**50。 I haven't found any errors in it after weeks of cpu time stress testing it on a 6 core AMD:
在 6 核 AMD 上进行了数周的 cpu 时间压力测试后,我没有发现任何错误:
import random
import math
def lars_last_modulus_powers_of_two(hm):
return math.gcd(hm, 1<<hm.bit_length())
def fast_probabilistic_isprime(hm):
if hm < 2**50:
return "This is to only be used on numbers greater than 2**50"
if lars_last_modulus_powers_of_two(hm+hm) != 2:
return False
if pow(2, hm-1, hm) == 1:
return True
else:
return False
def fast_probabilistic_next_prime(hm):
if hm < 2**50:
return "This is to only be used on numbers greater than 2**50"
if hm % 2 == 0:
hm = hm + 1
hm += 2
while fast_probabilistic_isprime(hm) == False:
hm += 2
return hm
""" hm here is bitlength, which must be larger than 50.
usage is create_probabilistic_prime(1000)
"""
def create_probabilistic_prime(hm):
if 2**hm < 2**50:
return "This is to only be used on numbers greater than 2**50"
num = random.randint(2**hm,2**(hm+1))
return fast_probabilistic_next_prime(num)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.